Sql Server get current date time in Europe - sql

SELECT GETDATE()
The above query in SQL Server will return current date and time in USA because server is located in USA. How can I modify it to retrieve current date and time in Europe?

try this: Set #Offset =
0 for Greenwich Mean Time (GMT) - Great Britain,
1 for Central European Time (CET) -Netherlands, Germnany, France
etc,
2 for Eastern European Time (EET) Czechoslovakia, Hungary, etc.
Set #Offset = 0, 1, 2 ...
Declare #offset tinyInt = 0
Select GetUtcDate() + #offset/24.0

Use SwitchDateTimeOffset function with the datetimeoffset data type (requires SQL Server 2008 or higher).
Demo query:
CREATE TABLE TimeZone (ID int identity,
LocalTime datetimeoffset);
INSERT INTO TimeZone values ('2008-05-21 17:50:01.1234567 -08:00'),
('2008-05-21 18:50:01.1234567 -08:00');
SELECT * FROM TimeZone;
SELECT ID, SWITCHOFFSET(LocalTime,'+01:00') [time for new time zone]
FROM TimeZone;
DROP TABLE TimeZone;
In your scenario this becomes:
SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(),'+01:00')
As pointed out by p.campbell, you still have to decide which timezone represents 'Europe' for your purposes.

Related

SQL Server to SYnapse SQL syntax modifications

I have a query that works just fine in SQL Server but I would like some help with the required syntax modifications to make it work at Synapse SQL. Any help would be appreciated! The id's are unix timestamps. I want to count the id's of the previous day filtering by time range using the unix timestamps
SELECT COUNT(Id)
FROM [dbo].[name]
WHERE Id >= CONVERT(bigint, DATEDIFF(SECOND,'1970-01-01', CONVERT(date,dateadd(d, -1, GETDATE()))))*1000
and Id < CONVERT(bigint, DATEDIFF(SECOND,'1970-01-01', CONVERT(date,GETDATE())))*1000
When SQL servers and Azure Synapse are in different time zones, the results of Query will be varying. Azure Synapse will be in UTC time zone.
In order to convert current date value to local datetime value in azure synapse, User defined function is created as in below script.
Create FUNCTION [dbo].[CurrentLocalTime]( #TimeZoneName sysname )
RETURNS datetime
AS
BEGIN
RETURN DATEADD(hour,
TRY_CAST((SELECT REPLACE(current_utc_offset, N':', N'.')
FROM sys.time_zone_info
WHERE [name] = #TimeZoneName) AS decimal(18,2)),
SYSDATETIME());
END;
Instead of using getdate() of function, the above function is used in the query.
Query
SELECT COUNT(Id) FROM sample1
WHERE Id >= CONVERT(bigint, DATEDIFF(SECOND,'1970-01-01', CONVERT(date,dateadd(d, -1, dbo.CurrentLocalTime('AUS Eastern Standard Time' )))))*1000
and Id < CONVERT(bigint, DATEDIFF(SECOND,'1970-01-01', CONVERT(date,dbo.CurrentLocalTime('AUS Eastern Standard Time' ))))*1000
The time zone can be replaced in the query as per the requirement.
Results for Sample data:

Convert to my time zone - SQL Server

Could somebody give me a help?
I have a source data where it is in another server in another country, when I get it to my B.I Staging area, I want to convert it to my time zone, but my SQL Server versions is 2014.
I can't use AT TIME ZONE SQL Server function.
Is there any other way to do this without fixing a hard coded value like below?
The below result doesn't solve my problem, because its hard coded values:
SELECT SWITCHOFFSET (DATETIMEFIELD, '-03:00')
SELECT TODATETIMEOFFSET(DATETIMEFIELD,'-03:00')
Ps: Not forgetting about DAYLIGHT SAVING
Without any sample data, this is very much a stab in the dark, that at least tries to explain it. Note, that this isn't fool proof. For example, when the clocks go back then the times 01:00:00 - 01:59:59 will occur twice. These times will always be assumed to be before DST, regardless of if they really were.
Anyway, firstly I created a small table with the dates that DST occurred both in the UK (GMT: UTC + 0) and in Eastern America (EST: UTC - 5).
Then, I created a query as below, using that data.
USE Sandbox;
GO
--SAmple Daylight Savings Table
CREATE TABLE DaylightSaving (Timezone char(3),
ClocksChangeDate datetime,
Change smallint,
UTCDiff smallint);
--Dates for UK
INSERT INTO DaylightSaving
VALUES ('GMT','20170326 02:00:00',1,0),
('GMT','20171029 02:00:00',0,0),
('GMT','20180325 02:00:00',1,0),
('GMT','20181028 02:00:00',0,0),
('GMT','20190331 02:00:00',1,0),
('GMT','20191027 02:00:00',0,0);
--Dates for Eastern America
INSERT INTO DaylightSaving
VALUES ('EST','20170312',1,-5),
('EST','20171105',0,-5),
('EST','20180311',1,-5),
('EST','20181104',0,-5),
('EST','20190310',1,-5),
('EST','20191103',0,-5);
GO
--Made up sample data
CREATE TABLE SampleData (DateAndTime datetime, Timezone char(3));
INSERT INTO SampleData
VALUES ('20180123 12:28:00.000','EST'),
('20180523 19:58:00.000','EST'),
('20181101 07:19:00.000','EST'),
('20190330 20:50:00.000','EST'),
('20190330 21:05:00.000','EST');
GO
SELECT DateAndTime AS ESTTime,
DATEADD(HOUR, 5 + DT.Change - ST.Change, DateAndTime) AS GMTTime,
DATEDIFF(HOUR, DateAndTime, DATEADD(HOUR, DT.UTCDiff - ST.UTCDiff + DT.Change - ST.Change, DateAndTime)) AS TimeZoneDifference,
ST.ClocksChangeDate, DT.ClocksChangeDate
FROM SampleData SD
CROSS APPLY (SELECT TOP 1 *
FROM DaylightSaving sq
WHERE sq.ClocksChangeDate <= SD.DateAndTime
AND Timezone = SD.Timezone
ORDER BY sq.ClocksChangeDate DESC) ST
CROSS APPLY (SELECT TOP 1 *
FROM DaylightSaving sq
WHERE sq.ClocksChangeDate <= DATEADD(HOUR, sq.UTCDiff - ST.UTCDiff,SD.DateAndTime)
AND sq.Timezone = 'GMT' --your destination Timezone
ORDER BY sq.ClocksChangeDate DESC) DT;
GO
DROP TABLE DaylightSaving;
DROP TABLE SampleData;
Edit: Note that that the difference between the 2 zones was hard coded (I have a DATEADD with a hard coded value of 5), as this is an example. You will need to extrapolate this for your own system, but as we have no concept of what that looks like, you'll need to do that work.
Edit 2:Felt like adding the timezone diffs in. Only hard coded value now is the value of the local timezone ('GMT' in this case in the second CROSS APPLY)
As several people have said, datetime has no concept of timezones. If this is paramount to your data, then don't use datetime, use datetimeoffset. That stores the UTC +/- value as part of the value; making things like this trivial.

can datetime type of sql server hold time only?

I'm trying to store time in format
05:00 PM
in database. but when i insert the data it automatically stores date as well like
2016-07-20 17:00:00.000
All i want only
17:00
in database
You cant store Time only in a datetime field it stores default date,Use TIME data type if you are on 2008..
declare #t datetime
select #t=cast(getdate() as time)
print #t
---Jan 1 1900 9:17AM
declare #t time
select #t=cast(getdate() as time)
print #t
---09:17:48.3330000
for sql 2008
select cast(getdate() as time)
--09:17:25.4400000
for 2005
select convert(varchar(10),getdate(),108)
---09:17:33
your exact format
select cast(datepart(hour,getdate()) as varchar(10))+':'+ cast(datepart(minute,getdate()) as varchar(10))
---9:17
You also can use FORMAT to store only time (From SQl 2012)
select FORMAT(GETDATE(),'HH:MM')
--09:07
First, as suggested in the comments, you should use time type instead of datetime.
The format of time in SQL Server is : hh:mm:ss or hh:mm:ss.nnnnnnn. If you want time to be in AM PM format just use this :
CONVERT(varchar(15),CAST('17:00:00.000' AS TIME),100)

Can I query a DateTime in a SQL Server view using timezone?

I have a DateTime field in SQL Server 2008 R2. I have some views that query that field such as
SELECT *
FROM table1
WHERE CONVERT(DATE, TransactionDate) = 'April 10 2012'.
The servertime is set to texas time, and I want to query this data using EST. In other words
SELECT *
FROM Table1
WHERE TransactionDate = 'April 10 2012 Eastern Standard Time'
What is the best way to achieve this?
Give this a shot.
"DATETIMEOFFSET Datatype
Currently when saving the date and time in a column, it will not indicate what time zone that date and time belongs to. This can be especially important when you are dealing with data including several different countries with different time zones. The new datatype DATETIMEOFFSET defines a date that is combined with a time of a day that has time zone awareness and is based on a 24-hour clock. The following script illustrates the usage of the DATETIMEOFFSET datatype.
DECLARE #dt DATETIMEOFFSET(0)
SET #dt = '2007-10-29 22:50:55 -1:00'
DECLARE #dt1 DATETIMEOFFSET(0)
SET #dt1 = '2007-10-29 22:50:55 +5:00'
SELECT DATEDIFF(hh,#dt,#Dt1)
http://www.sql-server-performance.com/2007/datetime-2008/

sql timezone calculation

I have a table which stores the storecodes and their timezone. Now based on a given local date, I need to know if that date converted to stores local date was a in a weekend or not. Now I already know how to get the weekend part. I am struggling with the conversion. I am actually confused. My table has for example the following two values:
Store / TimeZone(Standard)
100 / 1 (This is frankfurt)
200 / 2 (This is tel aviv)
Our sql server is located in LA. I used the following code to get the UTC date:
DECLARE #LocalDate DATETIME, #UTCDate DATETIME
SET #LocalDate = GetDate()
-- convert local date to utc date
SET #UTCDate = DATEADD(Hour, DATEDIFF(Hour, GETUTCDATE(), GETDATE()), #LocalDate)
If I understood everything correct, I can now simply add the required hours to the #UTCDate to get the #UTCDate of that local timezone, correct?
For frankfurt it would be:
print DATEADD(HOUR, 1, #UTCDate)
Now this returns me the UTCDate for Frankfurt. How would I get the local date of Frankfurt though?
Edit: I am using Sql 2005.
Edit2: Complete example that is still confusing to me:
DECLARE #LocalDate DATETIME, #UTCDate DATETIME
SET #LocalDate = GetDate()
-- convert local date to utc date
SET #UTCDate = DATEADD(Hour, DATEDIFF(Hour, GETUTCDATE(), GetDate()), #LocalDate)
print GetDate()
print #UTCDate
print DATEADD(HOUR, 1, #UTCDate)
Output:
Jan 11 2010 12:32PM
Jan 11 2010 4:32AM
Jan 11 2010 5:32AM
Now does this mean, that if its 12:32PM in LA, then its 5:32AM in Franfurt? That seems be incorrect though. It should be 9:32PM in Franfurt.
You shouldn't start with local time. Start directly with UTC time:
DECLARE #UTCDate DATETIME
SET #UTCDate = GETUTCDATE();
Frankfurt is one hour ahead of UTC (UTC + 1) when summer time is not in effect so you add one hour:
print DATEADD(HOUR, 1, #UTCDate);
Remember that time zones are not on 60 minutes intervals, Mumbai is UTC + 5:30 and Nepal is UTC + 5:45. You must also account for daylight savings, and those change regularly. Argentine for instance opts to use the daylight on a year-by-year basis based on the ammount of water stored in its hydro power plants.
To sum up: always use UTC and leave the localisation of time to the client display and reporting.
If you have a UTCDate, it is the same for all timezones... I.e., when it's 1 am UTC in New York, it is also 1 am UTC in Frankfort. To get local time for any timnezone just add the offset (that's the value you have in your table) from the UTC DateTime... i.e., when it's 1 AM UTC, it's 2 am local in Frankfort. To remember whether to add or subtract, just remember that its always Earlier East.
Here is a SQL-Only implementation I recently put together you can use (Forums suggest that CLR is the only method since TSQL is needlessly complicated to achieve this in - not really afaik). I implemented via an inline function which avoids RBAR (You can profile and test this to confirm).
Performance is great even over old-school Distributed Partitioned Views too.
Make sure your indexing is good for it, even on the string manipulations on the DateTime Fields (To bypass the Year DatePart dependencies) I get the desired seeks. Some of the underlying partitioned tables are over 80GB in size.
Of course, you will need to add your timezone rows as you need and remember to keep the daylight savings start and end dates updated (They can change).
In both cases of timezone and daylight savings, offsets are in minutes so this works for all scenarios I have bumped into so far.
Lastly, the Daylight savings offset is always a positive number, note the function caters for this to suite the rule of thumb (Spring Forward, Fall Back)
If Not Exists (Select Name from sys.objects where name = 'tblTimeZones' and type = 'U')
Begin
Create Table tblTimeZones(
[ID] Int Identity (0,1) NOT NULL,
[UserID] Int NOT NULL,
[Description] NVarchar(128) NOT NULL,
[TZ_OffSet_Mins] Int NOT NULL,
[Use_DST] Bit NOT NULL,
[DST_AddOffSet] Int NOT NULL,
[DST_StartDate] DateTime NOT NULL Constraint DF_DST_StartDate Default ('1900-01-01 00:00:00.000'),
[DST_EndDate] DateTime NOT NULL Constraint DF_DST_EndDate Default ('1900-01-01 00:00:00.000'),
Constraint PK_tblTimeZones Primary Key NonClustered (ID),
Constraint UQ_tblTimeZones_Description Unique Clustered ([Description])
)
End
Go
If Exists (Select Name from sys.objects where name = 'fncV1_iCalcDateInTimeZone' and type = 'IF')
Begin
Drop Function fncV1_iCalcDateInTimeZone
End
Go
Create Function fncV1_iCalcDateInTimeZone
(
#UserID Int, #DateAndTime DateTime, #EntID Int
)
Returns Table
With SchemaBinding
As
Return (
Select TZDateAndTime =
DateAdd(
mi,
tz.TZ_OffSet_Mins +
-- Daylight Savings STARTS earlier in the Year than Ends (So, Northern Hemisphere), In Daylight Savings Time Period and Daylight Savings In Use
Case when
tz.Use_DST = 1
And SubString(Convert(Varchar(23),tz.DST_StartDate,21), 6, 18) < SubString(Convert(Varchar(23),tz.DST_EndDate,21), 6, 18)
And SubString(Convert(Varchar(23),#DateAndTime,21), 6, 18) >= SubString(Convert(Varchar(23),tz.DST_StartDate,21), 6, 18)
And SubString(Convert(Varchar(23),#DateAndTime,21), 6, 18) < SubString(Convert(Varchar(23),tz.DST_EndDate,21), 6, 18)
then tz.DST_AddOffSet
Else 0
End
+
-- Daylight Savings STARTS later in the Year than Ends (So, Southern Hemisphere), In Daylight Savings Surround Period
Case when
tz.Use_DST = 1
And SubString(Convert(Varchar(23),tz.DST_StartDate,21), 6, 18) > SubString(Convert(Varchar(23),tz.DST_EndDate,21), 6, 18)
And
(
SubString(Convert(Varchar(23),#DateAndTime,21), 6, 18) >= SubString(Convert(Varchar(23),tz.DST_StartDate,21), 6, 18)
Or
SubString(Convert(Varchar(23),#DateAndTime,21), 6, 18) < SubString(Convert(Varchar(23),tz.DST_EndDate,21), 6, 18)
)
then tz.DST_AddOffSet
Else 0
End
,#DateAndTime
)
From dbo.tblSomeEntityTable rd
Inner Join dbo.tblBranch b on rd.BranchID = b.ID
Inner Join dbo.tblUsers u on u.ID = #UserID
Inner Join dbo.tblTimeZones tz on tz.ID = case when u.UserTZOverBranchTZ = 1 then u.TimeZoneID else b.TimeZoneID End
Where
rd.ID = Case when ISNULL(#EntID, -1) = -1 then rd.ID else #EntID End
)
Go