Share via

date format query

antony joseph 61 Reputation points
2021-06-25T08:43:37.997+00:00

Dear Experts

Pleass help to conver the below date format to as mentioned.

SELECT FORMAT(DATEADD(dd, 0, '2021-04-02 18:54:13.4966667'), 'HHmmss')

Desired result is 185413

I am getting error like

conversion failed when converting date and/or time from character string.

Please help

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

0 comments No comments

Answer accepted by question author

Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
2021-06-25T08:51:49.587+00:00

You need to explicitly cast the string literal to datetime2, because the default conversion is to datetime, which only handles three decimals.

SELECT FORMAT(DATEADD(dd, 0, cast('2021-04-02 18:54:13.4966667' as datetime2(7))), 'HHmmss')

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.