How can I convert datetimeoffset to datetime in SQL Server 2008? [duplicate] - sql-server

This question already has answers here:
How can I convert a Sql Server 2008 DateTimeOffset to a DateTime
(6 answers)
Closed 7 years ago.
I just need to change the date format from datatimeoffset to datetime while pulling the data

I will quote to "RichardTheKiwi"
declare #createdon datetimeoffset
set #createdon = '2008-12-19 17:30:09.1234567 +11:00'
select CONVERT(datetime2, #createdon, 1)
--Output: 2008-12-19 06:30:09.12
select convert(datetimeoffset,CONVERT(datetime2, #createdon, 1))
--Output: 2008-12-19 06:30:09.1234567 +00:00
Converting using almost any style will cause the datetime2 value to be converted to UTC.
Also, conversion from datetime2 to datetimeoffset simply sets the offset at +00:00, per the below, so it is a quick way to convert from Datetimeoffset(offset!=0) to Datetimeoffset(+00:00)
For more information you can visit: How can I convert a Sql Server 2008 DateTimeOffset to a DateTime

Related

MS SQL Converting a String to Datetime issue [duplicate]

This question already has answers here:
Milliseconds wrong when converting from XML to SQL Server datetime
(2 answers)
DateTime vs DateTime2 Time Range Discrepancy
(3 answers)
Closed 1 year ago.
I'm trying to convert the string to datetime, but getting an unexpected result.
Select Convert(DateTime, '2015-08-10 13:08:01.725', 121);
Result:
2015-08-10 13:08:01.727
Note that the milliseconds have changed from 725 to 727.
I'm using Microsoft SQL Server 2014 (SP3-CU2) (KB4482960) - 12.0.6214.1 (X64).
This is expected, datetime is only accurate to 1/300 of a second, and .725 can't be represented by that. As a result it's rounded to the nearest 1/300, which would be .726666666666~, and then displayed as .727 as the display layer is accurate to 3 digits.
If you want to be accurate to 1/1000 of a second, use a more precise data type. datetime2(3) seems to be what you are after:
SELECT CONVERT(datetime2(3), '2015-08-10 13:08:01.725', 121);
This is because DATETIME is rounded to increments of .000, .003, or .007 seconds
Directly from Microsoft documentation

MS SQL Server is dropping 1 millisecond from converted time [duplicate]

This question already has answers here:
Why is SQL Server losing a millisecond?
(6 answers)
Closed 5 years ago.
The following query:
select convert(datetime, '2016-06-20 7:22:52.728')
gives me:
2016-06-20 07:22:52.727
In SQL Server v12.0.4100.1.
Whatever value I put for milliseconds, the result always has 1 less millisecond.
Am I missing something or is this a bug?
It's not a bug. The resolution of datetime is such that the final digit can only be one of a few values... Rounded to increments of .000, .003, or .007 seconds.
This is documented: Microsoft documentation for datetime (Transact-SQL)
That is how datetime works in sql server. If you want that millisecond, switch to datetime2([3-7]).
datetime accuracy is to 0.00333 second.
datetime2 accuracy is to 100 nanoseconds.
Date and Time Data Types and Functions (Transact-SQL)
Similarly, if you want to get the server time with additional accuracy, you would use sysdatetime() which returns datetime2(7) instead of getdate() which returns datetime.

What is the correct MSSQL datatype if we want to support milliseconds in timestamp? [duplicate]

This question already has answers here:
DateTime2 vs DateTime in SQL Server
(17 answers)
Closed 6 years ago.
For example, in ORACLE, if we choose TIMESTAMP(3), it supports milliseconds.
For example, we could store in Oracle database, data like: 2016-11-28T14:48:15.565
For MSSQL, what should be the datatype chosen out of
datetime, datetime2?
you can define the data with Datetime type
The precision of DATETIME is ms and the precision of DATETIME2 is μs
DECLARE #d DATETIME=SYSDATETIME(),#d2 DATETIME2=SYSDATETIME()
SELECT #d AS d1,#d2 AS d2
output:
d1 d2
----------------------- ---------------------------
2016-11-28 17:39:51.773 2016-11-28 17:39:51.7734965

How to cast datetime into date in SQL Server 2005 [duplicate]

This question already has answers here:
How to return only the Date from a SQL Server DateTime datatype
(46 answers)
Closed 7 years ago.
I have used something like this:
select* from table where cast(CreatedDate as date) = cast(GETDATE() as DATE)
in SQL Server 2008R2 and it works perfectly. But when goes to 2005, it doesn't work because Date is not a defined system type. Can anyone let me know a way that works both 2005 and 2008R2?
The CreatedDate is default as current_timestamp and I want to get data with today's data only.
You can try this:
convert(DateTime, floor(convert(float, getdate())))
That gets rid of the time part, by converting it into the underlying float number and rounding down to the nearest integer. The integer part represents the date and the decimal part is the time, so converting the integer back to a DateTime give the same date you started with but without any time part.
You can also wrap it up as a scalar-valued function:
CREATE FUNCTION [dbo].[DateTrunc] (#date DateTime)
RETURNS DateTime
AS
BEGIN
return convert(DateTime, floor(convert(float, #date)))
END

Explicit conversion of datetime2 to timestamp in SQL Server [duplicate]

This question already has answers here:
UNIX_TIMESTAMP in SQL Server
(9 answers)
Closed 7 years ago.
I am working with a query in SQL Server:
select datetimevalue
from temp;
Here datetimevalue column's datatype is datetime2 and I need to convert this value into timestamp.
In MySQL if I simply write:
select UNIX_TIMESTAM(datetimevalue)
from temp;
then it returns timestamp.
Is there any way to convert datetime in timestamp explicitly in SQL Server 2014?
SQL Server doesn't have a timestamp type. It used to have one in old versions but that type IS NOT a date & time value. In current versions timestamp is a deprecated synonym for rowversion.

Resources