Change date format in oracle query - sql

When running
select processing_date from table;
i got this result "04-30-2020 20.12.49.978711"
what i want to change the format of the result to "30-APR-20"
is there a way i can do that ?
i tried select to_date(processing_date,'mm-dd-yyyy') from table; but it gives me errors
any help ?

You want to_char():
select to_char(processing_date, 'MM-DD-YYYY')
Dates are stored as an internal format, which you cannot change. If you want the date formatted in a particular way, then one solution is to convert to a string with the format you want.
EDIT:
The date appears to be a string. You can convert it to a date using:
select to_date(substr(processing_date, 1, 10), 'MM-DD-YYYY')
You can then either use as-is or use to_date() to get the format you really want.

Related

[SQL]Removing day in yyyy/mm/dd datetime format in sql

I'm using PostgreSQL, but this question is for any modern dbms
I want to basically convert a datetime column which has yyyy/mm/dd into just yyyy/mm
I tried getting months and year separately and using Concat, but the problem is the month comes as a single digit integers for values < 10 and that messes up ordering
select *,
concat(date_part('year' , date_old), '/', date_part('month' , date_old)) as date_new
from table
date _old
date_new
2010-01-20
2010-1
2010-01-22
2010-1
2010-11-22
2010-11
You can use to_char()
to_char(date_old, 'yyyy/mm')
If you want to display your date in the format YYYY-MM then
In PostgreSQL (db<>fiddle) and Oracle (db<>fiddle), use TO_CHAR:
SELECT TO_CHAR(date_old, 'YYYY/MM') FROM table_name;
In MySQL (db<>fiddle), use DATE_FORMAT:
SELECT DATE_FORMAT(date_old, '%Y/%m') FROM table_name;
In SQL Server (db<>fiddle), use CONVERT or, if you are using SQL Server 12 or later, FORMAT:
SELECT CONVERT(varchar(7), date_old, 111) FROM table_name;
SELECT FORMAT(date_old,'yyyy/MM') FROM table_name;
Don't do this.
If you're able to use the date_part() function, what you have is not actually formatted as the yyyy/mm/dd value you say it is. Instead, it's a binary value that's not human-readable, and what you see is a convenience shown you by your tooling.
You should leave this binary value in place!
If you convert to yyyy/mm, you will lose the ability to directly call functions like date_part(), and you will lose the ability to index the column properly.
What you'll have left is a varchar column that only pretends to be a date value. Schemas that do this are considered BROKEN.

Convert string of YYYYMMDD to YYYY-MM-DD Date format in Snowflake

Based on the example mentioned here in the Snowflake documentation, why are the date and timestamp values returning different values just by changing the ORDER BY clause? Also, I am trying to convert a string to a date format which is not returning correct results in Snowflake while this works fine in other SQL based Engines. Need help from experts on this.
This query
SELECT '20200710', TO_DATE('20200710');
is returning the following output
20200710 | 1970-08-22
Also tried:
SELECT TO_DATE('20200710', 'YYYY-MM-DD');
and got the error:
Can't parse '20200710' as date with format 'YYYY-MM-DD'
To convert to a date data type, you would use:
SELECT TO_DATE('20200710', 'YYYYMMDD')
I would recommend just keeping the date data type. But if you want a string in the format YYYY-MM-DD:
SELECT TO_CHAR(TO_DATE('20200710', 'YYYYMMDD'), 'YYYY-MM-DD')

SQlite compare dates without timestamp

In SQLite, how to compare date without passing timestamp?
The date format is 2018-03-18 08:24:46.101655+00 and I want to compare against only date part as 2018-03-18.
I have tried as where mydate='2018-03-18' but that didn't return any records.
Similarly, tried Date(mydate)='2018-03-18' but that didn't help either.
How can I compare dates ignoring the timestamp part?
select * from mytable
where strftime('%Y-%m-%d', mydate) = '2018-03-18'
This is not one of the supported date formats.
To extract the date part from the string, use substr():
... WHERE substr(mydate, 1, 10) = '2018-03-18'
It might be a better idea to store dates in a correct format in the database to begin with.
It is looking that there is problem with date format.
Sqlite doesn't understand data like '+00' in date.
So date() and strftime() will not work here if data type is 'timestamp with time zone'.
Try by using like clause.
Try using strftime
SELECT strftime('%Y %m %d', 'columnName');
you can find it here strftime.php

How do I convert this oracle date

I am trying to convert from one date format to another. I am not sure how to write the functions.
My source date looks like 01/15/2009 01:23:15
The format I need is 01152009.
Thanks
Try this.
TO_CHAR(TO_DATE('01/15/2009 01:23:15','MM/DD/YYYY HH:MI:SS'),'MMDDYYY')
More info here,
http://psoug.org/reference/date_func.html
Does this work for you? It assumes the date is in date format but will work with timestamp
select to_char(YourDateField,'DDMMYYYY') from dual;
You can always convert it back to a date using the TO_DATE function if you need that format.
select TO_CHAR(TO_DATE('01/15/2009 01:23:15','MM/DD/YYYY MI:HH:SS'),'MMDDYYYY') from dual
if your field is already of data type date then you should only do:
select TO_CHAR(<fieldname>,'ddmmyyyy') ...

Modify an existing to_char date format

Oracle SQL automatically converts my field D.START_DT to the following format:
TO_CHAR(D.START_DT,'YYYY-MM-DD')
Which makes it difficult for me to modify my own date format.
I've tried wrapping another TO_CHAR around it with no luck.
TO_CHAR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 'MM/DD')
And I've tried SUBSTR to select certain characters, with no luck. I think the hyphen is getting int he way.
SUBSTR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 6, 7) || '/' || SUBSTR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 9, 10)
What is the work around for this?
I agree with RMAN Express and see no problems converting dates to any format you need...
In case you still have problems try this (first to_char() in outer query is optional):
SELECT to_char(to_date(some_date, 'YYYY-MM-DD'), 'MM/DD') final_date
FROM
(
SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') some_date -- this is your "auto converted" date
FROM dual
)
/
A DATE datatype has no format. When you see a date printed on a screen, there was something that APPLIED the format you see. Could be a "default" in the program you are using (like SQL Developer) or your NLS setting, etc. But, a DATE datatype has no format. So, you have complete control over the format you see on screen.
The simplest is to use the TO_CHAR function:
select TO_CHAR(D.START_DT,'YYYY') from dual;
returns just the four digit year.
See TO_CHAR date format options.
http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements004.htm#CDEHIFJA
You should always supply the format in your code and not rely on some other "default" to supply it.