Teradata: Date value division with 365 days - sql

Needs an update what exactly the below query does when it divisible by 365??
select CAST((FLGHT_DATE )/365 AS INTEGER);
Note: FLGHT_DATE column is of date type and in YYYY-MM-DD format.

Related

Converting a numeric value into Date value in DB2

I have a DB2 table where NUM column is defined as INTEGER in DB2 and the query result is shown below,
NUM columns have numeric values which needs to be converted to date format. This numeric values are nothing but duration from 01.01.1850. Example : 01.01.1850 + 57677 days = 01.12.2007.
So Is it possible to convert or cast the numeric value into date fields in DB2 , so that the select query from the table can result as shown below after converting a numeric field into date field,
You may use the scalar ADD_DAYS function:
SELECT EMP_ID, ADD_DAYS('1850-01-01', NUM) AS NUM
FROM yourTable;
Not all Db2 products & versions have the ADD_DAYS function.
The following expression works for all of them.
You may optionally add DAY or DAYS at the end.
DATE ('1850-01-01') + 57677

Convert Timestamp to Date in excel

I got data from server to do some power bi job but the date format like timestamp and can't convert it to decimal or to date
0x00000000079A367B
In SQL Server; a timestamp column is automatically updated whenever a row is updated, and the value is monotonically increasing in the database. There is absolutely no relation to date and time. https://learn.microsoft.com/en-us/answers/questions/238819/purpose-to-use-timestamp-datatype-in-sql-server.html
But you can cast this hexadecimal value to a bigint value as the following:
SELECT CAST (0x00000000079A367B AS BIGINT); --output is 127547003
or
SELECT CAST (your_column AS BIGINT) as Num From Your_Table
Check this demo.

Subtracting dates in Proc SQL/SAS

I have a Proc SQL code block running in SAS Enterprise Guide. I need to find the number of days between two dates
proc sql;
select col_A,col_B,col_C, (put (date(),yymmdd10.) - col_C) as age_bucket
from DB2.Table_A;
quit;
col_C is a date of the 'YYYY-MM-DD' format (e.g. 2022-05-02)
I am subtracting col_C from today's date and want to get the total number of days between them in as age_bucket. I am getting the following error.
ERROR: Expression using subtraction (-) requires numeric types.
How do I go about this?
The Table_A is from DB2 database.
Using the PUT() function to convert the current date into a character string is going to make it impossible to perform arithmetic with the result.
If COL_C has a DATE value it does not matter how the value is displayed (formats just impact how values are displayed). A DATE value is just the number of days since 1960. You only need to subtract the two numbers to calculate the difference in days.
(date() - col_C) as age_bucket
If COL_C has a DATETIME value (number of seconds since 1960) then first convert it to a DATE value.
(date() - datepart(col_C)) as age_bucket
If COL_C is a character string in the style YYYY-MM-DD then use the INPUT() function with the YYMMDD informat to convert the string into a date value.
(date() - input(col_C,yymmdd10.)) as age_bucket
I think DB2 supports the DAYS_BETWEEN() function which will give you the number of days between the 2 date arguments.
https://www.db2tutorial.com/db2-date-functions/
Use YRDIF Function to get age in years. Subtracting dates will get your age in days.
Both dates should be SAS dates, numeric with a date format.
proc sql;
select col_A,
col_B,
col_C,
floor(YRDIF(input(col_C, yymmdd10.), today(),"AGE")) as age_bucket
from DB2.Table_A;
quit;

How to convert SAS E dates into readable dates

Hi I'm working in SAS platform and I've a data_set with more then 30 columns. there are two date columns in that data-set. dates in that data set are in format as 1.33E12
This is the little part of my table
I want to create a new data-set with few columns and then I'm exporting it to excel file.
my code is
dataset
othercolumns | date1 | date2
- 1.33E12 2.53E14
proc sql noprint;
create table my_data_set as
select ID, col_1, col_2, date1, date2
from data_set;
quit;
I want my date values in date1 and date2 column in a readable format like 10feb2017 as date9. SAS date format so they can be exported to my excel file. right now with E power dates I'm getting ####### as date1 and date2 columns in excel
I've tried
select ID, col_1, col_2, datepaart(date1), datepart(date2)
Warning: Invalid Argument, getting '.' values in date column
select ID, col_1, col_2, date1 date9., date2 date9.
select ID, col_1, col_2, date1 DATEw., date2 DATEw.
Error: Syntax error
select ID, col_1, col_2, date1 format=DATE9., date2 format=DATE9.
Getting the same E date values in my table
select ID, col_1, col_2, put(date1 , date9. ), put(date2 , date9.)
Error: Date value out of range
How to convert the E date into a readable format into my table so i can export it to excel?
this is my export code
ods excel file ="C:\data.xlsx";
ods excel close;
proc export
data = work.my_data_set
dbms = xlsx
outfile = "C:\data.xlsx"
replace;
quit;
data have;
unix_ts = 253402300799;
put unix_ts= datetime21.;
sas_dt = unix_ts + '01JAN1970:0:0'DT ;
put sas_dt= datetime21.;
run;
proc sql;
create table want as
select
(
case
when unix_ts + '01JAN1970:0:0'DT > '27FEB8000:0:0'DT then unix_ts + '01JAN1970:0:0'DT - 2 * 86400
when unix_ts + '01JAN1970:0:0'DT > '28FEB4000:0:0'DT then unix_ts + '01JAN1970:0:0'DT - 1 * 86400
else unix_ts + '01JAN1970:0:0'DT
end
) as sas_date format=datetime21.
from have;
quit;
Rather than cutting and pasting you should understand what is going on with the case statement and the 01-JAN-1970
253,402,300,799
Unix timestamp, seconds from 01-JAN-1970, representing 31-DEC-9999:23:59:59
Likely sentinel value contained in valid_to that OP imprecisely shows as
2.534E14
Date columns presumed to be Unix time stamps.
253,717,747,199
SAS datetime value '31-DEC-9999:23:59:59'DT is seconds from 01-JAN-1960
Timestamp conversion
Unix timestamp values are epoch 01-JAN-1970:0:0:0
SAS datetime values are epoch 01-JAN-1960:0:0:0
So one would presume a SAS values are 10 years (in seconds) greater than Unix value.
The simple approach is to add the epoch base differential to the Unix timestamp to achieve the SAS datetime
SAS_DT = UNIX_TS + '01JAN1970'DT; *Naive conversion;
However, this is incorrect because Unix and SAS calendaring disagree on some leap years!
253,402,300,799 is 31-DEC-9999:23:59:59 per https://www.epochconverter.com/
253,717,747,199 is '31-DEC-9999:23:59:59'DT
difference, 315,446,400 should be SAS '01-JAN-1970:0:0'DT. But the difference is actually '30DEC1969:00:00'DT.
So, adding the epoch baseline differential to a far off Unix timestamp will result in a SAS datetime that does not represent the same calendar point as in Unix.
In other words 253,402,300,799 + '01-JAN-1970:0:0'DT is '02-JAN-10000:0:0'DT -- two days beyond the expected Unix sentinel
Or, after about 8,000 years, the calendar accounting systems in Unix and SAS will deviate by 2 days.
Calendaring deviation
Unix calendaring considers year 4000 to be a leap year, 29-FEB-4000 is valid.
SAS calendaring incorrectly considers 4000 to be a non-leap year, '29-FEB-4000'DT is invalid.
ly4000 = '29-FEB-4000:0:0'DT;
-------------------
77
ERROR: Invalid date/time/datetime constant '29-FEB-4000:0:0'DT.
ERROR 77-185: Invalid number conversion on '29-FEB-4000:0:0'DT.
The same deviation happens again in year 8,000.
The least damaged conversion of Unix timestamp to SAS datetime takes the naïve conversion and subtracts one day for each misaligned leap-day determination in the time frame.
It looks as though you have two different types of UNIX timestamps, which count the number of milliseconds or microseconds from a particular date and time - usually 1st January 1970 00:00:00.000000. Without knowing exactly what sort of timestamps they are, I can only make an educated guess as to how to convert them to human-readable dates. Here are a few possible interpretations:
data example;
date1=2.53e14;
date2=1.33e12;
run;
proc sql;
create table want as
select
intnx('year',datepart(date/1e3),10,'s') format = yymmdd10. as date1a,
intnx('year',datepart(date/1e6),10,'s') format = yymmdd10. as date1b,
intnx('year',datepart(date2/1e3),10,'s') format = yymmdd10. as date2a,
intnx('year',datepart(date2/1e6),10,'s') format = yymmdd10. as date2b
from example;
quit;
The logic here is:
Divide by 1000 or 1000000 to convert to seconds
Interpret the result as a SAS datetime value counting the number of seconds from 1st January 1960 00:00:00
Extract the date component from the datetime
Add 10 years to convert to the UNIX epoch
Hopefully one of these looks right to you.

convert date to integer in postgresql

I'm trying to convert a date (date type) into int. This int should be something like the number of days since the 1 January 1900. How to get this in postgresql? In excel I'm getting this automatically when i concatenate a date with a string.
Example : 2011/11/01 convert into int as 36831
Simply subtract the two dates:
select date '2011-11-01' - date '1900-01-01'
the result will be the number of days.
More details in the manual:
http://www.postgresql.org/docs/current/static/functions-datetime.html