I am not sure if I understood your questions. But here is an explanation that may help you...
If you want to run a SQL query filtering by a date
Let's take Sample.Person class on the SAMPLES namespace as an example. There is a DOB (date of birth) field of type %Date. This stores dates in the $Horolog format of Caché (an integer that counts the number of dates since 12/32/1940.
If your date is in the format DD/MM/YYYY (for instance), you can use TO_DATE() function to run your query and convert this date string to the $Horolog number:
select * from Sample.Person where DOB=TO_DATE('27/11/1950','DD/MM/YYYY')
That will work independently of the runtime mode you are on (Display, ODBC or Logical).
On the other hand, if you are running your query with Runtime Select Mode ODBC, you could reformat your date string to the ODBC format (YYYY-MM-DD) and don't use TO_DATE():
select * from Sample.Person where DOB='1950-11-27'
That still is converting the string '1950-11-27' to the internal $Horolog number that is:
If you already has the date on the internal $Horolog format you could run your query using Runtime Select Mode Logical:
select * from Sample.Person where DOB=40142
You can try these queries on the management portal. Just remember changing the
If you are using dynamic queries with %Library.ResultSet or %SQL.Statement, set the Runtime Mode (%SelectMode property on %SQL.Statement) before running your query.
If you want to find records from a moving window of 30 days
The previous query brought, on my system, the person "Jafari,Zeke K.". He was born on 1950-11-27. The following query will bring all people that was born on '1950-11-27' and 30 days before '1950-11-27'. I will use DATE_ADD function to calculate this window. I have also selected ODBC Runtime Select Mode to run the query:
select Name, DOB from Sample.Person where DOB between DATEADD(dd,-30,'1950-11-27') and '1950-11-27'
Two people will appear on my system: Jafari and Quixote. Quixote was born '1950-11-04'. That is inside the window.
Moving window with current_date
You can use current_date to write queries such as "who has been born between today and 365 days ago?":
select Name, DOB from Sample.Person where DOB between DATEADD(dd,-365,current_date) and current_date
Using greater than or less than
You can also use >, >=, < or <= with dates like this:
select Name, DOB from Sample.Person where DOB >= DATEADD(dd,-365,current_date)
Just be careful with the Runtime Select Mode. The following works with ODBC Runtime Select Mode, but won't work with Display or Logical Mode:
select Name, DOB from Sample.Person where DOB >= DATEADD(dd,-30,'1950-11-27') and DOB<='1950-11-27'
To make this work with Logical Mode, you would have to apply TO_DATE to the dates first:
select Name, DOB from Sample.Person where DOB >= DATEADD(dd,-30,TO_DATE('1950-11-27','YYYY-MM-DD')) and DOB<=TO_DATE('1950-11-27','YYYY-MM-DD')
To make it work with display mode, format the date accordingly to your NLS configuration. Mine would be 'DD/MM/YYYY' because I am using a spanish location.