Baiju Mohan
Baiju Mohan

Reputation: 59

How to find out the records between AM and PM

I need to find out the records between AM and PM from the below table.

table1

for eg: If I Enter START TIME AS 7.00 AM AND END TIME AS 4.00 PM, I need to get below records.

output

Start Time and End Time data is in varchar datatype with 12 hour format. How do i distinguish between AM and PM in the search criteria. Is there any inbuilt function to extract the time in 24 hour format in sql server?. Thanks in advance.

Upvotes: 0

Views: 387

Answers (1)

Ravi
Ravi

Reputation: 31417

Convert your fields into time format and then compare it.

SELECT CONVERT(time, '07:01 PM')

SQL Fiddle

Now, you cane write your query like below

select * from yourtable where CONVERT(time, starttime) >= CONVERT(time, '07:00 AM') AND CONVERT(time, endtime) <= CONVERT(time, '04:00 PM')

Upvotes: 1

Related Questions