Date comparison with DEC and sy-datum - abap

I'm trying to perform a date comparison to sy-datum, however, it doesn' t work. I always get an empty result:
SELECT *
FROM CRMM_TERRITORY_V AS terr
INTO CORRESPONDING FIELDS OF TABLE ln_terr
WHERE sy-datum BETWEEN terr~VALID_FROM AND terr~VALID_TO.
The data type is DEC, therefore, no direct comparison seems to be possible.
Any ideas? Thanks in advance!

The table CRM_TERRITORY_Vdoes not have a column called sy-datum, try this:
SELECT *
FROM CRMM_TERRITORY_V AS terr
INTO CORRESPONDING FIELDS OF TABLE ln_terr
WHERE terr~VALID_FROM LE sy-datum
AND terr~VALID_TO GE sy-datum.

Since the validity dates are internally stored as DEC, it seems that no direct comparison is possible. My solution was to perform a datetime conversion and then compare in external format, e.g.:
CONVERT TIME STAMP terr-VALID_FROM TIME ZONE 'UTC' INTO DATE l_date_from.

Related

correct type for SQL snowflake date

I am using an SQL Script to parse a json into a table using dbt. One of the cols had this date value: '2022-02-09T20:28:59+0000'. What would be the correct way to define iso date's data type in Snowflake?
Currently, I just used the date type like this in my dbt sql script:
JSON_DATA:"situation_date"::date AS MY_DATE
but clearly, dateisn't the correct one because later when I test it using select * , I get this error:
SQL Error [100040] [22007]: Date '2022-02-09T20:28:59+0000' is not recognized
so I need to know which Snowflake date data type or datetime type suits the best with this one
Correct pulling the "date from JSON" so not so clear cut:
SELECT
'{"date":"2022-02-09T20:28:59+0000"}' as json_str
,parse_json(json_str) as json
,json:date as data_from_json
,TRY_TO_TIMESTAMP_NTZ(data_from_json, 'YYYY-MM-DDTHH:MI:SS+0000') as date_1
,TRY_TO_TIMESTAMP_NTZ(substr(data_from_json,1,19), 'YYYY-MM-DDTHH:MI:SS') as date_2
;
gives the error:
Function TRY_CAST cannot be used with arguments of types VARIANT and TIMESTAMP_NTZ(9)
Because the type of data_from_json as VARIANT and the TO_DATE/TO_TIMESTAMP function expect TEXT so we need to cast to that
SELECT
'{"date":"2022-02-09T20:28:59+0000"}' as json_str
,parse_json(json_str) as json
,json:date as data_from_json
,TRY_TO_TIMESTAMP_NTZ(data_from_json::text, 'YYYY-MM-DDTHH:MI:SS+0000') as date_1
,TRY_TO_TIMESTAMP_NTZ(substr(data_from_json::text,1,19), 'YYYY-MM-DDTHH:MI:SS') as date_2
;
If all your timezones are always +0000 you can just put that in the parse format (like example date_1), OR you can truncate that part off (like example date_2)
gives:
JSON_STR
JSON
DATA_FROM_JSON
DATE_1
DATE_2
{"date":"2022-02-09T20:28:59+0000"}
{ "date": "2022-02-09T20:28:59+0000" }
"2022-02-09T20:28:59+0000"
2022-02-09 20:28:59.000
2022-02-09 20:28:59.000
Using TRY_TO_TIMESTAMP:
SELECT TRY_TO_TIMESTAMP(JSON_DATA:"situation_date", 'format_here')
FROM tab;
so I need to know which Snowflake date data type or datetime type suits the best with this one
TIMESTAMP_INPUT_FORMAT
The specific input could be set up on ACCOUNT/USER/SESSION level.
AUTO Detection of Integer-stored Date, Time, and Timestamp Values
Avoid using AUTO format if there is any chance for ambiguous results. Instead, specify an explicit format string by:
Setting TIMESTAMP_INPUT_FORMAT and other session parameters for dates, timestamps, and times. See Session Parameters for Dates, Times, and Timestamps (in this topic).
I think ::TIMESTAMP should work for this. So JSON_DATA:"situation_date"::TIMESTAMP if you need to go just to date after, you could then to ::Date or to_Date()
After some testing, it seems to me you have 2 options.
Either you can get rid of the +0000 at the end:
left(column_date, len(column_date)-5)::timestamp
or use the function try_to_timestamp with format:
try_to_timestamp('2022-02-09T20:28:59+0000','YYYY-MM-DD"T"HH24:MI:SS+TZHTZM')
TZH and TZM both are TimeZone Offset Hours and Minutes

How to format date in where?

I have this script:
select * from OPDN A
where A."DocDate" between '2020/01/01' and '2020/01/31'
How to achieve the format "MM/DD/YYYY" in the WHERE clause?
select * from OPDN A
where A."DocDate" between '01/01/2020' and '01/31/2020'
The column DocDate is of type TimeStamp.
If the column data type is TIMESTAMP as the OP has written in a comment to Sandra Rossi's excellent answer then there are actually three conversions required to make the selection work as expected.
turn the first selection parameter (between a ...) into a date
turn the second selection parameter (between ... and b) into a date
turn the timestamp column DocDate into a date
This looks like this:
select
*
from
OPDN A
where
to_date(A."DocDate") between to_date('01/01/2020', 'MM/DD/YYYY')
and to_date('01/31/2020', 'MM/DD/YYYY');
The conversion of the selection parameters with the help of format-strings should be obvious, and the result is the same information in a SQL date data type.
The third conversion (to_date(a."DocDate")) might be surprising, but is rather important.
The way the selection is meant to work is to include everything from the very start of the selection period to the very end. If the input filters are simply converted to timestamps then they will have the time-component of 00:00 (midnight), as no specifics about the time of day are provided.
This will lead to all "DocDate" values after midnight to be excluded.
The correct level of comparison here is date, therefore the "DocDate" also needs to be converted to the hour-less date data type.
p.s. I just realized that the selection criteria are given in MM/DD/YYYY format which is probably the reason for the conversion error the OP received earlier.
It depends what is the type and format of the column DocDate.
In SAP ERP softwares (R/3, ECC, S/4HANA), the dates were historically defined as NVARCHAR types of 8 characters, with the format 'YYYYMMDD'.
In SAP Business One, I don't know...
In SAP HANA database, there are also four specific types (SAP Library "Datetime Data Types"): DATE, TIME, SECONDDATE, TIMESTAMP.
Solutions for each case:
If DocDate type is NVARCHAR 8 with format YYYYMMDD: select * from OPDN A where A."DocDate" between '20200101' and '20200131'
In SAP Business One, I don't know...
If DocDate type is DATE: select * from OPDN A where A."DocDate" between to_date('01/01/2020','DD/MM/YYY') and to_date('01/31/2020', 'DD/MM/YYY')
In Oracle, it can do that
select * from OPDN A
where to_date(A."DocDate", 'DD/MM/YYY') between to_date('01/01/2020','DD/MM/YYY') and to_date('01/31/2020', 'DD/MM/YYY');

PROC SQL: Delete rows by Date with format of e8601dt

Having some issues deleting rows from a data set. They need to be deleted by a date criteria, but the variable is in e8601dt. format. One thing I noticed about the variable is that its a number type variable, but left aligned (not sure if that has relevance or not), so I attempted to substring, and some additional attempts (below)...no success -
PROC SQL;
DELETE *
FROM DATASETS.BATCH_REPORT
WHERE datepart(BATCH_DATE) > '2015-10-01'
;
QUIT;
PROC SQL;
DELETE *
FROM DATASETS.BATCH_REPORT
WHERE BATCH_DATE > '11oct2015'd
;
QUIT;
Assuming there has to be an easy way to call out a value in this format...or will I need to convert this variable to a more compliable format, then do my processing?
OK...did some research. Apparently (and some one please correct me if I am wrong)...to use the e8601dt. format, a date value needs to be multiplied by 86400, then you can apply the format. So.....dividing by 86400 brought me back to the SAS data as an integer. This did the trick :
PROC SQL;
DELETE *
FROM SETS
WHERE ID >= 20372
;
QUIT;
You're close! Date conversions are a pain between systems. The representation of the values depends on the environment configuration.
Within proc SQL, I think you have to specify oracle functions (not the SAS datepart) Looks like you've figured out that Oracle's 'DATE' datatype stores both date&time within the same value. The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight). SAS has 2 different date types: date and datetime.
I'd suggest using the oracle to_date() function to compare against a character date, i.e.
WHERE BATCH_DATE > to_date('2015-10-01','yyyy-mm-dd')
If desired, you could use the oracle to_char(BATCH_DATE,'mm-dd-yyyy') to cast the date variable to a text value and then compare on the text value. But you loose some of the comparison power.
....edited due to new info about ...ew... db2 ..... :-)
I'm way NOT a DB2 guy, but maybe something like this?
First, set the date as in: (the date passed to DB2 needs the double quotes):
CALL SYMPUT('INT_DATE',"'"||PUT(sas_date,YYMMDDD10.)||"'");
Then use in the SQL as in:
PROC SQL ;
WHERE BATCH_DATE >= &INT_DATE
https://communities.sas.com/t5/SAS-Procedures/DB2-Date9-format-To-SAS-Serial-Date/td-p/32436

Sql Query using 'Like' is giving results but using '=' does not returns any result in Oracle

The Query using LIKE :(This query when fired gives the desired result)
select * from catissue_audit_event where event_timestamp like '16-DEC-14'
But when using query with '=' results in an empty resultset
select * from catissue_audit_event where event_timestamp='16-DEC-14'
Here event_timestamp is of type Date
Strange thing is that the query runs for other dates such as:
select * from catissue_audit_event where event_timestamp='15-DEC-14'
What can be the issue? I already checked for leading and trailing spaces in the data
Output after running the first query:
In Oracle a DATE (and of course a TIMESTAMP) column contains a time part as well.
Just because your SQL client is hiding the time, doesn't mean it isn't there.
If you want all rows from a specific day (ignoring the time) you need to use trunc()
select *
from catissue_audit_event
where trunc(event_timestamp) = DATE '2014-12-16';
Be aware that this query will not use an index on the event_timestamp column.
You should also not rely on implicit data type conversion as you do with the expression event_timestamp = '16-DEC-14. That statement is going to fail if I run it from my computer because of different NLS settings. Always use a proper DATE literal (as I have done in my statement). If you don't like the unambiguous ISO date, then use to_date():
where trunc(event_timestamp) = to_date('16-12-2014', 'dd-mm-yyyy');
You should avoid using month names unless you know that all environments (which includes computers and SQL clients) where your SQL statement is executed are using the same NLS settings. If you are sure, you can use e.g. to_date('16-DEC-14', 'dd-mon-yy')
The reason why this is different is different to the solution to your issue.
The solution to your issue is to stop performing date comparisons by implicit conversion to a string. Convert your string to a date to perform a date comparison:
select * from catissue_audit_event where event_timestamp = date '2014-12-16'
I cannot stress this enough; when performing a date comparison only compare dates.
Your column EVENT_TIMESTAMP is being implicitly (this is bad) converted to a date in accordance with your NLS_DATE_FORMAT, which you can find as follows:
select * from nls_session_parameters
This governs how date-data is displayed and implicitly converted. The reason why LIKE works and and = doesn't is because your NLS_DATE_FORMAT is masking additional data. In other words, your date has a time component.
If you run the following and then re-select the data from your table you'll see the additional time component
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss'
Thus, if you want all the data for a specific date without constraint on time you'll need to remove the time component:
select * from catissue_audit_event where trunc(event_timestamp) = date '2014-12-16'
have you tried matching the event_timestamp format example: DD-MMM-YY with the date that you are passing?

In DB2 String replace

I have 2 fields in my DB2 database: DATE and TIME. DATE is string (8/25/2013), DATE is timestamp (1/1/1970 7:00:00 AM). I want to write a sql to replace 1/1/1970 with 8/25/2103. Please help.
Thanks.
Well, you need to cast your string to a date, using the DATE function
DATE(DATE)
Where you see that's it's a bad idea to name your columns with existing function names...
Then combining the TIMESTAMP, DATE, and TIME function (again really unclear with your field names, but...)
TIMESTAMP(DATE(DATE), TIME(TIME))
Will give you a timestamp where the date part is coming from the DATE field, and the time part from the TIME field
See this and that
After casting to a DATE format, adding the difference between those two absolute dates should work -
UPDATE <table-name>
SET DATE= DATEADD(day,DATEDIFF(day,'1/1/1970','8/25/2013'),DATE)
WHERE DATE < '1/2/1970' AND DATE>='1/1/1970'
More on DATEADD and DATEDIFF
Also, it is recommended to not use 'DATE' as a column name as it is a reserved [edit] function name.