Converting Varchar to Timestamp in Snowflake - snowflake-cloud-data-platform

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

Related

Converting Varchar Date into timestamp in Snowflake

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

Parse date to different format in snowflake

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

Truncate a date in Postgres (latest version)

I have different timestamps within my posgres db. With timestamp I mean unix time since 1970 saved as bigint.
Now I need to get the timestamp since 1970 striped down to days. For instance: 1469059200000 (Today: 2:00 am should become 1469052000000 (Today: 00:00 am)
I've tried this in pgadmin
Select date_trunc('day', rp.timestamp) from rollup_days as rp
but just got this error. ERROR: function date_trunc(unknown, bigint) does not exist
A bigint is not "a timestamp", so you must convert the number to a date before you can apply date_trunc() on it:
Select date_trunc('day', to_timestamp(rp.timestamp))
from rollup_days as rp;
To convert the timestamp back to a bigint, use extract()
Select extract(epoch from date_trunc('day', to_timestamp(rp.timestamp)))
from rollup_days as rp;

converting start date to datetime as beginning of the day and end date to end of the day

Store PROCEDURE
--para--
#StartingDate DateTime = NULL,
#EndingDate DateTime = NULL
--condition --
dbo.callentry.CallDateTime BETWEEN ISNULL(#StartingDate,
dbo.callentry.CallDateTime) and ISNULL(#EndingDate,dbo.callentry.CallDateTime)
Question :
when i pass date '2012-09-17' from date picker as para #StartingDate, and the same as ending date . it is comparing 2012-09-17 00:00:00.000 to 2012-09-17 00:00:00.000 - will return no records
what i want is records in whole day 2012-09-17
Why not just use #StartingDate-1 then?
Or even DATEADD(d,-1,#StartingDate)
Or #EndDate + 1
Or even DATEADD(d,1,#EndDate)
DATEADD (Transact-SQL)
Returns a specified date with the specified number interval (signed
integer) added to a specified datepart of that date.
Try this:
dbo.callentry.CallDateTime >=ISNULL(#StartingDate,
dbo.callentry.CallDateTime) and dbo.callentry.CallDateTime <=ISNULL(#EndingDate,dbo.callentry.CallDateTime)
Also make sure dbo.callentry.CallDateTime this column datatype is also datetime
OR
Also reading from your question. I think when strt and end date are same you just need all the records for that day. if start and end date are same why cant you just use like below:
convert(date,dbo.callentry.CallDateTime) =convert(date,ISNULL(#StartingDate,
dbo.callentry.CallDateTime))
In case of sql server 2008 if below just convert both the sides to just date format annd compare

database field with type timestamp is not updating

Duplicate of database timestamp value is not
updating
I have a variable which contains the date and time in 0000-00-00 00:00:00 format but when i put this variable in database field with type timestamp .. it is still 0000-00-00 00:00:00
Make sure that you are using the right format of the datetime. Mysql default datetime format is YYYY-MM-DD H:i:s. But, it seems that you are trying to insert the datetime value with the format of (YYYY:MM-DD H:i:s). It does not allow to insert the datetime value, so the inserted value becomes 0000-00-00 00:00:00.
Try by changing your calendar format to (YYYY-MM-DD H:i:s). I assume that you are using datatime datatype for the Valid_till column. Click the below to know more about mysql data structure.
Mysql Date format

Resources