Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Time value into Integer in sql server

I need to convert Time value into integer value .

For E.g: If the time value is '07:00 PM' I need to convert it into 19.

I have tried below query to do this.

select CAST(substring(CONVERT(varchar,convert(smalldatetime,'07:00 PM'),114), 1,2) as int) as VALUE

It returns me the desired output. But it's taking too much time in case of big transactions. Can anyone suggest me an alternative solution ( a simple one perhaps)?

I can't use datepart(hour, 'timevalue'). Because there are data's with time value '00:00 AM'and'00:00 PM'which through's conversion error.In This case I need to return the value as 0.`

like image 770
bmsqldev Avatar asked Feb 15 '26 13:02

bmsqldev


1 Answers

You seem to want to extract the hour from the time. Use datepart():

select datepart(hour, timecol)
like image 178
Gordon Linoff Avatar answered Feb 17 '26 03:02

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!