I'm using
convert(varchar(20), getdate(), 112)
to convert getdate() to yyyymmdd format (ISO format), which works great. Now I need to do something similiar to get the time in hhmm format. How can I achieve this?
Example: 12:10 pm should look like 1210, 3:43 pm should look like 1543.
By using format code as 108 we can get datetime in “HH:mm: ss” format. By using format code as 109 we can get datetime in “MMM DD YYYY hh:mm:ss:fff(AM/PM)” format. By using format code as 110 we can get datetime in “MM- DD-YY” format. By using format code as 111 we can get datetime in “YYYY/MM/DD” format.
How do I get HH MM SS from date in SQL? SELECT convert(varchar, getdate(), 108) outputs as hh:mm:ss .
The HOUR() , MINUTE() , and SECOND() functions extract the hours, minutes, and seconds from a date or datetime value respectively. And if you're working with a higher precision, you can use the MICROSECOND() function to return the microseconds part.
SELECT REPLACE(CONVERT(varchar(5), GETDATE(), 108), ':', '')
SELECT REPLACE(CONVERT(CHAR(5),GETDATE(),108), ':', '')
If you don't need colon, just remove it...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With