Date Parameter to Cache SQL - intersystems-cache

I am very new to Cache. I am trying to develop a report with date parameters. When I issue the SQL command:
SELECT TOP 2 ad.admission_date from system.admission ad WHERE convert(sql_date,ad.admission_date) >= convert(sql_date,'08-01-2014' )
I get what I expect two records.
One of which is 10/1/2010 12:00:00 AM.
Then if I issue the command
SELECT TOP 2 ad.admission_date from system.admission ad WHERE convert(sql_date,ad.admission_date) <= convert(sql_date,'08-01-2014' )
I get no values returned?
When I issue the command
SELECT TOP 2 {fn convert('10-03-2010', sql_date) } FROM system.admission_data
I get two NULL values. Clearly I am confused about how Cache works.

I have found that if you use the standard ODBC format (yyyy-MM-dd) for the date you don't need to use the convert and it is much more efficient:
WHERE ad.admission_date <= '2014-08-01'

I formated date incorrectly. I have my code working now. Should look something like select top 2 convert(DATE, '10/03/2010 12:00:00 AM') .... and then I can actually do comparisons.

Related

PostgreSQL TO_DATE date/time field value out of range

I am using TO_DATE in one of my PostgreSQL functions and it is throwing errors like date/time field value out of range: "2021901". This is happening for the months of January to September as I need to add zeros in front of them. So I tried to execute a simple select query there as follows as I am using the same syntax in function.
SELECT TO_DATE(2021::varchar||09::varchar||'01','YYYYMMDD')
This is also giving me the error
ERROR: date/time field value out of range: "2021901"
SQL state: 22008
Now if I change the month to October, November, or December it works fine, but for all the other months, it is showing this error. I am actually new to Postgres and not sure how to fix this. It would be very much helpful if someone can point me in the right direction. Thanks
If your input values are numbers (integer), another alternative is to use make_date()
make_date(2021,9,1)
A better and easier way would be to just provide the date correctly and use TO_DATE, so as example do this:
SELECT TO_DATE('2021-09-01', 'YYYY-MM-DD')
If you really want to go your way, you can force a leading zero by using TO_CHAR, like this:
SELECT TO_DATE(2021::varchar||TO_CHAR(09, 'fm00')||'01','YYYYMMDD')
But I recommend to take the first propose.

Custom date format iseries access odbc for SSRS

How to resolve my issue below?
1. I am pulling data from AS/400 DB2 using (iseries access odbc driver) to SSRS.
2. I want to format the column stores in integer to date format.
Sample = 20180612 below is the SQL Query.
Using below query,
SELECT CHAR(DATE(SUBSTR(DIGITS(20180612),1,4)||'-'||
SUBSTR(DIGITS(20180612),5,2)||'-'||
SUBSTR(DIGITS(20180612),7,2)),ISO) AS RESULTSDATE
Output = 2018-06-02
Question: How can I produce a below custom date format like d/m/yyyy
I have used, ISO, USA, LOCAL, JIS, EUR but no yield.
Example: 2/6/2018
If you want to do it in SQL Server, the closest you can CONVERT it to is the USA 110 (mm-dd-yyyy) in SQL Server.
If the your dataset has a field with the 2018-06-02, you can use the CDATE function is SSRS to convert it to a date and then format the text box the way you want with the FORMAT property or the FORMAT function.
Format Function:
=FORMAT(CDATE("2018-06-02"), "M/d/yyyy")
Format Property of Text Box:
/* this is as close as I can come to the desired results there is a leading zero */
select
varchar_format(
timestamp_format(char(20180612), 'YYYYMMDD')
, 'DD/MM/YYYY') as resultdate
from my8digitdatetable
/* for export IRL send out a date */
select
cast(
timestamp_format(char(20180612), 'YYYYMMDD')
as date) as resultdate
from my8digitdatetable
I got it already. I just format it to the date without formatting.
SELECT DATE(SUBSTR(DIGITS(MIN(DTAHRS.HREMCP.EMREDT)),1,4)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HREMCP.EMREDT)),5,2)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HREMCP.EMREDT)),7,2)) AS RESULTSDATE
After that, I put this as sub-query so that I can pass this to the parameter.
Below is my whole query, then at SSRS I can use date/time parameter control to compare with the results.
SELECT x.RESULTSDATE
FROM ( SELECT DATE(SUBSTR(DIGITS(MIN(DTAHRS.HRLVTP.LTLDTE)),1,4)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HRLVTP.LTLDTE)),5,2)||'-'||
SUBSTR(DIGITS(MIN(DTAHRS.HRLVTP.LTLDTE)),7,2)) AS RESULTSDATE
) as x
WHERE x.RESULTSDATE >= ? AND x.RESULTSDATE <= ?
I hope it will help you too.

sql developer issue in visualizing date

since few weeks I started working on SQL developer.
I noticed an important issue that I do not know how to solve. This is related to the way my SQL developer visualizes the date.
First of all, here are my settings:
and here is my problem:
select to_date('01-01-0001', 'DD-MM-YYYY') from dual;
Instead of correctly visualizin 01-01-2001, it visualizes 03-JAN-0001
select select to_date('01-01-2001', 'DD-MM-YYYY') from dual;
It is interesting to observe that the issue of the day disappear when we change year.
So this -
defines for your session how to display dates and timestamps.
Example:
If you were querying a date w/o supplying a date format, then it adopts said settings.
But if you query a string, and ask Oracle to treat it as a date
select to_date('1-1-1990', 'DD-MM-YYYY') from dual;
The DD-MM-YYYY tells Oracle how to interpret your string as a date.
And then the output is shown using the format defined for NLS
Now, for your scenario, the year '1' - there is a bug in SQL Developer 18.1 that is causing the date to come back as 3 vs 1.
Bug 28093149 - DATE IS RETURNED INCORRECTLY FOR 01/01/0001 - COMES BACK AS 03/01/0001

FileNet - SQL for a date property between specific hours

I would like to fetch documents that a property date hour is between midnight and 4 AM.
I tried this:
SELECT [This], [Date], FROM Folder_Type_1
WHERE DATEPART(hh,[Date]) >= 0
AND DATEPART(hh,[Date]) <= 4
ORDER BY Date
and
SELECT [This], [Date], FROM Folder_Type_1
WHERE CONVERT(VARCHAR(8),Date,108) between '00:00:00' and '04:00:00'
ORDER BY Date
But none of them is working when I test it in the SQL query builder in the FEM.
DATEPART and CONVERT are not recognised. What is the correct way to do it?
I didn't find anything interesting in this SQL syntax reference.
Thank you in advance!
You are trying to use T-SQL functions within Content Engine Query Language. While its syntax might look like SQL, it is actually not. Not to mention it is obviously not T-SQL.
As of today, it is not possible to accomplish what you want. TimeSpan function introduced in the version 5.1 allows some manipulations with date parts. Those, however, are not sufficient for your task. You might want to check TimeSpan documentation.
I have used the follwoing before:
where c.DateCreated >= 20130101T000000Z
This is a snippet from a query executed using the api an not the fem, but in principle this should be the same sql

T-Sql date format for seconds since last epoch / formatting for sqlite input

I'm guessing it needs to be something like:
CONVERT(CHAR(24), lastModified, 101)
However I'm not sure of the right value for the third parameter.
Thanks!
Well I'm trying to write a script to copy my sql server db to a sqlite file, which gets downloaded to an air app, which then syncs the data to another sqlite file. I'm having a ton of trouble with dates. If I select a date in air and try to insert it, it fails because it's not in the right format... even if it was a valid date to begin with. I figured I'd try to experiment with the unix time since that's the only thing thats worked so far. I am considering just leaving them as varchar because I don't sort by them anyway.
Last epoch is when 1970 GMT?
SELECT DATEDIFF(s,'19700101 05:00:00:000',lastModified)
See also Epoch Date
sqlite> select datetime();
2011-01-27 19:32:57
sqlite> select strftime('%Y-%m-%d %H:%M:%S','now');
2011-01-27 19:33:57
REFERENCE:
(Date time Functions)[http://sqlite.org/lang_datefunc.html]
I wound up using format 120 in MS SQL:
convert(char(24), lastModified, 120)
Each time I needed to a select a date in SQLite for non-display purposes I used:
strftime(\"%Y-%m-%d %H:%M:%S\", dateModified) as dateModified
Now I just need a readable/friendly way to display the date to the user!
edit: accept answer goes to whoever shows me how to display the date nicely from sqlite ;p
Define "last epoch". Does this come close?
Select Cast(lastModified As Integer)
If you store them as varchar, store them as YYYYMMDD. That way you CAN sort by them later if you want to.
SQL server has only 2 failsafe date formats
ISO = YYYYMMDD, run this to see that
select convert(varchar(10),getdate(),112)
ISO8601 = yyyy-mm-dd Thh:mm:ss:mmm(no spaces) run this to see that
select convert(varchar(30),getdate(),126)
To learn more about how dates are stored in SQL server I wrote How Are Dates Stored In SQL Server?