Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Formatting datetime2(3) value
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01681626
Message ID:
01681650
Views:
37
This message has been marked as a message which has helped to the initial question of the thread.
Likes (1)
>>Hi,
>>
>>select stuff(replace(replace(convert(varchar(30), getdate(), 126),':',''),'-',''), 9, 1, '_')
>>
>>
>>MartinaJ
>
>Very nice! So, no FORMAT function option, right? And how would you do the opposite conversion from character to datetime?

I don't use FORMAT() functions because not all our customers use MSSQL 2014 or higher version.

MartinaJ
DECLARE @lcDT VARCHAR(30), @ldDT DATETIME2(3)
SELECT @lcDT=STUFF(REPLACE(REPLACE(CONVERT(VARCHAR(30), GETDATE(), 126),':',''),'-',''), 9, 1, '_'),
       @ldDT=CONVERT(DATETIME2(3), STUFF(STUFF(STUFF(STUFF(STUFF(@lcDT, 9, 1, 'T'), 12, 0, ':'), 15, 0, ':'), 5, 0, '-'), 8, 0, '-') , 121)

SELECT @lcDT, @ldDT

SELECT @lcDT=FORMAT(@ldDT, 'yyyyMMdd\_HHmmss\.fff'),
       @ldDT=CONVERT(DATETIME2(3), STUFF(STUFF(STUFF(STUFF(STUFF(@lcDT, 9, 1, 'T'), 12, 0, ':'), 15, 0, ':'), 5, 0, '-'), 8, 0, '-') , 121)

SELECT @lcDT, @ldDT
"Navision is evil that needs to be erazed... to the ground"

Jabber: gorila@dione.zcu.cz
Jabber? Jabbim
Previous
Reply
Map
View

Click here to load this message in the networking platform