0

I have a table with column Date and time together (Say Created Date). I have a requirement to fetch records from the Table by passing the time alone(since I retrieve the records at different intervals in same date).

My question is how do I break the time part alone in DateTime field in my Table while Fetching the records using Stored procedure

Further to Quote an example I have to fetch data from the same table for 5 times in a specified date say

9.00 to 10.00
11.00 to 12.00
....
....
13.00 to 14.00 etc
a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
Vasanth R
  • 11
  • 3

3 Answers3

1

You can also use the datepart() function to just extract the hour:

select datepart(hour, createdat), . . .
from . . .
where datepart(hour, createdat) between 9 and 13
group by datepart(hour, createdat)
order by datepart(hour, createdat);
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709
0

You can break Time part from DateTime field Using of below code.

select cast(CreatedDate as time) as TimePart
from your_Table_name
nisha
  • 21
  • 2
0

I think this was very simple and can be done in very optimize and SARGABLE way.

Declare @input datetime='2018-09-04'

declare @starttime varchar(15)='13:00'
declare @endtime varchar(15)='14:00'

declare @StartDate datetime=@input+@starttime

declare @EndDate datetime=@input+@endtime

select * from 
table1 
where createdate>=@StartDate and createdate<=@EndDate
KumarHarsh
  • 4,894
  • 1
  • 16
  • 20