|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using the Between statementLadies / Gentlemen
I would like to find out if this is a valid use of the 'Between' Statement Select * From Bids Where GetDate() is Between StartDate and EndDate Mark Moss Absolutely. I've used a similar expression before. To demonstrate:
create table test4 (col1 datetime, col2 datetime) GO insert into test4 values (dateadd(hh, -1, getdate()), dateadd(hh, 1, getdate())) select * from test4 where getdate() between col1 and col2 -- Show quoteHide quote"msnews.microsoft.com" wrote: > Ladies / Gentlemen > > > I would like to find out if this is a valid use of the 'Between' Statement > > > Select * > From Bids > Where GetDate() is Between StartDate and EndDate > > > Mark Moss > > > Mark,
I think it is valid--but then, why wouldn't it be? Assuming you have the [pubs] database on your SQL box, you can successfully run this in SQL Query Analyzer, although I doubt there will be any qualifying rows: use pubs go select * from dbo.sales s where (getdate() between s.ord_date and s.ord_date) Robert Show quoteHide quote "msnews.microsoft.com" <markm***@adelphia.net> wrote in message news:%23bWFxE2LGHA.3260@TK2MSFTNGP11.phx.gbl... > Ladies / Gentlemen > > > I would like to find out if this is a valid use of the 'Between' Statement > > > Select * > From Bids > Where GetDate() is Between StartDate and EndDate > > > Mark Moss > > Technically, yes, perfectly valid. However, symantically correct is the
real question. If your values are defined like: 2001-01-01 00:00:00 2001-01-31 00:00:00 2001-01-31 00:00:00 2001-02-28 00:00:00 Where there exists a StartDate that equals an EndDate, then you will get overlaps. If you defined your endpoints as: 2001-01-01 00:00:00 2001-01-31 23:59:59.997 2001-02-01 00:00:00 2001-02-28 23:59:59.997 Assuming they are datetime values, then yes, you are good to go. -- Show quoteHide quote---------------------------------------------------------------------------- Louis Davidson - http://spaces.msn.com/members/drsql/ SQL Server MVP "Arguments are to be avoided: they are always vulgar and often convincing." (Oscar Wilde) "msnews.microsoft.com" <markm***@adelphia.net> wrote in message news:%23bWFxE2LGHA.3260@TK2MSFTNGP11.phx.gbl... > Ladies / Gentlemen > > > I would like to find out if this is a valid use of the 'Between' Statement > > > Select * > From Bids > Where GetDate() is Between StartDate and EndDate > > > Mark Moss > > Clean it up a bit: drop the "is", replace SELECT * in producicton code
and use the Standard CURRENT_TIMESTAMP now that you have it: SELECT <<list of columns>> FROM Bids WHERE CURRENT_TIMESTAMP BETWEEN start_date AND end_date; You just have to watch out for the time part of a DATETIME data type. You probably want the start_date to be set to 00:00:00.00 and the end_date to have "23:59:59.99" so you get the full days. I have a section in my datetime article that addresses range queries and why BETWEEN should be
avoided for datetime range searches: http://www.karaszi.com/SQLServer/info_datetime.asp -- Show quoteHide quoteTibor Karaszi, SQL Server MVP http://www.karaszi.com/sqlserver/default.asp http://www.solidqualitylearning.com/ Blog: http://solidqualitylearning.com/blogs/tibor/ "msnews.microsoft.com" <markm***@adelphia.net> wrote in message news:%23bWFxE2LGHA.3260@TK2MSFTNGP11.phx.gbl... > Ladies / Gentlemen > > > I would like to find out if this is a valid use of the 'Between' Statement > > > Select * > From Bids > Where GetDate() is Between StartDate and EndDate > > > Mark Moss > >
Other interesting topics
|
|||||||||||||||||||||||