Stuart Galloway
Stuart Galloway

Reputation: 1

SQL query using Date and Time

I need some help with a query.

I would like to use the GETDATE as today's date and specify the hours 6:00am to 6:00pm

for example, something like:

where t_stamp between "current date 06:00:00" and "current date 18:00:00"

Upvotes: 0

Views: 176

Answers (2)

Nick.Mc
Nick.Mc

Reputation: 19235

GETDATE appears to indicate it's SQL Server - please include this info in future.

One of many methods would be this:

where  t_stamp between
CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),126) + 'T06:00:00',126)
AND
CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),126) + 'T18:00:00',126)

Ugly I know. There's about a million different permutations of the same answer.

Upvotes: 1

Moho
Moho

Reputation: 16553

For SQL Server 2008 and later:

declare @start datetime
declare @end datetime 

set @start = dateadd( hh, 6, convert( datetime, convert(date, getdate()) ) )
set @end = dateadd( hh, 12, @start )

select @start, @end

Upvotes: 0

Related Questions