Varchar2 to date in Oracle - sql

Can somebody help me with how to convert varchar column into Date data type?
My createDate column has VarChar2 data type in format example 20050923
I would like to convert this column into format example 2005/09/23
ALTER TABLE test ADD (new_create_date DATE);
UPDATE test SET new_create_date=TO_DATE(createDate,'MM/DD/YYYY');
When I run these sql I get format 23-aug-2005. Why?

Internally date is stored as a 7 byte value. Formatting is not something that is stored there. You get formatting when you pull the data out of the table.
Try this.
SELECT TO_CHAR(NEW_CREATE_DATE,'MM/DD/YYYY')
FROM TEST

You could do this:
update test
set new_create_date to_date(createDate, 'YYYYMMDD');
You just need to give to_date() the right format string.

This is because your database NLS_DATE_FORMAT must be in DD-MON-YYYY format, you can check that by using the below mentioned query.
SELECT *
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';
Also by definition to_date(x,format) converts a string i.e. x to a datetime, the format part is an optional part which indicates the format that is present for the string value x.
You can further use to_char(x,format) function in your case which converts the x datetime to a string as mentioned by the EvilTech.

Related

java dateformat with oracle format gives ORA-01722: invalid number

The date field in oracle DB is stored in '10-FEB-99' format. My calander function in UI accepts date in this format '02/30/2023'.
When i search date in UI, i have to query the DB with date field from UI to what exists in DB.
select * from case where TO_CHAR(p.OCCASION_FROM_DATE,'dd-MON-yyyy') = TO_CHAR('02/10/1999','MM/dd/yyyy');
When I execute this query I get below exception
**ORA-01722: invalid number
00000 - "invalid number"
Cause: The specified number was invalid.
Action: Specify a valid number.
Tried this
select * from case where TO_CHAR(p.OCCASION_FROM_DATE,'dd-MON-yyyy') = TO_CHAR('02/10/1999','MM/dd/yyyy');
The date field in oracle DB is stored in '10-FEB-99' format.
Is it? Perhaps. It depends on column datatype. What is it? If it is VARCHAR2, then yes - it might be stored that way, and that's wrong. You should always store dates into DATE datatype columns.
Let's presume it is a varchar2 column. In that case, you should match two values: first convert a string (02/10/1999) to a valid date value (using to_date with appropriate format model), and then back to string - again with format model that matches string stored into the column:
select * from case p
where p.OCCASION_FROM_DATE =
to_char(to_date('02/10/1999','MM/dd/yyyy'), 'dd-mon-rr', 'nls_date_language = english);
On the other hand, if values stored in table are in date datatype column (yes, that should be the case!), note that dates aren't stored in human-readable format. That's a 7-byte value representing century/year/month/day/hour/minute/second. The fact that you see it as 10-feb-99 is because your current NLS settings say so.
Query then gets somewhat simpler because you just have to convert string 02/10/1999 to date:
select * from case p
where p.occasion_from_date = to_date('02/10/1999', 'mm/dd/yyyy')

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

NULL values in a string to date conversion

I have a table with the following data:
logs.ip logs.fecha logs.metodo
66.249.93.79 19/Nov/2018:03:46:33 GET
All data columns are string and I want to convert logs.fecha into date with the following format: YYYY-MM-dd HH:mm:ss
I try the following query:
SELECT TO_DATE(from_unixtime(UNIX_TIMESTAMP(fecha, 'yyyy-MM-dd'))) FROM logs
Results of the query are NULL in all rows.
How can I make the conversion string to date for all rows? I know I must use ALTER TABLE but I don't know how to do it.
Thanks
The reason you get null is because the format of the input string is different from the input passed to unix_timestamp. The second argument to unix_timestamp should specify the string format of the first argument. In from_unixtime you can specify the output format desired. If nothing is specified, a valid input to from_unixtime returns an output in yyyy-MM-dd format.
The error can be fixed as below.
from_unixtime(unix_timestamp(fecha,'dd/MMM/yyyy:HH:mm:ss'),'yyyy-MM-dd HH:mm:ss')
You just have to tell Oracle the date format you are reading with TO_DATE.
Try:
SELECT TO_DATE(fecha,'DD/MON/YYYY:HH:MI:SS') FROM logs

How do I convert a string of format dd/mm/yy into datetime in SQL Server 2008?

I have table named PTBV with two columns: Dateptbv varchar(50) and [Column 1] varchar(50).
I'm trying to use the CONVERT() function to get actual datetime values from the string data stored in the Dateptbv column.
Here are examples of the data in that column:
"10/01/13"
"11/01/13"
"14/01/13"
"15/01/13"
"16/01/13"
"17/01/13"
"18/01/13"
"21/01/13"
"22/01/13"
"24/01/13"
"25/01/13"
"28/01/13"
"29/01/13"
"30/01/13"
"01/02/13"
"04/02/13"
Note that the quotation marks in this sample are part of the data and are stored in the column. Otherwise, data is stored in dd/mm/yy format.
Unfortunately, every date style I've tried has resulted in an error message. How can I convert this data into DateTime values?
If I understand your picture correctly, your column Dateptbv is VARCHAR(50) and stores the date as yy/mm/dd wrapped in " characters. First of all: Change this, if ever possible!. Try to store values in appropriately typed columns!
Try
SELECT CONVERT(DATE,SUBSTRING(Dateptbv,2,8)),3) --3 for dd/mm/yy
Read this link to find more details about the abilities of CONVERT.
Try the below script
SELECT CAST(SUBSTRING(Dateptbv,2,8) AS DATE)
FROM PTBV

want to convert string mm/dd/yy to date yyyy/mm/dd format in Sybase

I want to convert date which is string and like mm/dd/yy to date datatype in format yyyy/mm/dd.
I would double cast it, once to datetime, then back to varchar.
select convert(varchar,convert(datetime,'12/14/2012'),101)
This works on SQL Server, but I don't have a Sybase instance to test on.
Edit: Looks like you could also use this:
select convert(varchar,date('12/14/2012'),101)
You have to first add a date field to your table:
alter table *your_table* add *newdate* as date;
Then you can convert using:
update *your_table* set *newdate* = cast(*your_string_date* as date);