Click here to Skip to main content
15,885,954 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If we execute the query for 5 days with Start Time 1PM and End Time 5 PM (For eg), Query should fetch data between 1PM to 5 PM for all the 5 days. Likewise, if we pass any Datetime in the filter, the query should fetch data for the selected hour part for all the selected dates.
But I am not able to get the expecting result.Please help me to write a query

Sample Schema

ani nvarchar(100),
dnis nvarchar(100),
StartTime datetime


What I have tried:

SELECT id,ani,dnis,starttime,line
FROM CCR
WHERE CAST(StartTime as time) > CAST('16:45' as time) order by  StartTime desc  
Posted
Updated 15-Mar-18 9:11am

1 solution

If I set up a basic table:
ani     dnis    StartTime               Id
ani1   	dnis1   2018-03-15 09:00:00.000	1
ani2   	dnis2   2018-03-14 14:00:00.000	2
ani3   	dnis3   2018-03-13 16:59:00.000	3
ani4   	dnis4   2018-03-12 17:01:00.000	4
And run your query:
SQL
SELECT id,ani,dnis,starttime--,line
FROM CCR
WHERE CAST(StartTime as time) > CAST('16:45' as time) order by  StartTime desc

I get exactly what I expect - two rows:
id	ani	dnis	starttime
3	ani3    dnis3   2018-03-13 16:59:00.000
4	ani4    dnis4   2018-03-12 17:01:00.000

So your query does what you say you want it to...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900