Can't Display a DateTimeOffset value the way I want [duplicate] - sql-server

This question already has answers here:
Omitting the Milliseconds in a Date
(7 answers)
Closed 9 years ago.
I have a DateTimeOffset column in my table named BarcodeTime. A sample value looks like this:
2013-01-20 03:34:36.8930000 -05:00
I'd like to display it as follows:
2013-01-20 03:34:36
I thought the following should do it:
CONVERT(datetime2, CONVERT(datetime2, BarcodeTime, 0), 120) as BarcodeTime
But this isn't working. What I get is this: 2013-01-20 03:34:36.8930000
Can someone show me the correct way?

Set the number of decimals on the datetime2 value to 0.
CONVERT(datetime2(0), BarcodeTime)

Maybe a bit ugly, but if you just want to change the display output you could try:
SELECT LEFT(BarcodeTime, 19)

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

SQL Server returns question mark for some unicode chars [duplicate]

This question already has answers here:
What is the meaning of the prefix N in T-SQL statements and when should I use it?
(4 answers)
Closed 3 years ago.
In a nvarchar(512) field if i store unicode chars like this:
UPDATE MYTABLE SET UNICODEFIELD = 'TレEホSᅯTル'
when i query it i get
T?E?S?T?
It looks like the "unusual" chars are not considered as unicode, i would expect the "?" behavior in case of varchar, while with nvarchar it should work fine, i am expecting
TレEホSᅯTル
as output, instead of
T?E?S?T?
Does anyone have an idea about this?
Because you're using a varchar, not an nvarchar. 'TレEホSᅯTル' = 'T?E?S?T?' as characters like レ can't be stored in a varchar.
Use a literal nvarchar:
UPDATE MYTABLE SET UNICODEFIELD = N'TレEホSᅯTル';

How do I convert a SQL server timestamp to an epoch timestamp? [duplicate]

This question already has answers here:
converting Epoch timestamp to sql server(human readable format)
(2 answers)
Closed 5 years ago.
I know how to convert an epoch timestamp to a SQL server timestamp using DATEADD, but I am interested in a function that does the reverse of that.
You can do it using DATEDIFF function like below :
select DATEDIFF(s, '1970-01-01 00:00:00', '2017-11-20 23:12:02.000') as EpochTimeStamp
Output :
EpochTimeStamp
--------------
1511219522
You know already how can we get back the original date :
SELECT DATEADD(ss, 1511219522, '19700101') as OriginalDate
OriginalDate
-----------------------
2017-11-20 23:12:02.000

How to Subtract 119 days from current date in Sql Server [duplicate]

This question already has answers here:
How to subtract 30 days from the current date using SQL Server
(4 answers)
Closed 5 years ago.
I want to subtract 119 days from given date,
i tried dateadd function but it is not working.
select dateadd(dd,-119,'09/05/2017')
DATEADD is correct - I don't know how you have used it, but this would be the way:
SELECT DATEADD(day, -119, #givenDate)
Just saying "it is not working" doesn't help. From what I gather, it does work, but you don't know the syntax. Try this -
SELECT dateadd(d,-119,getdate())

how to get today's Date only and not with the time using sql [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Most efficient way in SQL Server to get date from date+time?
I want to update a field with today's date only but the problem i am having now is it is giving me the date and the time. I would like to update with specific format like this:
11/09/2012
how can i achieve this? here is my current code:
UPDATE MyTable
SET Field1 = GETDATE()
One way is to update and select only date (not time)
UPDATE #tab SET dates=(CONVERT(VARCHAR(10),GETDATE(),103));
SELECT CONVERT(VARCHAR, GETDATE(), 23) FROM #tab
Not much different here apart from slight syntax differences however I would recommend wrapping it up in a function.
CREATE FUNCTION [dbo].[GetDateOnly] (#Datetime datetime)
RETURNS datetime
AS
BEGIN
RETURN CONVERT(Datetime, FLOOR(CONVERT(float,#Datetime)))
END

Resources