DB2 Date format - sql

I just want to format current date into yyyymmdd in DB2.
I see the date formats available, but how can I use them?
http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.intro%2Fsrc%2Ftpc%2Fdb2z_datetimetimestamp.htm
SELECT CURDATE() FROM SYSIBM.SYSDUMMY1;
I dont see any straightforward way to use the above listed formats.
Any suggestion?

SELECT VARCHAR_FORMAT(CURRENT TIMESTAMP, 'YYYYMMDD')
FROM SYSIBM.SYSDUMMY1
Should work on both Mainframe and Linux/Unix/Windows DB2. Info Center entry for VARCHAR_FORMAT().

One more solution
REPLACE (CHAR(current date, ISO),'-','')

select to_char(current date, 'yyyymmdd') from sysibm.sysdummy1
result: 20160510

This isn't straightforward, but
SELECT CHAR(CURRENT DATE, ISO) FROM SYSIBM.SYSDUMMY1
returns the current date in yyyy-mm-dd format. You would have to substring and concatenate the result to get yyyymmdd.
SELECT SUBSTR(CHAR(CURRENT DATE, ISO), 1, 4) ||
SUBSTR(CHAR(CURRENT DATE, ISO), 6, 2) ||
SUBSTR(CHAR(CURRENT DATE, ISO), 9, 2)
FROM SYSIBM.SYSDUMMY1

Current date is in yyyy-mm-dd format. You can convert it into yyyymmdd format using substring function:
select substr(current date,1,4)||substr(current date,6,2)||substr(currentdate,9,2)

Related

How to convert system date to YYYYMM format by using SQL Query?

I need to convert sysdate in YYYYMM format to validate the expiry date, this is the query I have written but am facing the below error.
select to_char (to_date(sysdate,'MM/DD/YYYY'),'YYYYMM') from dual;
ORA-01858: a non-numeric character was found where a numeric was expected
There is no reason to convert sysdate to a date. So just use:
select to_char(sysdate, 'YYYYMM')
If you wanna to format the date, try "DATE_FORMAT" way:
SELECT DATE_FORMAT('2011-11-11 13:14:01','%m%Y')
or you wanna to get the difference between the expired date and sysdate,you may try "DATEDIFF":
SELECT DATEDIFF('2001-01-01','2001-02-02') -> -32

sql date in mmddyy format in DB2

I have this simple query that is driving me crazy.
select current_date as "TODAY" from table
except that I need the date to be in mmddyy format. so If I run it today it would be 030918.
I am using DB2
If you want the date in a particular format, then convert it to a string, using to_char():
select to_char(current_date, 'MMDDYY') as "TODAY"
from table;
select replace (convert(varchar(10), GETDATE(), 1), '/', '')

Combining date and time into datetime

I would like to merge date & time. I have used 'CONCAT' function & also '||' function. But both of functions taking more times and very slow for query. Can somebody tell me how i can reduce the query time? & What function can be used to merge the date & time? Thanks for any help.
where CONCAT (TO_CHAR (Date, 'YYYYMMDD'), LPAD (Time, 6, 0))
between CONCAT ('20180215', '130528') AND CONCAT ('20180215', '133003')
You do not need to convert your date to a string - just convert your static values to a date:
WHERE "Date" BETWEEN TO_DATE( '20180215130528', 'YYYYMMDDHH24MISS' )
AND TO_DATE( '20180215133003', 'YYYYMMDDHH24MISS' )
If you want to use CONCAT then:
WHERE "Date" BETWEEN TO_DATE( CONCAT( '20180215', '130528' ), 'YYYYMMDDHH24MISS' )
AND TO_DATE( CONCAT( '20180215', '133003' ), 'YYYYMMDDHH24MISS' )
You could also use a TIMESTAMP literal:
WHERE "Date" BETWEEN TIMESTAMP '2018-02-15 13:05:28'
AND TIMESTAMP '2018-02-15 13:30:03'
You want to have an index on Date to speed up the query.
And better yet would be to have Date and Time combined into a single field Date, with type Date (and have index on Date).
This would give you a simple performant query:
WHERE Date between to_date('20180215' || '130528', 'YYYYMMDDHH24MISS') and to_date('20180215'||'133003', 'YYYYMMDDHH24MISS')
For your current scheme, you will get better performance structuring WHERE like this:
where Date between to_date('20180215', 'YYYYMMDD') and to_date('20180215', 'YYYYMMDD') and Date || LPAD (Time, 6, 0)) between CONCAT ('20180215', '130528') AND CONCAT ('20180215', '133003')

How do I convert date from DB2 format to string?

I want to convert
SELECT CURRENT DATE FROM sysibm.sysdummy1;
to 20180119 (YYYYMMDD) rather than with the hyphen 2018-01-19 (YYYY-MM-DD)
Found out how to do this,
SELECT VARCHAR_FORMAT(current_timestamp,'YYYYMMDD') FROM SYSIBM.SYSDUMMY1;
Thanks for the help everyone!
If your Db2 server runs on Linux/Unix/Windows, then you can use:
select to_char( current date,'YYYYMMDD') from sysibm.sysdummy1;
See documentation here.
select int(current_date)
from sysibm.sysdummy1
and if you really want a string as asked...
select char(int(current_date))
from sysibm.sysdummy1
SELECT VARCHAR_FORMAT(CURRENT DATE, 'YYYYMMDD')
FROM SYSIBM.SYSDUMMY1
If above is not working, use below
SELECT VARCHAR_FORMAT(TIMESTAMP(CURRENT DATE), 'YYYYMMDD')
FROM SYSIBM.SYSDUMMY1

Convert number to date sql oracle

I'm trying to convert a number (yyyymmdd) to date (mm/dd/yyyy)
For example
20150302 ====> 03/02/2015
You can try this:
select to_date(20150302,'yyyymmdd') from dual;
or
select to_char(to_date(20150302,'yyyymmdd'),'mm/dd/yyyy') from dual;
You can use TO_DATE function to convert NUMBER to DATE. Try in following:
SELECT TO_DATE(20150302, 'YYYYMMDD') FROM DUAL
The above answer is still incorrect.
It returns a character string when a date is needed.
The correct way is:
select to_date(to_char(20210416), 'YYYYMMDD') num_to_char_to_date from dual;
TO_DATE accepts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype and converts into a value of DATE datatype.
So, convert the number into string, and apply to_date. You could use single-quotation marks around the number to convert it into string.
SELECT TO_DATE('20150302', 'YYYYMMDD') FROM dual;
Remember, a DATE has no format, what you see is for display purpose. If you want to display the date in a desired format, then use TO_CHAR along with your desired format model.
SELECT TO_CHAR(TO_DATE('20150302', 'YYYYMMDD'), 'mm/dd/yyyy') FROM dual;
Read more about TO_DATE.
To convert in the required format:
select to_char(to_date(20150302, 'YYYYDDMM'), 'mm/dd/yyyy')
select to_char(to_date(20220912, 'YYYYDDMM'), 'YYYYMMDD') from dual