Unix Technical Forum

select between date

This is a discussion on select between date within the SQL Server forums, part of the Microsoft SQL Server category; --> I am trying to get al the rows from table1 where datetime is between 9:00AM yesterday and 9:00AM today ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:16 AM
Fred
 
Posts: n/a
Default select between date

I am trying to get al the rows from table1 where datetime is between 9:00AM
yesterday and 9:00AM today if the time now is less than 9:00AM. Otherwise it
should return all where datetime>9:00 AM today.

Is this possible as a query in sql2000?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:16 AM
Erland Sommarskog
 
Posts: n/a
Default Re: select between date

Fred (Fred@hotmail.com) writes:
> I am trying to get al the rows from table1 where datetime is between
> 9:00AM yesterday and 9:00AM today if the time now is less than 9:00AM.
> Otherwise it should return all where datetime>9:00 AM today.
>


SELECT ...
FROM tbl
WHERE datepart(hour, getdate()) >= 9 AND
datecol >= convert(char(8), getdate(), 112) + ' 09:00:00'
OR datepart(hour, getdate()) < 9 AND
datecol BETWEEN datediff(day, -1,
convert(char(8), getdate(), 112) + ' 09:00:00') AND
convert(char(8), getdate(), 112) + ' 09:00:00'

The recurring expression

convert(char(8), getdate(), 112) + ' 09:00:00'

could be put in a function for shorter code. However, this could be
expensive performancewise.

The above is not tested.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 04:16 AM
John Gilson
 
Posts: n/a
Default Re: select between date

"Fred" <Fred@hotmail.com> wrote in message news:INvAc.32931$sj4.2639@news-server.bigpond.net.au...
> I am trying to get al the rows from table1 where datetime is between 9:00AM
> yesterday and 9:00AM today if the time now is less than 9:00AM. Otherwise it
> should return all where datetime>9:00 AM today.
>
> Is this possible as a query in sql2000?


Assume table T and datetime column dt.

SELECT *
FROM T
WHERE DATEPART(HOUR, CURRENT_TIMESTAMP) < 9 AND
dt BETWEEN
DATEADD(HOUR,
9,
CONVERT(CHAR(8), CURRENT_TIMESTAMP - 1, 112))
AND
DATEADD(HOUR,
9,
CONVERT(CHAR(8), CURRENT_TIMESTAMP, 112))
UNION ALL
SELECT *
FROM T
WHERE DATEPART(HOUR, CURRENT_TIMESTAMP) >= 9 AND
dt > DATEADD(HOUR,
9,
CONVERT(CHAR(8), CURRENT_TIMESTAMP, 112))

or, alternatively,

SELECT *
FROM T
WHERE (DATEPART(HOUR, CURRENT_TIMESTAMP) >= 9 OR
(dt BETWEEN
DATEADD(HOUR,
9,
CONVERT(CHAR(8), CURRENT_TIMESTAMP - 1, 112))
AND
DATEADD(HOUR,
9,
CONVERT(CHAR(8), CURRENT_TIMESTAMP, 112))))
AND
(DATEPART(HOUR, CURRENT_TIMESTAMP) < 9 OR
dt > DATEADD(HOUR,
9,
CONVERT(CHAR(8), CURRENT_TIMESTAMP, 112)))

--
JAG


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 04:16 AM
Fred
 
Posts: n/a
Default Re: select between date

Thank you Erland and John. Perfect!

"John Gilson" <jag@acm.org> wrote in message
news:t_wAc.83252$mX.27772407@twister.nyc.rr.com...
> "Fred" <Fred@hotmail.com> wrote in message

news:INvAc.32931$sj4.2639@news-server.bigpond.net.au...
> > I am trying to get al the rows from table1 where datetime is between

9:00AM
> > yesterday and 9:00AM today if the time now is less than 9:00AM.

Otherwise it
> > should return all where datetime>9:00 AM today.
> >
> > Is this possible as a query in sql2000?

>
> Assume table T and datetime column dt.
>
> SELECT *
> FROM T
> WHERE DATEPART(HOUR, CURRENT_TIMESTAMP) < 9 AND
> dt BETWEEN
> DATEADD(HOUR,
> 9,
> CONVERT(CHAR(8),

CURRENT_TIMESTAMP - 1, 112))
> AND
> DATEADD(HOUR,
> 9,
> CONVERT(CHAR(8),

CURRENT_TIMESTAMP, 112))
> UNION ALL
> SELECT *
> FROM T
> WHERE DATEPART(HOUR, CURRENT_TIMESTAMP) >= 9 AND
> dt > DATEADD(HOUR,
> 9,
> CONVERT(CHAR(8),

CURRENT_TIMESTAMP, 112))
>
> or, alternatively,
>
> SELECT *
> FROM T
> WHERE (DATEPART(HOUR, CURRENT_TIMESTAMP) >= 9 OR
> (dt BETWEEN
> DATEADD(HOUR,
> 9,
> CONVERT(CHAR(8),

CURRENT_TIMESTAMP - 1, 112))
> AND
> DATEADD(HOUR,
> 9,
> CONVERT(CHAR(8), CURRENT_TIMESTAMP,

112))))
> AND
> (DATEPART(HOUR, CURRENT_TIMESTAMP) < 9 OR
> dt > DATEADD(HOUR,
> 9,
> CONVERT(CHAR(8),

CURRENT_TIMESTAMP, 112)))
>
> --
> JAG
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:59 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com