In Snowflake I have the date format in following
Thu Nov 12 00:00:00 UTC 2020
If I want to make it as follows:
MM-DD-YYYY HH24:MI:SS
Can this be achieved. I have tried to_date function doesn't seems to work
https://docs.snowflake.com/en/sql-reference/functions/to_date.html
any suggestions
I assume that this data is held in a column with a TIMESTAMP-type datatype? If that is the case then it is stored as a number and how it is displayed is controlled by *_OUTPUT_FORMAT parameters.
If you want to display a date/time in anything other than the default format then just use TO_CHAR with the appropriate format e.g.
select col3
, to_char(col3, 'MM-DD-YYYY HH24:MI:SS')
from casttb;
gives:
COL3 TO_CHAR(COL3, 'MM-DD-YYYY HH24:MI:SS')
2017-01-23 20:34:52.000 01-23-2017 20:34:52
2016-10-05 00:15:53.000 10-05-2016 00:15:53
2017-01-23 08:26:27.000 01-23-2017 08:26:27
2016-10-05 10:16:47.000 10-05-2016 10:16:47
If your date is held as text then first you would need to cast it to a date/time (TO_DATE) and then to text with the required format
Related
I have a date defined as varchar. The value of the date field is 19810801. When I am trying to convert into Timestamp using To_TimeSTAMP the value returned is 1970-08-20 06:18:21.000 +0000. I am not sure why snowflake is setting the year as 1970 and month as August.
Do you know what could be the issue here?
Here is my code -
Select to_timestamp_tz(EFFECTIVE_DATE) from Table1
The output returned is 1970-08-20 06:18:21.000 +0000 for the EffectiveDate 19810801
TO_TIMESTAMP is evaluating the value as epoch time:
https://www.epochconverter.com/
If you want to convert it as 1981-08-01, you can use this:
Select to_timestamp_tz('19810801','YYYYMMDD');
I have a date field as varchar type and need to convert it into Timestamp as i need to apply datediff function over it. Below is the varchar date format i have
20210201053414
This is 1st of Feb 2021 05:34:14 AM
I tried
SELECT TO_TIMESTAMP('20210201053414','yyyyMMddHHmmss')
but it says
Can't parse '20210201053414' as timestamp with format 'yyyyMMddHHmmss'
Thank You.
Try this one:
SELECT TO_TIMESTAMP('20210201053414','YYYYMMDDHH24MISS');
The issue is about MM - it should be MI. Please check the Format Specifiers:
https://docs.snowflake.com/en/user-guide/date-time-input-output.html#about-the-format-specifiers-in-this-section
We receive a csv file that has a column in this date format -- Wed Oct 14 08:00:00 CDT 2020, along with a column that has a count for each date/time
I am using an SSIS package to grab the file and import this data into a sql table, then I can format it the way I need to and then actually export the data in the format needed.
If there is a way to do this all within one SSIS package I am all ears but currently I am working on just getting the data into SQL and converted to the right format so that I can export it.
I need to get that file and convert that date format and split it up into two separate columns
One column will be just the date in this format 2020-10-14 00:00:00.000
One column will be just the time in this format 08:00:00.0000000
Updated to change the dates to match so it's not as confusing and also the error I am receiving when running the suggested code below.
Image of Error I'm recieving
Image of table with the data I am trying to convert
Image of table attributes
Screenshot of my screen when running a select * from the table I am pulling the data that I need converted
Screenshot of the error I receive when running the query by Aaron.
If this is the format it will always be in, and timezone is irrelevant, you can first try to convert it to a datetime, then you can extract the parts from that.
SET LANGUAGE us_english; -- important because not all languages understand "Oct"
;WITH src AS
(
SELECT dt = TRY_CONVERT(datetime, RIGHT(OpenedDateTime ,4)
+ SUBSTRING(OpenedDatetime, 4, 16))
--, other columns...
FROM [dbo].[VIRTUALROSTERIMPORT_Res_Import]
)
SELECT OpenedDateTime = CONVERT(datetime, CONVERT(date, dt)),
OnHour = CONVERT(time, dt)
--, other columns...
FROM src;
Results:
OpenedDateTime OnHour
-------------- ----------------
2020-10-14 08:00:00.0000000
If you need to shift from one timezone to another timezone, that's a different problem.
I was just showing the date formats, don't look so into the actual date examples I used. The time zone is irrelevant I just need the formats changed.
When I used The code Aaron suggested I got a conversion error: I'm assuming its because the columns are varchar in the table, but I cant get the dates to load as date formats bc SSIS keeps giving me truncated errors-- so I have to load it as varchar.
Below is the code I was running, I tweaked it to use the column and table names I am using.
SET LANGUAGE us_english; -- important because not all languages understand "Oct"
DECLARE #foo varchar(36) = 'Wed Oct 14 08:00:00 CDT 2020';
;WITH src(d) AS
(
SELECT TRY_CONVERT(datetime, RIGHT(#foo,4) + SUBSTRING(#foo, 4, 16))
)
SELECT OpenedDateTime = CONVERT(datetime, CONVERT(date, OpenedDateTime)),
onhour = CONVERT(time, OpenedDateTime)
FROM [dbo].[VIRTUALROSTERIMPORT_Res_Import];
I'm using this query
SELECT convert(nvarchar(MAX), GETDATE(), 22) AS Date
Result: 08/05/16 12:23:08 PM
But I want result like this 8/5/2016 12:23:08 PM
dd/mm/yyyy hh:mm:ss a
As of SQL Server 2012 the FORMAT function is available allowing you to specify the format of data types and is locale-aware so it will consider date formatting in relation to the session's language or optional culture parameter.
You can achieve your custom formatting like so: FORMAT(GETDATE(), 'd/M/yyyy hh:mm:ss tt')
Note your requested format dd/mm/yyyy hh:mm:ss a is incorrect as in the case of single digits you want to remove zero padding i.e. 10/8/2016 not 10/08/2016. That's why in the format string I use only d and M.
Also, pay attention to #GarethD comment about the cost on larger datasets.
You could use the FORMAT function in T-SQL : https://msdn.microsoft.com/en-us/library/hh213505(v=sql.120).aspx
Here is the code :
SELECT FORMAT(GETDATE(), 'd/M/yyyy hh:mm:ss tt')
I have a column which is of datetime type.
I want to separate date and time for which i used left() & right() enclosed by cast() or convert() like below :
Select BATCH,
LEFT( CAST(S_DATE AS VARCHAR(20)),12) AS ST_DATE,
RIGHT( CAST(S_DATE AS VARCHAR(20)),8) AS ST_TIME,
LEFT( CAST(E_DATE AS VARCHAR(20)),12) AS E_DATE,
RIGHT( CAST(E_DATE AS VARCHAR(20)),8) AS E_TIME
INTO CRYST2
From Z_BATCH;
this is my actual format for datetime column :-
2015-10-01 14:00:00.000
But the problem is that it is separating the date and time accurately but it isn't returning the output in string form, as the output itself turns to date & time format respectively in their respective columns.
Following is the output I am getting:-
1 Oct 1 2015 2:00PM Oct 1 2015 2:30PM
As you can clearly see the columns I separated after date-time to string conversion is still giving me output in some date-time format only.
please help regarding it.
this is my actual format for datetime column :
2015-10-01 14:00:00.000
No, it's not. A datetime value doesn't have a format at all, it only represents a point in time. It only gets a format when you convert it to a string.
You are not specifying any format at all when you convert the datetime values, so it will be using the default format (0). You can use the format that you saw the datetime values displayed as (121) to get the desired result:
Select BATCH,
LEFT( CAST(S_DATE AS VARCHAR(19),121),10) AS ST_DATE,
RIGHT( CAST(S_DATE AS VARCHAR(19),121),8) AS ST_TIME,
LEFT( CAST(E_DATE AS VARCHAR(19),121),10) AS E_DATE,
RIGHT( CAST(E_DATE AS VARCHAR(19),121),8) AS E_TIME
INTO CRYST2
From Z_BATCH;
Cast The date and the Time in the exact format like:
CAST(S_DATE AS Time) AS ST_TIME
CAST(S_DATE AS Date) AS ST_Date
will return:
14:00:00
2015-10-01
Use CONVERT with format 120 to get the desired result, it always return the format yyyy-MM-dd hh:mm:ss
CONVERT(varchar(20), S_DATE, 120)
You can try this, if you want to get results in string format:
select convert(varchar(11),cast(S_DATE as date),106)
select convert(varchar(11),cast(S_DATE as time),100)
If you try to get answers in date and time format, try this:
CAST(S_DATE AS Time) AS ST_TIME
CAST(S_DATE AS Date) AS ST_Date