convert MM/DD/YYYY to YYYYMMDD in redshift - sql

I have a requirement to convert MM/DD/YYYY to YYYYMMDD in amazon redshift database.
My result of this query gives me some weird result. Can some one please help me.
select to_date ('07/17/2017','YYYYMMDD');
0007-07-20

If you just wish to convert the hard-coded string into a DATE:
select to_date('07/17/2017', 'MM/DD/YYYY')
If you have a column already formatted as DATE, then use:
to_char(fieldname, 'YYYYMMDD')
Combining the two concepts:
select to_char(to_date('07/17/2017', 'MM/DD/YYYY'), 'YYYYMMDD')

TO_DATE - converts a date represented in a character string to a DATE data type.
TO_CHAR - converts a time stamp or numeric expression to a character-string data format.
select to_char(sysdate,'YYYYMMDD');
If I’ve made a bad assumption please comment and I’ll refocus my answer.

Related

convert int to date in redshift

I have column named datenum which is int type. For ex. 20220208
Need to convert datenum column type to date with YYYY/MM/DD format.
Tried to_date("datenum", 'YYYY/MM/DD') but isn't working.
Dates in Redshift (or any SQL database) do not really have any internal format. To convert your input text dates to bona fide Redshift dates, use TO_DATE with the appropriate date mask:
SELECT TO_DATE(datenum, 'YYYYMMDD')
FROM yourTable;
If you really want to view your input dates in some other text format, then do a full roundtrip back to string, using TO_CHAR:
SELECT TO_CHAR(TO_DATE(datenum, 'YYYYMMDD'), 'YYYY/MM/DD')
FROM yourTable;

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 to convert a VARCHAR to a date in ORACLE SQL?

I have that field in my table:
2020-01-16T10:55:16..296518000
How to convert this Varchar into a date in format 'YYYY-MM-DD' ?
I tried:
select TRUNC(to_date(DATE_UPDATED ,'YYYY-MM-DD hh24:mi:ss')) from JOB_SCHEDULE_TBL
but I'm getting an error:
ORA-01830: date format picture ends before converting entire input string
Just use substr():
select to_date(SUBSTR(DATE_UPDATED, 1, 10) ,'YYYY-MM-DD')
The trunc() is unnecessary.
You are confusing the format of DATE with what you are seeing on the screen. A DATE data type has no "format". It's Oracle's internal, binary format. Even when you SELECT TO_DATE ..., the result is going to get displayed by the client, and to do so the client will have to peform (under the covers) a TO_CHAR on the resulting DATE. And that implied to_char will use the current system/session/ settings for TNS_DATE_FORMAT.

In oracle SQL how to get todays date in the format of lets say 01-JAN-15?

Just want to know In oracle SQL how to get todays date in the format of lets say 01-JAN-15?
So it should return 06-DEC-15
Use to_char(). This is how you convert dates to a string with any format you like:
select to_char(sysdate, 'DD-MON-YY')
The explanation of the format is here.

ORA-01722 INVALID NUMBER in oracle

I am getting invalid number error message while executing the below select statement.Can any one have an idea about the issue..Please let me know.
select TO_DATE(TO_CHAR('2015/01/22 00:00:00','YYYY/MM/DD'),'YYYY/MM/DD')
actually i want oracle standard date format without time stamp for this date '2015/01/22 00:00:00'
select to_date('2015/01/22 00:00:00','YYYY/MM/DD HH24:MI:SS') as dt
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/1/0
As an FYI, the Oracle DATE data type does include the time component (just not down to fractional seconds, as is the case with the TIMESTAMP data type).
If you are converting values and want to bring all the time values to zero you can use the trunc function like this (which changes 12:07:00 to 00:00:00):
select trunc(to_date('2015/01/22 12:07:00','YYYY/MM/DD HH24:MI:SS'),'DD') as dt_with_time_zerod
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/2/0
If the source is itself a date and you want to convert the date to a string in the Oracle default date format ('DD-MON-RR') you can achieve that by running:
select to_char(trunc(to_date('2015/01/22 12:07:00','YYYY/MM/DD HH24:MI:SS'),'DD'),'DD-MON-RR') as dt_with_time_zerod
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/3/0
If it's a date field, to_char without a mask will give you what you say you want.
actually i want oracle standard date format without time stamp for this date '2015/01/22 00:00:00'
I'm not sure what you mean by "Oracle standard date format." The format in which a date would appear would be based on your NLS settings (in particular, NLS_DATE_FORMAT). If you are just trying to format this string representing a date, then you might want something like the following:
SELECT TO_CHAR(TO_DATE('2015/01/22 00:00:00','YYYY/MM/DD HH:MI:SS'), 'YYYY/MM/DD')
FROM dual;
That is, you have the TO_CHAR() and TO_DATE() functions in the wrong order, and an incomplete date mask for the call to TO_DATE().
Try using date literals with the standard ISO 8601 format.
date '2015-01-22'
I suggest you not to give hour-minute-second if you do not want to show the time.
This is my simplest answer :
SELECT TO_DATE('2015/01/22','YYYY/MM/DD') FROM dual