Convert VARCHAR to DATE in Snowflake - sql

I need to convert my varchar to date.
My varchar looks like this:
event_date
2020-09-20
2021-07-21
2021-01-20
2020-10-08
Here is my attempted solution:
SELECT
TO_DATE(event_date,'YYYY-MM-DD')
FROM
database.schema.table
Here is the error I get:
Can't parse ' ' as date with format 'yyyy-mm-dd'
Can anybody help me out? Thanks!

You can use try_to_date(event_date,'YYYY-MM-DD'), it will return NULL (will not fail) if event_date can not be parsed, you can filter and check what exactly it contains:
SELECT
event_date as varchar_date,
TRY_TO_DATE(event_date,'YYYY-MM-DD') as parsed_date
FROM
database.schema.table
WHERE TRY_TO_DATE(event_date,'YYYY-MM-DD') IS NULL --filter and check
After checking which formats do you have, you can parse different formats using coalesce:
coalesce(TRY_TO_DATE(event_date,'YYYY-MM-DD'),
TRY_TO_DATE(event_date,'YYYYMMDD')
)

Answer was the trim() function for the extra spaces around my varchar. Thanks everyone!

Related

Converting string to date in sql

I need to convert 2014-11-18T14:08:43+00:00 which is in varchar in my sql developer to date format in order to filter a few entries.
I tried
to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')
but it gives an error
ORA-01830: date format picture ends before converting entire input
string.
Kindly help..
Just in case you didn't mean to put up sql server but instead you need to use oracle (seeing as you are using to_date and you are getting an ora exception)
I added a quick datetime conversion for date and timestamp (no milliseconds) for your date format:
SELECT to_Date(concat
(substr
(myvar,0,10),
concat(' ',
substr(myvar,12,8)
)
),'YYYY-MM-DD HH24:mi:ss') AS mydate
FROM mytable
Fiddle
declare #varDate as nvarchar(50) = '2014-11-18T14:08:43+00:00'
select CAST(substring(#varDate,0,CHARINDEX('T',#varDate)) as date)
Either you can use it like this
declare #Date as nvarchar(100) = '2014-11-18T14:08:43+00:00'
SELECT CONVERT(DATE,#Date) AS Date
OR go for this answer which is accepted in this question
ORA-01830: date format picture ends before converting entire input string / Select sum where date query
ORA-01830: date format picture ends before converting entire input string.
to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')
2014-11-18T14:08:43+00:00 is TIMESTAMP and not DATE.
First of all, you should never ever store DATE/TIMSTAMP as string. It is a database design flaw.
Anyway, you could convert it to TIMESTAMP WITH TIMEZONE.
For example,
SQL> SELECT to_timestamp_tz('2014-11-18T14:08:43+00:00',
2 'YYYY-MM-DD"T"HH24:MI:SS.FFTZH:TZM')
3 AS tm_stamp
4 FROM dual;
TM_STAMP
----------------------------------------------------------------
18-NOV-14 02.08.43.000000000 PM +00:00
SQL>
You could try this;
Select CAST ('2014-11-18T14:08:43+00:00' as date)
The assumption is you are in SQL Server 2012

In SQL, how do I convert a date field into a string if null?

I would like to write an SQL query in SQL Server 2008 R2 that converts a date to a string when it is NULL. For example...
Date ShipmentRef RecieptNo
2009-01-01 03:12:11.596 DS298-YYY 18060
FM298-YYY 95464
2010-11-11 08:33:55.974 IL298-YYY 56703
2003-08-01 07:00:44.846 UI835-XYX 40264
US655-YXY 34643
2004-03-07 12:46:33.352 WE242-XXX 83755
The above data is just a sample table of what my current data looks like. When I run the SELECT query, I want it to return the data as follows:
Date ShipmentRef RecieptNo
2009-01-01 03:12:11.596 DS298-YYY 18060
InsertRandomStringHere FM298-YYY 95464
2010-11-11 08:33:55.974 IL298-YYY 56703
2003-08-01 07:00:44.846 UI835-XYX 40264
InsertRandomStringHere US655-YXY 34643
2004-03-07 12:46:33.352 WE242-XXX 83755
I'm not sure which would be better, CASE or CONVERT. Any help you give me will be very much appreciated.
Assuming SQL-Server:
SELECT ISNULL(CONVERT(nVarChar(30), Date, 121), 'InsertRandomStringHere')
DEMO
CAST and CONVERT (Transact-SQL)
COALESCE(datefield, 'InsertRandomStringHere')
(though as others point out, for some DBMS you may need to do additional typecasting operations).
It depends what exactly you want to achieve.
Each value in column has to be the same type (in MS-SQL at least), so all values in Date columns have to be VARCHAR type if you want ''random string'' in case of NULL date.
Then something like:
SELECT CAST(COALESCE(GETDATE(), 'InsertRandomStringHere') AS VARCHAR) AS DATE
UNION
SELECT CAST(COALESCE(NULL, 'InsertRandomStringHere') AS VARCHAR)
should work.
However it could make hard to read values from Date column in your app (if there is an app) on the other end of wire.
SELECT ISNULL(Date, 'InsertRandomStringHere') AS Date
FROM Table
Try this . This is w.r.to mysql
select if(Date <> '',Date,'xxxxxxx'),ShipmentRef,RecieptNo from table

Need help converting date in format 20120130 to Date data type oracle sql

Could you please help me in converting date from the format "20120101" to DATE format in Orcle Sql.
I looked at this link but it does not mention if the date format is custom..
EDIT: Is it possible to write any exclusion rule to the conversion function?
something like this "99999999" to "9999-12-31"? Is it possible?
SELECT to_date('20120101','YYYYMMDD') FROM dual;
to_date('20120101','YYYYMMDD')
should be fine...
You cans specify the format:
to_date('20120101', 'yyyymmdd')
If you have a string '20120101' that you want to convert into a date, assuming that the string contains a 4 digit year followed by a 2 digit month and a 2 digit day
to_date( '20120101', 'YYYYMMDD' )

SQL query to convert Date to another format

Thanks for your help. I am not able to make out the type/format of the "Value" in a Date column.I guess its in Julian Date format.
The Column is paid_month and the values are below.
200901
200902
So,please help in writing SQL query to convert the above values(Mostly in Julian Format) in the Date Column to normal date (MM/DD/YYYY) .
Thanks
Rohit
Hi,
I am sorry for missing in giving the whole information.
1)Its a Oracle Database.
2)The column given is Paid_Month with values 200901,200902
3)I am also confused that the above value gives month & year.Day isnt given if my guess is right.
4)If its not in Julian format ,then also please help me the SQL to get at least mm/yyyy
I am using a Oracle DB and running the query
THANKS i GOT THE ANSWER.
**Now,i have to do the reverse meaning converting a date 01/09/2010 to a String which has 6 digits.
Pls help with syntax-
select to_char(01/01/2010,**
It looks like YYYYMM - depending on your database variant, try STR_TO_DATE(paid_month, 'YYYYMM'), then format that.
Note: MM/DD/YYYY is not "normal" format - only Americans use it. The rest of the world uses DD/MM/YYYY
For MySQL check
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y')
For MySQL, you would use the STR_TO_DATE function, see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE(paid_month,'%Y%m');
Sounds like the column contains some normal dates and some YYYYMM dates. If the goal is to update the entire column, you can attempt to isolate the YYYYMM dates and update only those. Something like:
UPDATE YourTable
SET paid_month = DATE_FORMAT(STR_TO_DATE(paid_month, '%Y%m'), '%m/%d/%Y')
WHERE LENGTH(paid_month) = 6
SELECT (paid_month % 100) + "/01/" + (paid_month/100) AS paid_day
FROM tbl;
I'm not sure about how oracle concatenates strings. Often, you see || in SQL:
SELECT foo || bar FROM ...
or functions:
SELECT cat (foo, bar) FROM ...

Query to convert from datetime to date mysql

I am trying to get the date portion of a datetime field. I know I can get it with date_format, but that returns a string or "varchar" field. How can I convert the result to date and not as varchar?
This is my query returning the varchar:
(Select Date_Format(orders.date_purchased,'%m/%d/%Y')) As Date
I tried several combinations from this question, but could not make it to work:
mysql query - format date on output?
Any help is appreciated.
Try to cast it as a DATE
SELECT CAST(orders.date_purchased AS DATE) AS DATE_PURCHASED
Use the DATE function:
SELECT DATE(orders.date_purchased) AS date
Either Cybernate or OMG Ponies solution will work. The fundamental problem is that the DATE_FORMAT() function returns a string, not a date. When you wrote
(Select Date_Format(orders.date_purchased,'%m/%d/%Y')) As Date
I think you were essentially asking MySQL to try to format the values in date_purchased according to that format string, and instead of calling that column date_purchased, call it "Date". But that column would no longer contain a date, it would contain a string. (Because Date_Format() returns a string, not a date.)
I don't think that's what you wanted to do, but that's what you were doing.
Don't confuse how a value looks with what the value is.
I see the many types of uses, but I find this layout more useful as a reference tool:
SELECT DATE_FORMAT('2004-01-20' ,'%Y-%m-01');
syntax of date_format:
SELECT date_format(date_born, '%m/%d/%Y' ) as my_date FROM date_tbl
'%W %D %M %Y %T' -> Wednesday 5th May 2004 23:56:25
'%a %b %e %Y %H:%i' -> Wed May 5 2004 23:56
'%m/%d/%Y %T' -> 05/05/2004 23:56:25
'%d/%m/%Y' -> 05/05/2004
'%m-%d-%y' -> 04-08-13