sql server 2000 convert datetime to get hhmm
Asked Answered
T

3

9

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.

Tallent answered 14/11, 2011 at 18:11 Comment(2)
msdn.microsoft.com/en-us/library/aa258265%28v=sql.80%29.aspxHerdic
@MarcB -Your suggesting I get the datepart for the hours, get the datepart for the minutes, and concantenate?Tallent
B
21
SELECT REPLACE(CONVERT(varchar(5), GETDATE(), 108), ':', '')
Breather answered 14/11, 2011 at 18:13 Comment(1)
that is military time. It actually take more work to get a 12 hour clock out of it. msdn.microsoft.com/en-us/library/ms187928.aspx Instead of 108, you can also use 8, 14 or 114 to get the same results.Antonyantonym
I
3
SELECT REPLACE(CONVERT(CHAR(5),GETDATE(),108), ':', '')

If you don't need colon, just remove it...

Impersonality answered 14/11, 2011 at 18:21 Comment(0)
C
0

Do NOT do just the datepart for the hours, get the datepart for the minutes, and concatenate as 19:05 will end up 195. If you go this route, you'll need to do something like this to deal with minutes:

right('0' + varchar(datepart(mi,getdate())),2)

at this point it's getting pretty inefficient.

Canary answered 16/9, 2014 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.