I have a table like this:
| Id | Data | TimeStamp
-- Example info. Check Data EOL to see Time
| Id | 732DC7DE-2B9D-4B91-8753-0004128B26D2
| Data | {"message":"Machine is down","machineId":"165ACE37-4E2C-4D44-9D14-D4D3ABK66C","machineName":"1501","ipAddress":"192.168.0.1","time":"2018-05-20T18:33:23.171"}
| TimeStamp | 2018-05-20 18:33:23.1710000
I need to convert the datetime values in this table to UTC time.
I've figured out the TimeStamp conversion.
Update ProcessEventMessage SET TimeStamp = DateADD(hour, -2, TimeStamp)
My local is UTC+2h I.E. I have to remove 2h from the json-data.
But I have no idea how to convert the json-datetime object into UTC.
I've tried something like this but without success. How would I accomplish converting these numbers into UTC? (i.e. remove two hours).
"time":"2018-05-20T18:33:23.171"
Read more about ISO_8601 Here Structure: YYYY-MM-DDTHH:MM:SS.MSS
TL;DR: Remove 2h from the json string
Thoughts: one way to do this, would be to look for the T in the time, and replace the hours after it to deduct 2h. (possible?)
EDIT:
I've now got a structured query of the sql-table like this:
DECLARE #json NVARCHAR(MAX) = {"message":"Machine is down","machineId":"165ACE37-4E2C-4D44-9D14-F9E2CB2C2C13","machineName":"1501","ipAddress":"192.168.150.101","time":"2018-05-20T18:33:23.171"}
SELECT * FROM OPENJSON(#json)
WITH ( message varchar(200) '$.message',
machineId varchar(200) '$.machineId',
machineName int '$.machineName',
ipAddress varchar(200) '$.ipAddress',
time datetime2(7) '$.time'
)
Can i somehow use these variables to modify the value?
Try something like this:
DECLARE #UTCDateTime DATETIME = GETUTCDATE();
SET #UTCDateTime = TimeStamp AT TIME ZONE 'your time zone' AT TIME ZONE 'UTC'
SET #UTCDateTime = TimeStamp AT TIME ZONE 'Central Europe Standard Time' AT TIME ZONE 'UTC'
select #UTCDateTime
(AT TIME ZONE) works on only SQL Server 2016+
DECLARE #JSON NVARCHAR(MAX)
SET #JSON =
'{
"TMP": [
{"message":"Machine is down","machineId":"165ACE37-4E2C-4D44-9D14-F9E2CB2C2C13","machineName":"1501","ipAddress":"192.168.150.101","time":"2018-05-20T18:33:23.171"}]}
'
SET #JSON = JSON_MODIFY(#JSON, '$.TMP[0]', 'something')
Related
My table in the DB has the following format yyyy-mm-dd HH:MM:SS. but the required format for a varchar data type is dd-mm-yyyy HH:MM:SS. when I change this in the query below the code runs but it returns nothing as the table date format doesn't match the varchar data type.
I assume it's a simple data conversion but I'm quite new to this and not sure on how to get around it.
DECLARE #LastxDays TABLE (DateForReport datetime)
DECLARE #DateForReport datetime
DECLARE #Results TABLE (DateForReport DateTime,
Currency VARCHAR(20),
TotalTransactionsIn Decimal(10,2),
TotalTransactionsOut Decimal(10,2),
TotalBalances Decimal(10,2))
-- SET initial date
SET #DateForReport = CAST('26-05-2022 17:00:00' as DATETIME)
PRINT #DateForReport
-- Add a row for each day of the year up to yesterday
WHILE(#DateForReport < dateadd(dd,-1, getdate()))
BEGIN
INSERT INTO #LastxDays VALUES (#DateForReport)
SET #DateForReport = DATEADD(day, 1, #DateForReport)
END
my table result is the following:
|Date | Name |Transaction in |Transaction out|
|2022/26/05|***** |***** |***** |
I'm inserting sample call data to the table ContactCallDetail. when I query the data inserted I'm getting datetime in the format yy-mm-dd hh-mm-ss-ff
declare #StartDateTime datetime = convert(varchar, getdate(), 20);
declare #EndDateTime datetime = dateadd(ss,20,#StartDateTime);
INSERT INTO ContactCallDetail values(0,NULL,'+xxxxx545xx77',#StartDateTime,#EndDateTime,0);
GO
select * from ContactCallDetail
SessionID SessionSeqNum originatorID CallingDN StartDateTime EndDateTime Transfer
100000 0 NULL +xxxxx5453xx7 2022-02-28 20:11:34.000 2022-02-28 20:11:54.000 0
I need to store the datetime without fractions part(ff). how can I achieve that? i want to see 2022-02-28 20:11:34 instead of 2022-02-28 20:11:34.000
The datetime data type always stores fractional seconds with a 1/300 second precision. Use datetime2(0) if you don't need fractional seconds. This will also save storage space (2 bytes).
Just to be sure and to add to #DanGuzman 's answer, you need to change the datatype of the StartDateTime and EndDateTime columns in the ContactCallDetail table to datetime2(0).
That will also make it so you don't have to do CONVERT either on the way in or the way out.
I have a table with the column CreatedDateTime as shown below :
[CREATED_DATE_TIME] [datetime] NOT NULL DEFAULT GETUTCDATE()
When I am retrieving the created date time, it is giving me datetime value in UTC, but I want to show the datetime value in local time.
How can this be achieved?
Looking forward to your answers and thanks in advance.
Try like below
— Convert a UTC Time to a Local Time
DECLARE #UTCDate datetime
DECLARE #LocalDate datetime
DECLARE #TimeDiff int
— Figure out the time difference between UTC and Local time
SET #UTCDate = GETUTCDATE()
SET #LocalDate = GETDATE()
SET #TimeDiff = DATEDIFF(hh, #UTCDate, #LocalDate)
— Convert UTC to local time
DECLARE #DateYouWantToConvert datetime
DECLARE #ConvertedLocalTime datetime
SET #DateYouWantToConvert = '4/25/2007 18:00'
SET #ConvertedLocalTime = DATEADD(hh, #TimeDiff, #DateYouWantToConvert)
— Check Results
PRINT #ConvertedLocalTime
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.
I'm importing an access database to sql.
The original access database has a date field that imports nicely, but the time field is text (10:00 AM, for instance).
I have over 4000 records and assume there is a way to convert 10:00 AM to 10:00:00.000 (or 07:30 PM to 19:30:00.000, etc...) so that it can be combined with the pre-existing date field (which is now like 2011-11-11 00:00:00.000).
Also, if it's easy to do the conversion and concatenation in the same process, please note.
to convert the time from am or pm 10:00 PM format into time format 10:00:00.000:
select cast(timestring as time(7))
look this:
declare #timeField as varchar(10)
set #timeField = '07:30 PM'
declare #dateField as varchar(10)
set #dateField = '1900-01-01'
select CONVERT(datetime,#dateField + ' ' + CAST(CONVERT(time, #timeField ,121) AS VARCHAR(11)),121)
In your import scripts (I assume you use some sort of SQL to E.T.L your data from Access to SQL server), you can use the convert function as such:
declare #t as time = Convert(time, '10:00PM' )
print #t -- prints 22:00:00.0000000
OR
declare #t as time = Convert(time, '10:00AM' )
print #t -- prints 10:00:00.0000000
And of course if you want to control the precision as per your example:
Convert(time(3), '10:00PM' ) -- prints 22:00:00.000