Showing posts with label equivalent. Show all posts
Showing posts with label equivalent. Show all posts

Monday, March 19, 2012

DateTime in UTC

I have a DateTime column in a database table.

How can I get the equivalent UTC value for this column?

eg something like the DateTime.ToUniversalTime() in C#.

or select DATE_COL1, getutcdate(DATE_COL1) FROM TABLE

nb: The GETUTCDATE() function returns the current utc DATEIt depends what you want to do exactly - is the offset from UTC based
on where the server is physically, where the clients are physically, or
something else? I don't believe there's any easy way to do this in
MSSQL, because you need to know the server's location and current UTC
offset, so you would probably need an external program which gets this
information from the operating system. If you want to base the offset
on the clients' location, then things would be more complicated,
especially if you have clients in different time zones.

If the offset is constant, you could put it in a lookup table and
create your own scalar function to modify the date, but then you would
need to handle daylight savings and so on yourself as well. So if the
C# function you mentioned already does what you want, it might be
easiest just to use it in an external program (in SQL 2005 you could
write a C# stored procedure or function to do this).

Simon|||PromisedOyster (PromisedOyster@.hotmail.com) writes:
> I have a DateTime column in a database table.
> How can I get the equivalent UTC value for this column?
> eg something like the DateTime.ToUniversalTime() in C#.
> or select DATE_COL1, getutcdate(DATE_COL1) FROM TABLE
>
> nb: The GETUTCDATE() function returns the current utc DATE

Use the dateadd() function. You will have to handle the logic for
the offset to UTC yourself, as SQL Server does not have any time zone
information.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||hi

hope this trick would work:
select DATE_COL1, dateadd("mi", datediff("mi",GETUTCDATE() ,getdate())
,DATE_COL1)

best Regards,
Chandra
http://www.SQLResource.com/
http://chanduas.blogspot.com/
------------

*** Sent via Developersdex http://www.developersdex.com ***|||We do this all the time:

--test method
DECLARE @.Date smalldatetime
SET @.Date = GETDATE()

SELECT @.date, DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()),
@.Date), GETUTCDATE()

You could deal with minutes, but since UTC time is a change in hours,
simply your life :)

Stu|||Hi Stu
I think UTC time deals with 1/2 Hrs also. and because of this minute
should be correct.

For eg, India is +5.30 Hrs GMT

best Regards,
Chandra
http://www.SQLResource.com/
http://chanduas.blogspot.com/
------------

*** Sent via Developersdex http://www.developersdex.com ***|||Thanks Stu

I worked this out myself and the proc I developed is about the same as
yours. I had to use minutes though to handle Central Australian Time.

Stu wrote:
> We do this all the time:
> --test method
> DECLARE @.Date smalldatetime
> SET @.Date = GETDATE()
> SELECT @.date, DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()),
> @.Date), GETUTCDATE()
> You could deal with minutes, but since UTC time is a change in hours,
> simply your life :)
> Stu|||Really? I never knew that. I always thought that the timezones were
shifts in hours; didnot realize they shifted in half hors as well.
That's gotta be a pain for mking long distance calls.

:)|||Stu wrote:
> Really? I never knew that. I always thought that the timezones were
> shifts in hours; didnot realize they shifted in half hors as well.
> That's gotta be a pain for mking long distance calls.
> :)
Yes, its true.

And to further complicate things, Nepal is GMT+5:45. They just had to
be different from India.

Saturday, February 25, 2012

dateserial eqivalent

hi all
what is an equivalent to VB's dateserial (if any)?
thanx,
erezNot sure what you want to do, but you can use convert.
For example
select convert(datetime, '2006-01-26')|||Erez Mor wrote:
> hi all
> what is an equivalent to VB's dateserial (if any)?
> thanx,
> erez
DateSerial takes three numeric arguments for year, month and day and
turns them into a date. There probably aren't many situations in SQL
Server when it would make sense to manipulate dates as three separate
numeric values. The easiest way to specify dates in code is as a
string:
SELECT CAST('20060126' AS DATETIME);
Always store dates as DATETIME or SMALLDATETIME.
If you need to do date arithmetic, use the DATEADD and DATEDIFF
functions.
If you still think you have a need to use three numerics for year,
month and day, try this:
SELECT
CAST(
RIGHT('0000'+CAST(2006 AS VARCHAR(4)),4)+
RIGHT('00'+CAST(1 AS VARCHAR(2)),2)+
RIGHT('00'+CAST(26 AS VARCHAR(2)),2)
AS DATETIME);
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||thanx a lot, folks
"David Portas" wrote:

> Erez Mor wrote:
> DateSerial takes three numeric arguments for year, month and day and
> turns them into a date. There probably aren't many situations in SQL
> Server when it would make sense to manipulate dates as three separate
> numeric values. The easiest way to specify dates in code is as a
> string:
> SELECT CAST('20060126' AS DATETIME);
> Always store dates as DATETIME or SMALLDATETIME.
> If you need to do date arithmetic, use the DATEADD and DATEDIFF
> functions.
> If you still think you have a need to use three numerics for year,
> month and day, try this:
> SELECT
> CAST(
> RIGHT('0000'+CAST(2006 AS VARCHAR(4)),4)+
> RIGHT('00'+CAST(1 AS VARCHAR(2)),2)+
> RIGHT('00'+CAST(26 AS VARCHAR(2)),2)
> AS DATETIME);
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>