Convert GMT to IST in snowflake - snowflake-cloud-data-platform

I am trying to convert GMT to IST in snowflakes. I converted but when I try to change DateTime to date then it is not working.
SELECT '2020-02-29 23:59:57' AS Date,
convert_timezone('UTC', '2020-02-29 23:59:57') IST_datetime,
cast(convert_timezone('UTC', '2020-02-29 23:59:57') AS date) IST_date
DATE IST_DATETIME IST_DATE
2020-02-29 23:59:57 2020-03-01 05:29:57 2020-02-29
Problem is highlighted in red rectangle

Instead of date, use datetime:
SELECT '2020-02-29 23:59:57' AS Date,
convert_timezone('GMT','Asia/Kolkata', '2020-02-29 23:59:57') IST_datetime,
cast(convert_timezone('GMT','Asia/Kolkata', '2020-02-29 23:59:57') AS datetime) IST_date;
By the way, I also added source and target timezone (GMT/Kolkata) for reliable conversion (which will be not depend on your session timezone).

Related

Converting Varchar to Timestamp in Snowflake

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

Get correct date from timestamp at a timezone

I have a (Azure) SQL Server database with timestamps in UTC. I want to get the date at a specific timezone.
The following shows the issue
DECLARE #TS DateTime2='2020-02-08 23:00:00'
SELECT CAST((#TS at time zone 'W. Europe Standard Time') as date) as StartDate, #TS as StartTimeStampUTC, #TS at time zone 'W. Europe Standard Time' as StartTimeStampLocalTime
StartTimeStampUTC : 2020-02-08 23:00:00.0000000
StartTimeStampLocalTime : 2020-02-08 23:00:00.0000000 +01:00
StartDate : 2020-02-08
I would have expected the StartDate value to be 2020-02-09, as local time is 2020-02-08 23:00 + 01:00 = 2020-02-09 00:00
How can I get the correct date?
You can follow the below-mentioned process in SQL Server.
1. Get UTC time using - GETUTCDATE()
2. Get difference between your DateTime and UTC DateTime using below link -
https://dzone.com/articles/dates-and-times-in-sql-server-at-time-zone
3. Add that difference in UTC DateTime
Please find below script -
SELECT GETDATE() AS 'Local Timezone',
GETUTCDATE() AS 'Utc_Timezone',
(DATEADD(HOUR,5,DATEADD(MINUTE,30,GETUTCDATE()))) as'Local Timezone based on UTC'
I have added 5:30 Hrs based on my local timeZone.
This seems to do the trick :
SELECT CAST(dateadd(hour,DATEdiff(hour,#TS AT TIME ZONE 'W. Europe Standard Time',#TS ),#TS) as date) as StartDate
answer is now 2020-02-09 as expected

Convert HH:MM TT to Time and append onto date

I have a column that holds a string time (i.e. 8:00 AM) and I need to convert that to an actual Time and be able to append it onto a DateTime field (which is in DateTime format, not string).
Example:
Date field = 2019-06-25 00:00:00.000
Time field = 8:00 AM
Desired result: 2019-06-25 08:00:00.000
Does anyone know how I can accomplish this in SQL?
I know some people still suggest doing it in code, but I'm writing a query that does a comparison between two DateTime fields (one of which has the integrated DateTime with the proper time and the other in which the date and time are separate)
Using DATETIME + CAST(timevariable AS DATETIME) will return your expected result:
DECLARE #Time AS VARCHAR (10) = '8:00 AM'
DECLARE #DateField AS DATETIME = '2019-06-25 00:00:00.000';
SELECT #DateField + CAST(#time AS DATETIME)
Demo on db<>fiddle
It is also work with 1:00 PM

Convert datetime of a timezone to GMT in sql server

I have a requirement of converting a record of time w.r.to timezone and compare it with current time.
For this, I want to convert datetime of a timezone to GMT timezone in SQL server.
i.e. (Jan 12 2015 11:30 A.M +5:30--->GMT standard time
Also is it possible to find out client's time zone from sql server?
If your datetime is stored as a datetimeoffset (or convertible to it) you can simply convert to a datetime2 (assuming you want to keep the precision of the datetimeoffset):
declare #dt datetimeoffset
select #dt = 'Jan 12 2015 11:30 AM +05:30'
select convert(datetime2, #dt, 1)
Which returns the time in UTC:
2015-01-12 06:00:00.0000000
This has nothing to do with the users' timezone, as it is simply doing the timezone to UTC calculation based on your provided offset.
SqlFiddle

How to add time to a date in Sybase?

I'd like to take the current date in Sybase and set the time to 13:30:00.
Let say I do
select getdate()
12/5/2014 4:06:24.670 PM
I want to return
12/5/2014 13:30:00 PM
How do I transform that?
I did not have time to test this but i would suggest you convert the date part of getdate() into mm/dd/YYYY and then add "13:30:00 PM" before you convert it back from a string into a datetime object.
select convert(datetime,convert(varchar, getdate(), 101) + "13:30:00 PM")

Resources