How to compare Indian time with world's other country times in SQL Server 2008?
Means i want to know if in India its mid night 1 am what will be timing in other countries through SQL Server 2008
SQL Server 2008 uses the DateTimeOffset type to represent DateTime with a timezone offset.
You can convert values from one timezone to another using the SWITCHOFFSET function.
To create DateTimeOffset values for a specific timezone from existing date values, use TODATETIMEOFFSET.
You can get the current date and time including the offset using SYSDATETIMEOFFSET.
Finally, you can use plain-old DATEDIFF to compare DateTimeOffset values and get their difference.
To get your current time in India and switch it to another timezone you have to know the proper time offset. The statement would be something like
DECLARE #localTime DATETIMEOFFSET=SYSDATETIMEOFFSET()
SELECT #localTime, SWITCHOFFSET(#localTime,'-5:00')
Have a look at SYSUTCDATETIME():
SELECT SYSDATETIME(), SYSUTCDATETIME();
With UTC Time you can easily calculate the time in any other country.
DECLARE #indianTime DATETIME = GETDATE()
DECLARE #timeZoneOffset SMALLINT = 3 -- or -3
SELECT DATEADD(hour, #timeZoneOffset, #indianTime)
See next MSDN articles: 1, 2
But it's better to use SQL Server 2008 and DateTimeOffset type which supports time zones.
So you could do next:
DECLARE #indianTime DATETIME = SYSDATETIMEOFFSET()
DECLARE #timeZoneOffset NVARCHAR(6) = '-03:00'
SELECT SWITCHOFFSET(#indianTime, #timeZoneOffset)
Related
I have database table that has datetime field in SQL Server.
I have to send as below sample.
Can you please help how to get this dateformat?
Example: 2020-06-10T13:11:00-05:00
Thanks
If you were on a supported version of SQL Server, this is actually quite trivial. If we assume the value in the table is 2020-06-10T13:11:00 it would just need a CONVERT and SWITCHOFFSET:
SELECT V.Dt,
SWITCHOFFSET(CONVERT(datetimeoffset(0),V.dt),'-05:00')
FROM (VALUES(CONVERT(datetime2(0),'2020-06-10T13:11:00')))V(Dt);
If it's actually a UTC time, and you need to change it to (I assume) Central, then it would be:
SELECT V.Dt,
CONVERT(datetimeoffset(0),V.dt) AT TIME ZONE 'Central Standard Time'
FROM (VALUES(CONVERT(datetime2(0),'2020-06-10T18:11:00')))V(Dt);
Instead, you're going to have to do some varchar manipulation, as you're using a version of SQL Server that is completely unsupported:
--If switching the offset
SELECT V.Dt,
CONVERT(datetimeoffset(0),CONVERT(varchar(20),V.Dt,126) + '-05:00')
FROM (VALUES(CONVERT(datetime2(0),'2020-06-10T13:11:00')))V(Dt);
--If changing the offset
SELECT V.Dt,
CONVERT(datetimeoffset(0),CONVERT(varchar(20),DATEADD(HOUR, -5,V.Dt),126) + '-05:00')
FROM (VALUES(CONVERT(datetime2(0),'2020-06-10T18:11:00')))V(Dt);
Note that both of these are DST agnositic, as it SWITCHOFFSET. Only AT TIME ZONE will consider the DST.
I am bit confusing here?
declare #date1 datetime = '2016-01-21 14:10:47.183'
I want to convert '2016-01-21 14:10:47.183' To '21-01-2016'
when I tried: select convert(date,#date1,105)
I am getting: 2016-01-21
But with: select convert(varchar(10),#date1,105)
I am getting: 21-01-2016
Why I am not having same results with above code?
Why should I convert to varchar?
Thanks in advance
This is just presentation matter and should be done in application layer. If you cannot do it in application you could use FORMAT (SQL Server 2012+):
declare #date1 datetime = '2016-01-21 14:10:47.183'
SELECT FORMAT(#date1, 'dd-mm-yyyy');
LiveDemo
Why I am not having same results with above code?
select convert(date,#date1,105)
-- DATETIME -> DATE
-- vs
select convert(varchar(10),#date1,105)
-- DATETIME -> VARCHAR(10) using specific style
If you only to skip time part use SELECT CAST(#date1 AS DATE) and do not bother how it is presented. It is still DATE.
To sum up: in SQL query use DATE as date, in application display it with desired format.
The reason why is because once you put a value in a datetime column (or date or any of the other variations on date-time datatypes) in SQL Server. SQL Server ceases to think of that date as having any particular format. It translates it into numbers, and stores it that way internally.
So when you select a date from a date time column, SQL Server displays it in the default format that you have selected based on your environment/local settings.
If you want to display it in any other format, you have to first convert it to a string, because as far as SQL Server is concerned, dates don't have formats. They are just numbers. The 21st day of March is the 21st day of March, whether you write it as 3/21 or 21/3.
So when you try to convert a date to a date with a different format, SQL Server just ignores you because dates don't have formats. However, if you want to convert that date to a string, SQL Server will be happy to help you display that string in any format you like.
Hope this helps, but sounds like some further research into how SQL Server stores dates would help your understanding.
I want to convert Eastern time ("GMT-05:00") into IST ("GMT+05:30") in SQL Server 2008.
It should based on Bias & DayLightBias.
Ex: Eastern Time has Bias value 300 & DaylightBias value -60 & IST has Bias value -330 & DayLightBias value -60.
I know how to convert this in C# but I want to create a job and for that I need this conversion in SQL Server.
Use the DATETIMEOFFSET datatype and the SWITCHOFFSET method in SQL Server 2008 or newer:
-- define your input in Eastern Time
DECLARE #Input DATETIMEOFFSET = SYSDATETIME()
-- SET #Input = SWITCHOFFSET(#Input, '-05:00')
SET #Input = SWITCHOFFSET(#Input, -300)
DECLARE #output DATETIMEOFFSET
-- convert Eastern Time to IST
-- SET #output = SWITCHOFFSET(#input, '+05:30')
SET #output = SWITCHOFFSET(#input, 330)
SELECT #Input, #output
MSDN SQL Server Books Online documentation for DATETIMEOFFSET
MSDN SQL Server Books Online documentation for SWITCHOFFSET
There are a lot more things to consider other than just the base offset and dst bias. Specifically, different offsets switch between standard and daylight time on different dates and at different times.
The correct way is with a named time zone. Unlike many other databases, SQL Server does not have native support for time zones. It only has support for time zone offsets. See "Time Zone != Offset" in the timezone tag wiki.
Fortunately, I've done all the hard work for you. Using my SQL Server Time Zone Support project, you can write this query:
SELECT Tzdb.ConvertZone(yourDateTimeValue, 'America/New_York', 'Asia/Kolkata', 1, 1)
The time zones are standard IANA tz database identifiers, and the numerical options at the end are explained in the project's readme.
I would like to know which format SQL Server saves datetimes, GMT or UMT?
The default DATETIME value in SQL Server has no knowledge of time zones and thus doesn't really care about time zones - that's entirely up to you to manage.
DECLARE #MyDate DATETIME
SET #MyDate = '20100922 04:05:06' --- no information on timezone
See MSDN docs on DATETIME.
With SQL Server 2008, a new data type was introduced called DATETIMEOFFSET which stores time along with a timezone offset. So here, you can store the local time and store the timezone that time is local to as well.
DECLARE #MyDateOffset DATETIMEOFFSET
SET #MyDateOffset = '20100922 04:05:06 +09:00' -- UTC plus 9 hours
Neither, it just returns the values in a specific format but stores it in a format without a timezone.
DECLARE #DateNow smalldatetime
SET #DateNow='12:30'
select #DateNow
-------------------------------------OR--------------------------------------
select CAST( '12:30' as datetime )
Result: 1900-01-01 12:30:00.000 (i don't want this)
But i need this result in time format not string not datetime?
Result: 12:30 (i want this)
Like José said, you can use CONVERT to display a datetime as date. MSDN has a list of all possible formats. For example format 8 is hh:mi:ss:
select convert(varchar(32),getdate(),8)
12:51:21
Now, you can cut the seconds off by specifying less characters:
select convert(varchar(5),getdate(),8)
12:51
Another often used format is 121, yyyy-mm-dd hh:mi:ss.mmm(24h):
select convert(varchar(32),getdate(),121)
2009-05-08 12:51:21.987
Where you can pick the time part like:
select substring(convert(varchar(32),getdate(),121),12,5)
12:51
Or to combine the string trickeries:
select right(convert(varchar(16),getdate(),121),5)
12:51
Right? Right!
There's a TIME type in SQL Server 2008, but in previous versions, you can represent it as a varchar for display.
This is how you can retrieve the time portion of a DATETIME field in the format you want.
DECLARE #DateNow smalldatetime
SET #DateNow = GETDATE() -- 2009-05-08 12:58:02.680
SELECT CONVERT(CHAR(5), #DateNow, 8)
-- this returns the time portion as 12:58
You can use the CONVERT function.
By the way, there's no "TIME" datatype in SQL Server. So, the converted result will always be a STRING.
EDIT: There's no "TIME" datatype in SQL Server versions < SQL Server 2008.
If you need to compare, add and/or subtract time (only) values, think about the possibility to use a INT to store the "TIME" value and then to CONVERT it to CHAR when displaying the result.