Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I need to select all hrs in day in 12 hrs format based on current time .ie if it is 03:00 PM I should get all the times between 03:00 PM and 12:00 AM .Any help will be really appreciated .Thanks in Advance

What I have tried:

select distinct CONVERT(varchar(15),CAST( dateadd(hour,number-1,'01-jan-2017' ) AS TIME),100) as slots from master..spt_values where number>=1 and number<=24
Posted
Updated 1-Sep-18 2:03am

1 solution

Do you mean you want to return all rows where the enter date is between now and midnight tonight? If so, try something like:
SQL
SELECT * FROM MyTable WHERE EnterDateColumn BETWEEN @StartDate AND CONVERT(DATE, DATEADD(dd, 1, @StartDate))

If you don't then you need to explain exactly what you do mean, probably with input and output examples...
 
Share this answer
 
Comments
Member 12926744 1-Sep-18 8:12am    
No I need to select all hrs in a day between 12:00 Am to 12:00 PM . such as
12:00 AM ,1:00 AM,2:00 AM......12:00 PM .
I am getting these in the following query "select distinct CONVERT(varchar(15),CAST( dateadd(hour,number-1,'01-jan-2017' ) AS TIME),100) as slots from master..spt_values where number>=1 and number<=24"

But the problem is that ,for example if the current time is 3:00 pM , I need only those times between 3:00 pm and 12 :00 pm and so on.
OriginalGriff 1-Sep-18 8:27am    
Then just change
... where number >= 1
to
... where number >= 15
Member 12926744 1-Sep-18 9:00am    
how to check it with current time as I am stuck there
OriginalGriff 1-Sep-18 9:02am    
DATEPART(hh, GETDATE())

Bear in mind that will be in Server time - so if your server is in a different timezone, you will need to pass the value you want from your presentation software.

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