Home All Groups Group Topic Archive Search About

Using the Between statement

Author
11 Feb 2006 11:16 PM
msnews.microsoft.com
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

Author
11 Feb 2006 11:29 PM
Mark Williams
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
>
>
>
Are all your drivers up to date? click for free checkup

Author
11 Feb 2006 11:38 PM
Robert Ellis
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
>
>
Author
12 Feb 2006 3:10 AM
Louis Davidson
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.

--
----------------------------------------------------------------------------
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)

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
>
>
Author
12 Feb 2006 6:15 AM
--CELKO--
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.
Author
12 Feb 2006 9:55 AM
Tibor Karaszi
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 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
>
>

Bookmark and Share