Converting records from 'YYYY-MM-DD' format to MM/DD/YYYY in PL/SQL - sql

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;

Related

SQL - Date format conversion issue (AUTO to 'MM/DD/YYYY')

In Snowflake, I have a column in a created table called "Date1", that has dates formatted as AUTO (ex. 2022-06-17). I am trying to query that column, but need the date formatted as 'MM/DD/YYYY', yet everything I've tried returns an error of some kind.
When I try date(Date1, 'MM/DD/YYYY) the error says that it can't parse 2022-06-17 for MM/DD/YYYY. When I try to_date(Date1 [MM/DD/YYYY]) or try_to_date(Date1 [MM/DD/YYYY]) the error says it doesn't recognize MM.
Any thoughts?
If you're trying to display the date using a specific format, you're converting to a varchar rather than a date:
select to_varchar(current_date, 'MM/DD/YYYY');
If you're trying to compare a column with a date to a formatted string in MM/DD/YYYY format then:
select current_date = try_to_date('08/04/2022', 'MM/DD/YYYY');
You should try to provide correct format to match 2022-06-17 literal:
SELECT TRY_TO_DATE(Date1, 'YYYY-MM-DD')
FROM tab_name;
Your column is already of type DATE. TO_DATE() and TRY_TO_DATE() convert non-date formats (string, integer) to a DATE type. They are not a means to format your DATE for presentation.
Date data type and presentation format are indepent.
You can alter your session to change the default display format of a date, but the underlying representation in the database remains the same.
alter session set DATE_INPUT_FORMAT='MM/DD/YYYY';
alter session set DATE_OUTPUT_FORMAT='MM/DD/YYYY';
select <col_name> from table; -- Now will show as MM/DD/YYYY for date columns

How can i make a field with a datatype Varchar be a date in Snowflake?

I am using Snowflake and I have a field with a datatype VARCHAR and the values in that field are for example: 2/10/17, 9/7/18, 1/23/19.
I trying to convert that field into a Date using this script:
select To_Date(Field_name) from CONCUR
However i get this message:
Date '' is not recognized
You need a format specification as a second argument to to_date() (otherwise it defaults to session parameter DATE_INPUT_FORMAT, which is probably not what you want):
to_date(field_name, 'MM/DD/YYYY')
You may also want to use try_to_date(), that returns null when the conversion fails rather than raising an error as to_date() does.
To_date should be used with the format like below
select to_date('02/14/2014', 'MM/DD/YYYY'), date('02/14/2014', 'MM/DD/YYYY');
https://docs.snowflake.com/en/sql-reference/functions/to_date.html
Thanks
Palash

How change format mask in XMLELEMENT function from Oracle DB 11g CORE 11.2.0.3.0?

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

Is there a way to convert a VARCHAR to a DATE in AWS Redshift?

Attempting to convert a variable character data type field that is time related (eg '2015-Q1') to a timestamp (or time) data type field in order to compare with a different field.
Need to be able to use dateadd() on the field.
Use the TO_DATE function: https://docs.aws.amazon.com/redshift/latest/dg/r_TO_DATE_function.html
https://docs.aws.amazon.com/redshift/latest/dg/r_FORMAT_strings.html
Along with TO_TIMESTAMP
https://docs.aws.amazon.com/redshift/latest/dg/r_TO_TIMESTAMP.html
example:
select to_timestamp(to_date('2015-Q1', 'YYYY-MM-DD'), 'HH24:MI:SS');

creating table in Oracle with Date

I want to create a table in Oracle 10g and I want to specify the date format for my date column. If I use the below syntax:
create table datetest(
........
startdate date);
Then the date column will accept the date format DD-MON-YY which I dont want.
I want the syntax for my date column to be MM-DD-YYYY
Please let me know how to proceed with this.
Regards,
A DATE has no inherent format. It is not simply a string that happens to represent a date. Oracle has its own internal format for storing date values.
Formats come into play when actual date values need to be converted into strings or vice versa, which of course happens a lot since interactively we write dates out as strings.
The default date format for your database is determined by the settings NLS_DATE_FORMAT, which you probably have set to DD-MON-YYYY (which I believe is the default setting for American English locales). You can change this at the database level or for a single session for convenience, but in general it is safer programming practice to be explicit so that you don't get errors or, worse, wrong results if your code is run in a different environment.
The simplest way to specify a date value unambiguously is a date literal, which is the word 'date' followed by a string representing the date in YYYY-MM-DD format, e.g. date '2012-11-13'. The Oracle parser directly translates this into the corresponding internal date value.
If you want to use a different format, then I recommend explicitly using TO_CHAR/TO_DATE with your desired format model in your code. Examples:
INSERT INTO my_table (my_date) VALUES ( TO_DATE( '11-13-2012', 'MM-DD-YYYY' ) );
SELECT TO_CHAR( my_date, 'MM-DD-YYYY' ) FROM my_table;
dates rdo not have a format like you're suggesting. they are stored internally as a 7 byte number. to format the date when selecting, please use TO_CHAR(yourdatefield, 'format')
where formats are all shown here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
eg to_char(startdate, 'mm-dd-yyyy')