A varchar column in a table contains dates in '05/13/2019 20:48:13 PM' format. It can contain either a single date or many dates concatenated with pipe |. I use this query to get the very first one and try to convert it to timestamp.
Select (CASE WHEN charindex('AM', COMPL_DATE)>=1 OR charindex('PM', COMPL_DATE) >= 1
THEN TO_TIMESTAMP ( SPLIT_PART( COMPL_DATE, '|' , 1), 'MM/DD/YYYY hh24:mi:ss AM')
ELSE TO_TIMESTAMP ( SPLIT_PART( COMPL_DATE, '|' , 1), 'MM/DD/YYYY hh:mi:ss')
END
) from some_table.
I get this error
Can't parse '03/23/2019 20:56:22 PM' as timestamp with format 'MM/DD/YYYY hh24:mi:ss AM'.
It is not that it fails for all the rows. There are some rows for which it fails.
However when I use this data to run separately, like this -
with tab as ( select '03/23/2019 20:56:22 PM' COMPL_DATE from dual )
SELECT
(CASE WHEN charindex('AM',COMPL_DATE)>=1 OR charindex('PM',COMPL_DATE) >= 1
THEN TO_TIMESTAMP ( SPLIT_PART( COMPL_DATE, '|' , 1), 'MM/DD/YYYY hh:mi:ss AM')
ELSE TO_TIMESTAMP ( SPLIT_PART( COMPL_DATE, '|' , 1), 'MM/DD/YYYY hh24:mi:ss')
END
) result
FROM tab ;
It works fine.
Any help is appreciated.
Using the combination of the 24 hour clock and AM/PM is not allowed:
'MM/DD/YYYY hh24:mi:ss AM'
HH24 - Two digits for hour (00 through 23); am/pm NOT allowed.
Try: 'MM/DD/YYYY hh12:mi:ss AM'
It could be related with the extra space characters on your date string. I noticed that the web interface does not show the extra space characters in error messages so I'll contact with the development team to fix the space characters of the results in web interface.
Here's a sample query:
select TO_TIMESTAMP ( '03/23/2019 20:56:22 PM', 'MM/DD/YYYY hh24:mi:ss AM');
The error message which you will see in the web interface:
Can't parse '03/23/2019 20:56:22 PM' as timestamp with format 'MM/DD/YYYY hh24:mi:ss AM'
If you run the same query on Snowflake CLI, you can see the error clearly:
PUBLIC>select TO_TIMESTAMP ( '03/23/2019 20:56:22 PM', 'MM/DD/YYYY hh24:mi:ss AM');
100096 (22007): Can't parse '03/23/2019 20:56:22 PM' as timestamp with format 'MM/DD/YYYY hh24:mi:ss AM'
Could you verify the dates in your column?
Related
Background:
I have the below table of data where I'm trying to concat the order_date and transaction_time columns to create a final timestamp column
Problem:
There is a PST/PDT string in the transaction_time column. I am trying to convert my final timestamp column(VARCHAR) into a UTC timestamp
My attempted solution that didn't work:
select
transaction_date
, to_date(transaction_date, 'mon dd, yyyy') as order_date
, transaction_time
, concat(transaction_date, ' ', transaction_time) as timestamp
-- , to_timestamp_tz(concat(transaction_date, ' ', transaction_time), 'mon dd, yyyy hh:mm:ss am pdt') as final_timestamp
from raw_db.schema_name.table_name
Please help?? Thank you!!
So PST and PDT are not valid iana timezone's which is what is expected by the Timestamp Formats, so you cannot use the inbuilt functions to handle that, but you can work around it.
SELECT time
,try_to_timestamp(time, 'YYYY-MM-DD HH12:MI:SS AM PDT') as pdt_time
,try_to_timestamp(time, 'YYYY-MM-DD HH12:MI:SS AM PST') as pst_time
,dateadd('hour',7, pdt_time) as pdt_as_utc_time
,dateadd('hour',8, pst_time) as pst_as_utc_time
,coalesce(pdt_as_utc_time, pst_as_utc_time) as utc_time1
,iff(substr(time, -3) = 'PDT', pdt_as_utc_time, pst_as_utc_time ) as utc_time2
FROM VALUES
('2020-10-28 7:25:44 AM PDT'),
-- insert more rows here...
('2020-11-06 6:35:18 PM PST')
v(time);
shows two ways to get a unified UTC time from the two.
which could be shortened to:
SELECT time
,coalesce(dateadd('hour',7, try_to_timestamp(time, 'YYYY-MM-DD HH12:MI:SS AM PDT')), dateadd('hour',8, try_to_timestamp(time, 'YYYY-MM-DD HH12:MI:SS AM PST'))) as utc_time1
,iff(substr(time, -3) = 'PDT',dateadd('hour',7, try_to_timestamp(time, 'YYYY-MM-DD HH12:MI:SS AM PDT')), dateadd('hour',8, try_to_timestamp(time, 'YYYY-MM-DD HH12:MI:SS AM PST')) ) as utc_time2
FROM VALUES
('2020-10-28 7:25:44 AM PDT'),
('2020-11-06 6:35:18 PM PST')
v(time);
which gives:
TIME UTC_TIME1 UTC_TIME2
2020-10-28 7:25:44 AM PDT 2020-10-28 14:25:44 2020-10-28 14:25:44
2020-11-06 6:35:18 PM PST 2020-11-07 02:35:18 2020-11-07 02:35:18
As Per my comment if you have more TIMEZONE you need to support, lets say New Zealand's two timeszones ;-) then a CASE would be more suitable
SELECT time
,substr(time, -4) as tz_str -- longer and NZxT is longer
,CASE
WHEN tz_str = ' PDT' THEN dateadd('hour',7, try_to_timestamp_ntz(time, 'YYYY-MM-DD HH12:MI:SS AM PDT'))
WHEN tz_str = ' PST' THEN dateadd('hour',8, try_to_timestamp_ntz(time, 'YYYY-MM-DD HH12:MI:SS AM PST'))
WHEN tz_str = 'NZDT' THEN dateadd('hour',-13, try_to_timestamp_ntz(time, 'YYYY-MM-DD HH12:MI:SS AM NZDT'))
WHEN tz_str = 'NZST' THEN dateadd('hour',-12, try_to_timestamp_ntz(time, 'YYYY-MM-DD HH12:MI:SS AM NZST'))
END as utc_time
FROM VALUES
('2020-10-28 7:25:44 AM PDT'),
('2020-11-06 6:35:18 PM PST'),
('2021-04-23 2:45:44 PM NZST'),
('2021-01-23 2:45:44 PM NZDT')
v(time);
OR you could use a regex to match up to the AM/PM part of the date time like in this SO Question/Answer, and have just one try_to_timestamp_ntz and just use the CASE to correct based of the suffix.
An import has rendered a date [YEAR] as varchar and I need to convert it to a proper date.
The conversion fails I believe, because SQL cannot recognise d/mm/yyyy equal to dd/mm/yyyy.
How do I get SQL to convert / cast this into a date or timestamp?
SELECT [YEAR], LEN([YEAR]) AS ColumnLength
FROM [IDW_Dev]
YEAR ColumnLength
10/07/2020 10
8/07/2020 9
14/08/2020 10
I tried this, and this should normally work:
, CAST([YEAR] AS DATE) AS AUDIT_DATE
Conversion failed when converting date and/or time from character string.
Here's a demo using the convert function, just because I felt like testing if it worked with all variations of DD and MM in the string.
Convert has most layouts built in, 103 is for British/French format with slashes.
SELECT SampleDate
, CONVERT(Datetime2, SampleDate, 103) AS ConvertedDate
FROM (
VALUES ('1/2/2020')
, ('1/02/2020')
, ('01/2/2020')
, ('01/02/2020')
)Samples(SampleDate)
I tried some conversions but seems like it's not working. My datatype is a decimal:
date_column
-----------
20140619
20160527
20150601
20131127
20170217
20171204
20160519
My SQL statement:
SELECT
CONVERT(DATE, CONVERT(VARCHAR(10), date_column), 111) AS mydate
FROM
my_table
Results
mydate
-------------
2014-06-19
2016-05-27
2015-06-01
2013-11-27
2017-02-17
2017-12-04
2016-05-19
You currently do this:
decimal -> varchar -> date
which is good, but you are still missing one more step:
decimal -> varchar -> date -> varchar (new format)
Thus, you need:
select CONVERT(varchar(10), CONVERT(date, CONVERT(varchar(8), date_column)), 111) as mydate
from my_table
(What you should actually do is to store the date as a DATE in the first place, but I am aware that this is not always an option for legacy databases.)
You can try either of the below code, based on your preference.
POST SQL SERVER 2012
declare #testDate DATE;
SET #testDate = '20140619'
SELECT FORMAT ( #testDate, 'd', 'zh-cn' ), FORMAT( #testDate, 'yyyy/MM/dd', 'en-US' )
Prior to SQL SERVER 2012
declare #testDate DATE;
SET #testDate = '20140619'
SELECT REPLACE(CAST(CONVERT(date,'20140619',111) AS CHAR(10),'-','/')
SELECT convert(char(10),#testDate,111)
Let me share some of the answers i got:
select format(cast(date_column as date),'yyyy/MM/dd') as mydate
from my_table;
select convert(date,convert(varchar(10), date_column ), 111 ) as mydate
from my_table;
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/53a51068-b802-43e7-baa3-7510d56c5b63/how-to-convert-date-formart-yyyymmdd-to-yyyymmdd-in-sql-server-2008?forum=transactsql
Thank you
I have a view that I'm querying against, this view is pulling the data from 3 different tables. The curious thing is that when I execute this query:
select * from CPOB_MONITORING_DASHBOARD where FISCAL_MONTH_START_DT =
TO_TIMESTAMP('2016-01-01 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FF')
it works as expected.. it returns all of the rows with a date like 2016-01-01
But when I execute a query of the same type against another column:
select * from CPOB_MONITORING_DASHBOARD where VOYAGE_STRT_DT =
TO_TIMESTAMP('2014-04-07 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FF')
it returns no rows.. even if there are many dates with the value '2014-04-07'
The columns are from different tables and of type 'Date'. I can only use TO_TIMESTAMP() and not TO_DATE(). Any ideas of where do I need to look to solve this?
It looks like all your FISCAL_MONTH_START_DT values are at midnight (which seems reasonable from the column name) but your VOYAGE_STRT_DT are not - they have other times of day, so they aren't matching your query, which is only looking for exactly midnight.
select * from CPOB_MONITORING_DASHBOARD
where VOYAGE_STRT_DT >= TO_TIMESTAMP('2014-04-07 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FF')
and VOYAGE_STRT_DT < TO_TIMESTAMP('2014-04-08 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FF')
or more simply:
select * from CPOB_MONITORING_DASHBOARD
where VOYAGE_STRT_DT >= TIMESTAMP '2014-04-07 00:00:00'
and VOYAGE_STRT_DT < TIMESTAMP '2014-04-08 00:00:00'
or even more simply:
select * from CPOB_MONITORING_DASHBOARD
where VOYAGE_STRT_DT >= DATE '2014-04-07'
and VOYAGE_STRT_DT < DATE '2014-04-08'
though if you can't use to_date() for some reason (!?) you may not be able to use date or timestamp literals either.
You may be seeing just the date part when you query the table in other ways, which is down to your client and NLS settings. To check the times you can change your settings or explicitly format the values:
select to_char(VOYAGE_STRT_DT, 'SYYYY-MM-DD HH24:MI:SS') as VOYAGE_STRT_DT
from CPOB_MONITORING_DASHBOARD
where VOYAGE_STRT_DT >= DATE '2014-04-07'
and VOYAGE_STRT_DT < DATE '2014-04-08'
I have date of birth stored in SQL server database as in this 4/23/1988 6:30:00 PM format. This column has nvarchar format. Now i want to convert this into IST which will add 5.30 hours to this date i.e output should be 4/24/1988 . It has null values also. how to do this ?
Convert() to datetime first before adding the 330 minutes
SELECT DATEADD(MINUTE, 330, CONVERT(DATETIME, date_of_birth, 101))
Try this
SELECT CAST(DATEADD(MINUTE,330,cast(date_of_birth AS datetime)) AS DATE)
Found solution....i skipped results which are null & are not valid.
Select DATEADD(MINUTE, 330, CONVERT(DATETIME, um_date_of_birth, 101)) from user_master where um_date_of_birth is not null and isdate( um_date_of_birth) <> 0