简体   繁体   中英

How to get specific time format in SQL Stored procedure

In my database, I have to perform CRUD operations based on the last date modified field.

DateLastModified

2021-09-09 16:46:05.670
....

In my SP, I am getting a date using getdate() function but it shows me the date something like this

set  @TodayDate = getdate()
May 17 2022  3:04PM

How can I get the date in the format shown below?

2022-05-17 15:04:00.113

Use SELECT instead of PRINT inside your procedure.

PRINT implicitly converts a date/time to a string using a specific style (0 IIRC). The full list of style numbers are documented here .

SELECT does no such shenanigan - it sends the original data type to the client, though the client is still free to render that in any way it pleases. It just happens that SSMS will render a datetime value the way you want to see it (which happens to be style 21 / 121).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM