# SQL date filter

**URL:** <https://clarionhub.com/t/sql-date-filter/7606>\
**Category:** questions\
**Tags:** sql\
**Created:** [October 25, 2024, 7:38pm UTC](https://clarionhub.com/t/sql-date-filter/7606 "2024-10-25T19:38:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JonW](https://clarionhub.com/user_avatar/clarionhub.com/jonw/32/1344_2.png) [@JonW](https://clarionhub.com/u/JonW)\
**Post date:** [October 25, 2024, 7:38pm UTC](https://clarionhub.com/t/sql-date-filter/7606/1 "2024-10-25T19:38:24Z")

</div>

Apologies, a bit brain-dead as this point in the afternoon.

I am selecting a date (will have no time portion), from a dropdown, and I want to use that to filter a SQL file where the date-times _do_ have a time portion.

If I was providing that date as a constant in a SQL query, my WHERE clause would look like:

where date’’ = trunc(fil.date\_added)

or

where to\_date(‘\<date\_literal\>’,‘yyyy-mm-dd’) = trunc(fil.date\_added)

As I said, brain-dead. Cannot figure out how to get the date literal from clarion, whether stored as a date or as a string, into the filter I want to send. I think maybe I can construct the whole filter string and then use that, but otherwise I cannot figure out how to combine the bits I want to send directly in SQL and bits I want Clarion to put real values in, not literals.

---

<div class="post-metadata">

**Author:** ![totalhip](https://clarionhub.com/user_avatar/clarionhub.com/totalhip/32/1477_2.png) [@totalhip](https://clarionhub.com/u/totalhip)\
**Post date:** [October 25, 2024, 11:08pm UTC](https://clarionhub.com/t/sql-date-filter/7606/2 "2024-10-25T23:08:49Z")

</div>

You should be able to use date\_part in your query as documented for PG.

---

<div class="post-metadata">

**Author:** ![FedericoNavarro](https://clarionhub.com/letter_avatar_proxy/v4/letter/f/2acd7d/32.png) [@FedericoNavarro](https://clarionhub.com/u/FedericoNavarro)\
**Post date:** [October 26, 2024, 12:19am UTC](https://clarionhub.com/t/sql-date-filter/7606/3 "2024-10-26T00:19:26Z")

</div>

Jon, another alternative is with ranges: date\_added \>= singleDate and date\_added\<singleDate+1  
(inclusive on lower range and exclusive on upper range). Look which is better for the query plan

---

<div class="post-metadata">

**Author:** ![seanh](https://clarionhub.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@seanh](https://clarionhub.com/u/seanh)\
**Post date:** [October 26, 2024, 1:49am UTC](https://clarionhub.com/t/sql-date-filter/7606/4 "2024-10-26T01:49:50Z")

</div>

I’ve used either Frederico’s method or built the SQL as a string and passed that to the browse using SQL(sqlvar) in the filter.

---

<div class="post-metadata">

**Author:** ![PurpleEdge2214](https://clarionhub.com/user_avatar/clarionhub.com/purpleedge2214/32/5109_2.png) [@PurpleEdge2214](https://clarionhub.com/u/PurpleEdge2214)\
**Post date:** [October 26, 2024, 2:11am UTC](https://clarionhub.com/t/sql-date-filter/7606/5 "2024-10-26T02:11:05Z")

</div>

An easy technique is to have a filter…

Where The:DateTime \>= FilterDate AND The:DateTime \< FilterDate + 1

---

<div class="post-metadata">

**Author:** ![totalhip](https://clarionhub.com/user_avatar/clarionhub.com/totalhip/32/1477_2.png) [@totalhip](https://clarionhub.com/u/totalhip)\
**Post date:** [October 26, 2024, 1:23pm UTC](https://clarionhub.com/t/sql-date-filter/7606/6 "2024-10-26T13:23:16Z")

</div>

> [@seanh](#):
>
> built the SQL as a string and passed that to the browse using SQL(sqlvar) in the filter.

Always for me when filter is not a simple equals.Can be mixed on different filter conditions.

---

<div class="post-metadata">

**Author:** ![Niels\_Larsen](https://clarionhub.com/user_avatar/clarionhub.com/niels_larsen/32/655_2.png) [@Niels\_Larsen](https://clarionhub.com/u/Niels_Larsen)\
**Post date:** [October 26, 2024, 1:47pm UTC](https://clarionhub.com/t/sql-date-filter/7606/7 "2024-10-26T13:47:35Z")

</div>

WHERE CAST ([DATE\_FIELD] AS DATE) =‘2017-08-16’  
or if MSSQL  
WHERE CAST ([DATE\_FIELD] AS DATE) =DATEFROMPARTS(YEAR(CLA\_DATE),MONTH(CLA\_DATE),DAY(CLA\_DATE))

---

<div class="post-metadata">

**Author:** ![JonW](https://clarionhub.com/user_avatar/clarionhub.com/jonw/32/1344_2.png) [@JonW](https://clarionhub.com/u/JonW)\
**Post date:** [October 26, 2024, 3:39pm UTC](https://clarionhub.com/t/sql-date-filter/7606/8 "2024-10-26T15:39:38Z")

</div>

I think I’ll go for building the whole string. Oracle is a bit weird about passing strings to represent dates. date’stringdate’ where it is in the format yyyy-mm-dd works fine, but straight ‘stringdate’ in the same format causes trouble, and what format the server expects in that second case varies depending on server settings. So building the whole string I think is the safest option

---

<div class="post-metadata">

**Author:** ![JonW](https://clarionhub.com/user_avatar/clarionhub.com/jonw/32/1344_2.png) [@JonW](https://clarionhub.com/u/JonW)\
**Post date:** [October 28, 2024, 2:34pm UTC](https://clarionhub.com/t/sql-date-filter/7606/9 "2024-10-28T14:34:36Z")

</div>

Despite the help, after another fruitless hour, I had to go for a work-around. I added a numeric field called “batch\_added”, set that up with a key, and used a range limit. (Can’t do that with a date field because you can only select the string(8) field, and that clarion tries to send the data as a string).

Why did I have to give up? Between SQL() causing things inside it to be only totally literal, so that SQL(mylimitstring) would send “where mylimitstring…” to the server, getting messages about things not being bound, when they were, things getting unwanted quotations wrapped around them, I just could not get it to work. Plus just to complicate things this was a conditional filter on the fifth tab.

When I have some more time I will give it another spin, but I had to get the revision out, so…

---

<div class="post-metadata">

**Author:** ![JonW](https://clarionhub.com/user_avatar/clarionhub.com/jonw/32/1344_2.png) [@JonW](https://clarionhub.com/u/JonW)\
**Post date:** [October 29, 2024, 6:35pm UTC](https://clarionhub.com/t/sql-date-filter/7606/10 "2024-10-29T18:35:47Z")

</div>

OK, here are seven methods that don’t work. I haven’t found a method that does yet. In all cases the filter\_string and the date\_string are bound. This is the Oracle driver, though I don’t think it should make much difference. Date\_string is just a string representing a date (like 2024-10-29) selected from a drop-down. As you would expect, where the filtering is done after the browse opens the unfiltered list appears before you try to filter it using the dropdown.

There are some things in there I cannot explain. For example, the SQL() doesn’t seem to recognize the closing bracket as the end of what it should take literally. I suspect the replacement of & with || (the concatenation operator in Oracle) is specific to Oracle.

I don’t know why the PROP:SQLFilter wraps stuff in quotation marks.

I suspect that the ones with the BIND errors are because it cannot figure out what to do with TRUNC and DATE when they are not wrapped in SQL().

I don’t know why some of them it decides to do a \<\> ‘’.

Method 2, the forum has translated the 80 character empty string back to a single character string.

I’m probably missing something blindingly obvious, but…

Jon

What I want to send to the server is:

…where trunc(dt) = date’2024-10-31’ order by dt

**Method 1:**  
filter\_string = ‘trunc(dt) = date’‘’ & date\_string & ‘’‘’  
browse filter = SQL(filter\_string)  
SQL sent: SELECT A.ID, A.DT FROM TESTJON.“DATE\_FILTER” A WHERE ( filter\_string ) ORDER BY A.DT  
Result: Oracle 920 error (invalid relational operator)

**Method 2:**  
filter\_string = ‘trunc(dt) = date’‘’ & date\_string & ‘’‘’  
browse filter = filter\_string  
SQL sent: SELECT A.ID, A.DT FROM TESTJON.“DATE\_FILTER” A WHERE ( ’ ’ \<\> ‘’ ) ORDER BY A.DT  
Result: No error, no data

**Method 3:**  
filter\_string = ‘trunc(dt) = date’‘’ & date\_string & ‘’‘’  
browse filter – None  
SetFilter: BRW1.SetFilter(‘SQL(filter\_string)’)  
SQL sent: SELECT A.ID, A.DT FROM TESTJON.“DATE\_FILTER” A WHERE ( filter\_string ) ORDER BY A.DT  
Result: Oracle 920 error (invalid relational operator)

**Method 4:**

filter\_string = ‘trunc(dt) = date’‘’ & date\_string & ‘’‘’  
browse filter – None  
SetFilter: BRW1.SetFilter(filter\_string) (also called after resetting the filter\_string)  
SQL sent: SELECT A.ID, A.DT FROM TESTJON.“DATE\_FILTER” A WHERE ( A.DT \>= :1 ) ORDER BY A.DT  
Result: Clarion error “Bind has not been called…filter and range limits ignored”, all rows shown

**Method 5:**  
Browse filter = SQL(trunc(dt) = date’) & date\_string & SQL(‘)  
or SQL(trunc(dt) = date’‘’) & date\_string & SQL(‘’‘’)  
Result: Clarion error “Invalid expression for evaulate…filter and range limits ignored”, all rows shown

**Method 6:**  
Browse filter = SQL(‘trunc(dt) = date’‘’) & date\_string & SQL(‘’‘’)  
SQL sent: SELECT A.ID, A.DT FROM TESTJON.“DATE\_FILTER” A WHERE ( ‘trunc(dt) = date’‘’ || ’ ’ || ‘’‘’ \<\> ‘’ )  
Result: no data

**Method 7:**  
filter\_string = ‘trunc(dt) = date’‘’ & date\_string & ‘’‘’  
or 'trunc(dt) = date & date\_string ’  
brw1{PROP:SQLfilter} = filter\_string  
SQL sent: SELECT A.ID, A.DT FROM TESTJON.“DATE\_FILTER” A WHERE ( A.DT \>= :1 ) ORDER BY A.DT  
Result: all data, no filter

---

<div class="post-metadata">

**Author:** ![Rick\_UpperPark](https://clarionhub.com/user_avatar/clarionhub.com/rick_upperpark/32/346_2.png) [@Rick\_UpperPark](https://clarionhub.com/u/Rick_UpperPark)\
**Post date:** [October 29, 2024, 6:51pm UTC](https://clarionhub.com/t/sql-date-filter/7606/11 "2024-10-29T18:51:43Z")

</div>

> [@JonW](#):
>
> **Method 1:**  
> filter\_string = ‘trunc(dt) = date’‘’ & date\_string & ‘’‘’  
> browse filter = SQL(filter\_string)  
> SQL sent: SELECT A.ID, A.DT FROM TESTJON.“DATE\_FILTER” A WHERE ( filter\_string ) ORDER BY A.DT  
> Result: Oracle 920 error (invalid relational operator)

Jon,

Change this to  
browse filter = ‘SQL(’ & filter\_string & ‘)’

---

<div class="post-metadata">

**Author:** ![PurpleEdge2214](https://clarionhub.com/user_avatar/clarionhub.com/purpleedge2214/32/5109_2.png) [@PurpleEdge2214](https://clarionhub.com/u/PurpleEdge2214)\
**Post date:** [October 30, 2024, 1:08am UTC](https://clarionhub.com/t/sql-date-filter/7606/12 "2024-10-30T01:08:08Z")

</div>

[quote=“JonW, post:10, topic:7606”]  
filter\_string = ‘A.DT \>= \<39\>’ & TheDateString ‘\<39\> AND A.DT \< \<39\>’ & TheDateStringPlus1 & ‘\<39\>’

filter\_string = ‘A.DT \>= \<39\>’ & FORMAT(ClarionDate,@D010-) ‘\<39\> AND A.DT \< \<39\>’ & FORMAT(ClarionDate + 1,@D010-) & ‘\<39\>’

---

<div class="post-metadata">

**Author:** ![JonW](https://clarionhub.com/user_avatar/clarionhub.com/jonw/32/1344_2.png) [@JonW](https://clarionhub.com/u/JonW)\
**Post date:** [October 30, 2024, 11:43am UTC](https://clarionhub.com/t/sql-date-filter/7606/13 "2024-10-30T11:43:06Z")

</div>

Something _like_ that would work OK in a limited fashion in this case. Why only limited? If I write:

```auto
select date'2024-10-29' dt
from dual

```

or

```auto
select to_date('2024-10-29','yyyy-mm-dd')
from dual

```

it will always work and return a date.

If I have a table with a date in it, and write:

```auto
select *
from bank
where mod_last_dt = '2024-10-29'

```

it will fail with a “literal does not match format string” error. This is because the database will interpret date strings using the locale setting, which in my case happens to be the American “dd-mon-yyyy” format. So if I modified your code to use the @d7- format, it would work on my server now, but there is no guarantee it would work anywhere else.

---

<div class="post-metadata">

**Author:** ![JonW](https://clarionhub.com/user_avatar/clarionhub.com/jonw/32/1344_2.png) [@JonW](https://clarionhub.com/u/JonW)\
**Post date:** [October 30, 2024, 11:54am UTC](https://clarionhub.com/t/sql-date-filter/7606/14 "2024-10-30T11:54:02Z")

</div>

I really wanted that to work, but with the Oracle driver (I’ll try with the ODBC driver later), it produces:

SELECT A.ID, A.DT FROM TESTJON.“DATE\_FILTER” A WHERE ( ‘SQL(’ || ’

---

<div class="post-metadata">

**Author:** ![Rick\_UpperPark](https://clarionhub.com/user_avatar/clarionhub.com/rick_upperpark/32/346_2.png) [@Rick\_UpperPark](https://clarionhub.com/u/Rick_UpperPark)\
**Post date:** [October 30, 2024, 2:00pm UTC](https://clarionhub.com/t/sql-date-filter/7606/15 "2024-10-30T14:00:37Z")

</div>

Jon, can you show your actual code where you are setting the filter?

---

<div class="post-metadata">

**Author:** ![JonW](https://clarionhub.com/user_avatar/clarionhub.com/jonw/32/1344_2.png) [@JonW](https://clarionhub.com/u/JonW)\
**Post date:** [October 30, 2024, 2:23pm UTC](https://clarionhub.com/t/sql-date-filter/7606/16 "2024-10-30T14:23:28Z")

</div>

In Record Filter in Actions for the browse:

`'SQL(' & filter_string & ')'`

Reset field for the browse: date\_field (the use of the dropdown box to select the date)

In Event Accepted on the dropdown:

`filter_string = 'trunc(dt) = date''' & date_string & ''''`

In BIND variables in ThisWindow.Init:

```auto
bind('filter_string',filter_string)
bind('date_string',date_string)

```

---

<div class="post-metadata">

**Author:** ![JonW](https://clarionhub.com/user_avatar/clarionhub.com/jonw/32/1344_2.png) [@JonW](https://clarionhub.com/u/JonW)\
**Post date:** [October 30, 2024, 4:32pm UTC](https://clarionhub.com/t/sql-date-filter/7606/17 "2024-10-30T16:32:56Z")

</div>

I tried with an ODBC connection (with postgresql as the backend) with similar results:

`filter_string = date_trunc(day,dt) = date'2024-10-31'`

filter on the browse still` 'sql(' & filter_string & ')'`

query sent to server:

`SELECT A.ID, A.DT FROM public."date_filter" A WHERE ( 'SQL(' || '' || ')' <> '' ) AND ( A.DT >= ? ) AND ( ( A.DT > ? ) OR ( ( A.ID <= ? ) )) ORDER BY A.DT, A.ID DESC`

---

<div class="post-metadata">

**Author:** ![PurpleEdge2214](https://clarionhub.com/user_avatar/clarionhub.com/purpleedge2214/32/5109_2.png) [@PurpleEdge2214](https://clarionhub.com/u/PurpleEdge2214)\
**Post date:** [October 31, 2024, 12:13am UTC](https://clarionhub.com/t/sql-date-filter/7606/18 "2024-10-31T00:13:48Z")

</div>

> <https://stackoverflow.com/questions/76233811/filter-condition-with-date-is-not-working-in-oracle>

If you add the DATE specifier will that work?

I’m pretty sure in MSSQL the yyyy-mm-dd format works regardless of the locale setting, and maybe Oracle does the same if you tell it the string is a date?

---

<div class="post-metadata">

**Author:** ![JonW](https://clarionhub.com/user_avatar/clarionhub.com/jonw/32/1344_2.png) [@JonW](https://clarionhub.com/u/JonW)\
**Post date:** [October 31, 2024, 1:07am UTC](https://clarionhub.com/t/sql-date-filter/7606/19 "2024-10-31T01:07:48Z")

</div>

Absolutely, in the pure Oracle query situation having either date’stringdate’ or to\_date(‘stringdate’,‘format’) will work just fine. My problem is that I cannot get Clarion to send the string I want to Oracle: it adds extra quote marks, only sends absolute literal text when you use SQL(), no Clarion variables, and adds incomprehensible additions like \<\> ‘’ where it shouldn’t. So, if the idea of using “between stringdate and stringdate + 1” is to simplify what Clarion sends so that it might actually manage it, if you need to add the date’’ wrapped around your date strings it doesn’t really simplify anything.

---

<div class="post-metadata">

**Author:** ![seanh](https://clarionhub.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@seanh](https://clarionhub.com/u/seanh)\
**Post date:** [October 31, 2024, 1:24am UTC](https://clarionhub.com/t/sql-date-filter/7606/20 "2024-10-31T01:24:25Z")

</div>

> [@JonW](#):
>
> If I have a table with a date in it, and write:
> 
> ```auto
> select *
> from bank
> where mod_last_dt = '2024-10-29'
> 
> ```
> 
> it will fail with a “literal does not match format string” error. This is because the database will interpret date strings using the locale setting

That’s interesting because I often use this syntax. I use Postgres and the local is Australia (dd-mm-yyyy’) but it always interprets the date if in ISO format.

[Next page](https://clarionhub.com/t/sql-date-filter/7606.md?page=2)
