teradata yymmdd date format - date

I have a field that should be 6 digit character but it is numeric. I am using the following code to add the leading zero:
select CAST(CAST(CHD_OPEN_DATE AS FORMAT '9(6)') AS CHAR(9))
I'm using the following code to format this as a date:
cast(cast(lpad(to_char(CHD_OPEN_DATE),6,'0') as date format 'YYMMDD') as date format 'YYYY-MM-DD')
When using this date format 1990 comes up as 2090. Is there a work-around for this?

If your number has a YYMMDD format you can use the following to cast to a date without the need to cast to an intermediate string. Assuming a date range between 1930 and 2029:
SELECT 900331 AS CHD_OPEN_DATE,
Cast(CASE WHEN CHD_OPEN_DATE < 300000
THEN CHD_OPEN_DATE + 1000000
ELSE CHD_OPEN_DATE
END AS DATE)

Related

Converting Integer values to Date in Presto SQL

Below is a script i am trying to run in Presto; Subtracting today's date from an integer field I am attempting to convert to date. To get the exacts days between. Unfortunately, it seems the highlighted block does not always convert the date correctly and my final answer is not correct. Please does anyone know another way around this or a standard method on presto of converting integer values to date.
Interger value in the column is in the format '20191123' for year-month-date
select ms, activ_dt, current_date, date_diff('day',act_dt,current_date) from
(
select ms,activ_dt, **CAST(parse_datetime(CAST(activ_dt AS varchar), 'YYYYMMDD') AS date) as act_dt**, nov19
from h.A_Subs_1 where msisdn_key=23480320012
) limit 19
You can convert "date as a number" (eg. 20180527 for May 27, 2018) using the following:
cast to varchar
parse_datetime with appropriate format
cast to date (since parse_datetime returns a timestamp)
Example:
presto> SELECT CAST(parse_datetime(CAST(20180527 AS varchar), 'yyyyMMdd') AS date);
_col0
------------
2018-05-27
You can use below sample query for your requirement:
select date_diff('day', date_parse('20191209', '%Y%m%d'), current_timestamp);

Comparing date values for two columns

I have a column called PairDt, a string that contains a date value in the last 5 characters. I want to compare that date value with the date value in the Day column, which contains dates in the YYYY-MM-DD format.
PairDt Day
----------------------------------
DCS-CNY-Yunbi-42606 2016-08-24
DCS-CNY-Yunbi-42607 2016-08-25
DCS-CNY-Yunbi-42608 2016-08-26
DCS-CNY-Yunbi-42609 2016-08-27
DCS-CNY-Yunbi-42610 2016-08-28
How do I convert Day to a value?
I'm trying to isolate Date values in PairDt that does not match the date value in Days
This 5 digit number at the end of PairDt looks like number of days since December 30th 1899. To convert this number to date use DATEADD to add as many days. To convert a date to number, use DATEDIFF to calculate the number of days. Something like this code:
declare #PairDt varchar(50) = 'DCS-CNY-Yunbi-42606', #Day date = '2016-08-24'
select DATEADD(d, cast(right(#PairDt, 5) as int), '1899-12-30'), DATEDIFF(day, '1899-12-30', #Day)

How to convert date field to this format 2018 / 01

I have one date field in my table as char "01jan2017",
I want to convert the date field to this format "2018 / 01" and there should be space between forward slash.
Thanks
If what you're looking for is just for display then here is a character conversion
data r;
date = '01jan2017'd;
date1 = compbl(put(year(date),best.)|| " / "||put(month(date),z2.));
run;
There are three key steps you need to do:
catx(' / ',year(input(date_char,date9.)),put(month(input(date_char,date9.)),z2.));
Convert the date to date9. format in order to extract the year and month,
Use the z2. format for the month to get the leading Zero,
Use Catx() to concatinate the year , month & ' / '.
Full Code:
data want;
date_char="01jan2017";
dateYYMM=catx(' / ',year(input(date_char,date9.)),put(month(input(date_char,date9.)),z2.));
run;
Output:
date_char=01jan2017 dateYYMM=2017 / 01

DB2 For i: Convert Char YYMMDD to Date

I have a CHAR_Date column containing date values in the format 'YYMMDD'.
I would like to do date arithmetic so I need to convert it into a Date data type. The problem is that the Char_Date also contains Blanks.
How do I cast the CHAR_Date to a DATE_Date column, with valid values?
SELECT
case when CHAR_Date = '' then TIMESTAMP('0001-01-01')
else TIMESTAMP_FORMAT(CHAR_Date, 'YYMMDD')
end
as DATE_Date
FROM TABLE_Data
You can use the function TIMESTAMP_FORMAT
TIMESTAMP_FORMAT('990205' , 'YYMMDD')
And if you want a date:
DATE(TIMESTAMP_FORMAT('990205' , 'YYMMDD'))

Convert a date to an integer in YYYYMMDD format in SSRS

What is the equivalent expression in SSRS of the following conversion of a date (#Date) in T-SQL?
CONVERT(INT,CONVERT(CHAR,#Date,112))
I need the date parameter value to be converted to an integer in YYYYMMDD format.
Assuming you have a date parameter called YourDate.
You could use the following expression:
=Cint(Format(Parameters!YourDate.Value, "yyyyMMdd"))
Explanation:
Step 1: Format the date to the yyyyMMdd format:
Format(Parameters!YourDate.Value, "yyyyMMdd")
Step 2: Cast the result as an int:
Cint(<FormattedDate>)
Dry coding here, but try...
=Fields!MyDateColumn.Value.Year * 10000 + Fields!MyDateColumn.Value.Month * 100 + Fields!MyDateColumn.Value.Date
Try the following expression :-
=format(cdate(Parameters!Date.Value),"yyyyMMdd")