May 19, 2020 at 8:29 pm
Hello,
I am trying to convert '2020-04-29-14.41.26.686978' this value to DateTime2. Can you please suggested the best way.
Thanks,
Sab
May 19, 2020 at 8:42 pm
Declare @dateString varchar(30) = '2020-04-29-14.41.26.686978';
Select *
, DateOnly = left(d.dateString, 10)
, TimeOnly = substring(d.dateString, 12, 8)
, MillisecondsOnly = right(d.dateString, 7)
, OutputDatetime2 = cast(concat(left(d.dateString, 10), ' ', replace(substring(d.dateString, 12, 8), '.', ':'), right(d.dateString, 7)) As datetime2(7))
From (Values (@dateString)) As d(dateString);
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
May 19, 2020 at 8:45 pm
SELECT CONVERT(DATETIME2(7), STUFF(REPLACE(STUFF('2020-04-29-14.41.26.686978',11,1,'T'),'.',':'),20,1,'.'));
--Jeff Moden
Change is inevitable... Change for the better is not.
May 19, 2020 at 8:52 pm
Thank you !! Working as expected
May 19, 2020 at 9:28 pm
Thank you !! Working as expected
You're welcome but which one?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 19, 2020 at 9:48 pm
Shoot - I usually see the STUFF option but missed it on this one...and putting in the 'T' was on my mind but it is getting late 😉
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
May 19, 2020 at 10:41 pm
Thank you Jeff Moden - STUFF one :). however thank you both 🙂
May 20, 2020 at 12:09 am
Thank you for the feedback, Sabarish (I hope I split your name correctly). Just to be sure, though, do you understand what STUFF does? I ask only because a whole lot of people have never heard of it before.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 20, 2020 at 1:50 am
Thank again Jeff!!
STUFF() deletes a part of a string and then inserts another part into the string, starting at a specified position.
Step 1 : Add T between Date and Time using STUFF()
SELECT STUFF('2020-04-29-14.41.26.686978', 11, 1, 'T') -- 2020-04-29T14.41.26.686978
Step 2 : Replace all the [.] with [:]
SELECT REPLACE(STUFF('2020-04-29-14.41.26.686978', 11, 1, 'T'), '.', ':') -- 2020-04-29T14:41:26:686978
Step 3 : Replace 20th position [:] with [.] using STUFF()
SELECT STUFF(REPLACE(STUFF('2020-04-29-14.41.26.686978', 11, 1, 'T'), '.', ':'), 20, 1, '.'); -- 2020-04-29T14:41:26.686978
Please correct me if I am wrong on this. Appreciate your help
May 20, 2020 at 4:44 am
Nope... you've got it. The 2nd operand tells what character position to start working with. The 3rd operand identified how many characters to replace. It can be "0" which means don't replace anything... just to the "stuff" into that position. In both cases, I replace one character with another.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply