SELECT check_date
FROM check_header;
Which returns me 3/14/2014 3:24:08 PM
but I need to format this to MM-dd-yy and should be returned as 03-14-14
I tried this:
SELECT to_date(check_date,'MM-dd-yy')
From check_header;
but it is giving me a error invalid month
Try with to_char():
SELECT to_char(check_date,'MM-dd-yy') From check_header;
You need to use to_char instead of to_date:
SELECT to_char(check_date,'MM-dd-yy') from check_header;
to_date converts a string into Oracle's internal date representation, which is then displayed according to your NLS settings.
to_char converts from Oracle's internal date representation to a string using the format you specify.
Related
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')
I use function XMLELEMENT and XMLATTRIBUTES in my sql query, but I have problems with format date.
Example:
SELECT XMLELEMENT("triggers", XMLATTRIBUTES(3.2 AS "version"),
XMLELEMENT("request", XMLATTRIBUTES(1 AS "num"),
XMLELEMENT("lastname", trigg.last_name),
XMLELEMENT("firstname", trigg.first_name),
XMLELEMENT("middlename", trigg.middle_name),
XMLELEMENT("birthday", trigg.birth_date).....
Field XMLELEMENT("birthday", trigg.birth_date) output to console date in format:
<birthday>1980-01-05</birthday>
I need convert in format mask:
<birthday>05.01.1980</birthday>
Data about date in my datebase saved like 00.00.0000 and have type date.
I tried used function TO_DATE(date, 'DD.MM.YYYY'), TO_TIMESTAMP but this useless
Please, tell me how convert to needed format? Thanks.
You want to convert it FROM a date TO a CHAR, so use TO_CHAR
XMLELEMENT("birthday", TO_CHAR(trigg.birth_date, 'DD.MM.YYYY'))
I am trying to convert a specified column which is in format 'YYYY-MM-DD' and I need to convert it in MM/DD/YYYY as a data warehousing task.
The specified column is in varchar2 format.
I've been trying to use to_date, to_char but haven't succeeded yet. Any ideas?
We can try first converting your text dates into bona fide dates using TO_DATE. Then, we can use TO_CHAR to convert them to the new format.
UPDATE yourTable
SET date_col = TO_CHAR(TO_DATE(date_col, 'YYYY-MM-DD'), 'MM/DD/YYYY');
-- and maybe a WHERE clause
This being said, it is bad practice to persist your date information as text. Rather, use a proper date or timestamp column if at all possible. You would be better off creating a new date column, and then stopping after calling TO_DATE.
Inside your function/procedure, you can try this:
some_var := to_char(to_date(column_to_convert,'YYYY-MM-DD'),'MM/DD/YYYY');
It converts first the data to a date, then back to varchar2 using the desired format. Just replace the identifiers accordingly.
this will work:
select to_char(to_date('2018-10-19','YYYY-MM-DD'),'MM/DD/YYYY')from dual;
How does following query work , could not find documents explaining how following query works.Are there any other similar queries for casting values?
SELECT date '2017-01-01' FROM dual;
Gives result 1/1/2017
Your query is using a date literal. From the Oracle documentation:
You can specify a DATE value as a string literal, or you can convert a character or numeric value to a date value with the TO_DATE function. DATE literals are the only case in which Oracle Database accepts a TO_DATE expression in place of a string literal.
To specify a DATE value as a literal, you must use the Gregorian calendar. You can specify an ANSI literal, as shown in this example:
DATE '1998-12-25'
I need to convert dates like this:
3/2/2016 12:00:00 AM
to this:
2-MAR-2016
For ORACLE You can use to_char(your_date, format)
SELECT TO_CHAR(your_Date ,'DD-MON-YYYY')
FROM DUAL;
for mysql
SELECT TO_CHAR(your_Date ,'%d-%m-%Y')
FROM DUAL;
Oracle's default date format is YYYY-mm-dd. We can use the TO_CHAR method to convert to a specific format.
TO_CHAR(date, 'FMDD-MON-YYYY')
Breakdown
FMDD- Apperantly, just using DD as recommended in the documentation does not format days with a leading 0. You need to use FMDD.
MON- Abbreviated month name
%YYYY- Long year format
Reference: https://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm
In my-sql, the same could be accomplished with the DATE_FORMAT method
DATE_FORMAT(date, '%d-%b-%y')
Slightly different formatter options
Scroll down to the Datetime Format Elements