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'))
Related
I'm trying to select records from a DB2 Iseries system where the date field is greater than the first of this year.
However, the date fields I'm selecting from are actually PACKED fields, not true dates.
I'm trying to convert them to YYYY-MM-DD format and get everything greater than '2018-01-01' but no matter what I try it says it's invalid.
Currently trying this:
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYY-MM-DD') >= '2018-01-01';
it says expression not valid using format string specified.
Any ideas?
char(dateshp) is going to return a string like '20180319'
So your format string should not include the dashes.. 'YYYYMMDD'
example:
select to_date(char(20180101), 'YYYYMMDD')
from sysibm.sysdummy1;
So your code should be
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYYMMDD') >= '2018-01-01';
Charles gave you a solution that converts the Packed date to a date field, and if you are comparing to another date field, this is a good solution. But if you are comparing to a constant value or another numeric field, you could just use something like this:
select *
from table1
where val = 145
and dateShp >= 20180101;
Within my PROC SQL snipped I am trying to compare a column of datatype date9. to the date 31.12.2015'. I tried:
test_date = '31DEC2015'
This returns me the following error:
ERROR: Expression using equals (=) has components that are of different data types.
What would be the correct syntax?
Add "d" to the end of the quoted date value:
test_date = '31DEC2015'd;
I'm trying to convert value for DIM_DT_ID to MMddYY. I'm successful in doinf that. However, query fails because ultimately I'm comparing a character value to date here. Is there a way by which I can get value for DIM_DT_ID in MMddyy format and its data type still remains DATE ?
Here DIM_DT_ID
SELECT DIM_DT_ID
DIM_DT_ID >= FORMATDATE('MMddyy',ADDDAY(TO_date('yyyy-MM-dd','2016-12-21'), -25)); from abc;
Regards,
Ajay
In Denodo, to convert a string to a date field, use "to_date()" (which returns a date).
Then, don't convert back to a string, leave that field as a date (so don't use "Formatdate()", which returns a string).
So:
SELECT *
FROM MyTable
WHERE now() >= to_date('yyyy-MM-dd',myStringFieldThatLooksLikeADate)
In my example, "now()" is a date, and so is the output of the to_date function... so you can do a comparison.
If you try to convert the date back to a string using formatdate, it won't work:
#This doesn't work:
SELECT *
FROM MyTable
WHERE now() >= formatdate('MMddyy',to_date('yyyy-MM-dd',myStringFieldThatLooksLikeADate))
It doesn't work because we are comparing a date ("now()") to a string.
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)
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")