Converting from Date format to date format in oracle - sql

I have a date in a format like **23-APR-20** in oracle SQL and I wanna make it 23-04-2020 in date format also not string,how can I handle this please?

Please use below type conversion,
select to_date(to_char(date_column, 'DD-MON-YY'), 'DD-MM-YYYY') from table_name;

This is where you NLS_DATE_FORMAT works.
Try this:
ALTER SESSION SET NLS_DATE_FORMAT='DD-MM-YYYY';
Then all your dates will be in dd-mm-yyyy format.
Please note that oracle do not store dates in any format. It stores it in its own binary structure. NLS setting just make the format visible to your client.

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

Converting records from 'YYYY-MM-DD' format to MM/DD/YYYY in PL/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;

Convert DateTime into desired format in SQL Server

In my project we are migrating from an Oracle database to SQL Server.
In Oracle, to get the required date format, we will use the to_char function - it will return the desired format as we mentioned in the second param:
TO_CHAR(TXNDT,'dd/MM/yyyy hh24:mi:ss')
In SQL Server, the same thing can be achieved like below.
convert(varchar(255), TXNDT, 131)
My problem is: I want to get the date format dd-MM-yyyy hh24:mi:ss
I can do this in Oracle:
TO_CHAR(TXNDT,'dd-MM-yyyy hh24:mi:ss')
But how can I achieve the samething in SQL Server?
Any help will be greatly appreciated!
Use format(). I think this does what you want:
format(TXNDT,'dd-MM-yyyy HH:mm:ss')
If you need this as a varchar():
convert(varchar(255), format(TXNDT,'dd-MM-yyyy HH:mm:ss')
CONVERT(Transact-SQL) has all of the style codes for CONVERT. Note, also, that you need to CONVERT to a varchar, not a datetime. datetime has a fixed display format of yyyy-MM-dd HH:mm:ss.sss.
So, for you, it would be CONVERT(varchar(20),TXNDT,120). notice the use of varchar, rather than datetime.

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

Changing SQL VARCHAR2 date to another date format

I have a date (stored as a VARCHAR2) in a database with the format:
20090123_163842.865
yyyyMMdd_hhmmss.ttt
and I want to make a SQL sentence to obtain:
23/01/2009 16:38:42,865
dd/MM/yyyy hh:mm:ss,ttt
MY objective is to add it manually (I know that data can be exported from database, and imported into Excel, but I want to do it manually) to Excel as a recognizable Date.
How should my SQL sentence be?
I have tried to to it by:
select TO_TIMESTAMP(my_time_utc, 'YYYYMMDD_HH24MISS.FF3') from myTable
but I am only able to obtain:
2009-01-23 16:38:42.865
Thanks
It never ceases to amaze me how many people confuse these operations.
First you need to convert the varchar 'fake date' to a real date: use to_date for this.
Then you need to convert the date to a varchar for presentation: use to_char for this.
select to_char(to_date(column, 'yyyyMMdd_hhmmss.ttt'), 'dd/MM/yyyy hh:mm:ss,ttt')
from your_table;
should do what you want.
When oracle retrieve a date field from database and show it to you a cast implicit conversion is made. The format pattern for this conversion is set in oracle configuration. Quoting oracle doc:
The default date format for an Oracle date value is derived from the
NLS_DATE_FORMAT and NLS_DATE_LANGUAGE initialization parameters
If you perform query from Excel, your actual query is enougth because excel know date format and is able to read from Oracle with out problems.
If you do a copy-paste from your screen results to excel, then you should cast back date to varchar with your desired format or, of course, change oracle configuration to match your locales.