sql date in mmddyy format in DB2 - sql

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), '/', '')

Related

Converting DD/MM/YYYY format into YYYY-MM-DD

I am struggling with a conversion from DD/MM/YYYY format into YYYY-MM-DD in NETEZZA.
I was trying some variants but it didn't worked:
SELECT REVERSE(contract_end_date)
from JNK_TABLE (was tried to revers and after do a To_DATE)
SELECT RIGHT(contract_end_date)
from JNK_TABLE
Any help will be good.
How about converting to a date?
select to_date(contract_end_date, 'DD/MM/YYYY')
Then, fix the data model so that dates are stored as dates.
Once you have it as a date, you can control the format by converting to a string:
select to_char(contract_end_date, 'YYYY-MM-DD')
If you want to find dates that have errors in the day where the day is 0, then try:
select contract_end_date
from t
where contract_end_date like '0/%' or
contract_end_date like '00/%';

How do I convert a date timestamp in oracle to a date?

I'm looking to convert (using Oracle) a date timestamp
[2018-01-25T00:00:00.000+00:00] to a date [2018-01-24]. I've tried several formats however I can't seem to find the right one to convert it. I'm unsure of how to handle the +00:00.
Thanks in advance
It depends on what you really ask.
It you have a real Oracle timestamp and you want a string in format 'YYYY-MM-DD', you can use to_char():
select to_char(col, 'YYYY-MM-DD') as res from mytable
If you have a string in ISO timestamp format, and you want a string as a result:
select substr(col, 1, 10) as res from mytable
If you have a timestamp column and you want to set the time portion to 00:00:00:
select trunc(col) as res from mytable;
This returns a value of datatype date.

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

Query that will select a timeframe from specific date to today

I'm having issues in my WHERE clause selecting data from a specific day to today's date. The day/time format in my date column is '7/2/2020 3:12:08 PM'.
I've tested a couple options but keep getting this error - 'literal does not match format string'.
Any idea's of how I can select all data from March 1, 2020 to current date?
Thanks!
In Oracle date columns are not strings, they are exactly in date datatype, so you don't need to convert/cast it. Just use simple date literals:
https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-8F4B3F82-8821-4071-84D6-FBBA21C05AC1
select * from table where your_date_columg >= date'2015-12-31'
or with to_date function for your string:
select * from table
where
your_date_columg >= to_date('2019-11-25 13:57:52',
'yyyy-mm-dd hh24:mi:ss')

DB2 Date format

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)