all groups > sql server programming > january 2006 >
You're in the

sql server programming

group:

Picking rows based on getdate


Picking rows based on getdate stjulian
1/31/2006 9:30:52 PM
sql server programming: I have a very easy query that isn't working and is driving me mad.

I wish to select from a list of sales promotions that are current for this
day.

SELECT * FROM SALES WHERE PromoEffective>=GETDATE() AND
PromoExpires<=GETDATE()

The SALES table is defined with

PromoEffective - datetime(8)
PromoExpires - datetime(8)

Values for the row I am searching for

PromoEffective = '1/30/2006'
PromoExpires - '2/30/2006'
(note that there is no time)

and no matter what I do, I cant get it to get the SELECT to pick it up. I
get 0 rows.

I've CASTed and CONVERTed until I am dizzy. And this should be simple. Or,
am I falling for a common SQL programmer mistake?

Julian

Re: Picking rows based on getdate Aaron Bertrand [SQL Server MVP]
1/31/2006 9:48:59 PM
GETDATE() is a point in time, not just a date. So, for this day, you want
to say (I'm making some assumptions here):

DECLARE @dt SMALLDATETIME;

SET @dt = 0 + DATEDIFF(DAY, 0, GETDATE());

SELECT col_list FROM Sales
WHERE PromoExpires >= @dt;

If you say PromoEffective >= @dt AND PromoExpires <= @dt, you won't capture
the row you're looking for. If you want further help with your specific
data, please see http://www.aspfaq.com/5006 for some help on providing
useful specs.





[quoted text, click to view]

Re: Picking rows based on getdate Aaron Bertrand [SQL Server MVP]
1/31/2006 9:48:59 PM
GETDATE() is a point in time, not just a date. So, for this day, you want
to say (I'm making some assumptions here):

DECLARE @dt SMALLDATETIME;

SET @dt = 0 + DATEDIFF(DAY, 0, GETDATE());

SELECT col_list FROM Sales
WHERE PromoExpires >= @dt;

If you say PromoEffective >= @dt AND PromoExpires <= @dt, you won't capture
the row you're looking for. If you want further help with your specific
data, please see http://www.aspfaq.com/5006 for some help on providing
useful specs.





[quoted text, click to view]

Re: Picking rows based on getdate --CELKO--
2/1/2006 3:43:25 PM
SELECT *
FROM Sales
WHERE CURRENT_TIMESTAMP
BETWEEN promo_effective_date BETWEEN promo_expires_date;

Do not use SELECT * in produciton code. Now that you have the Standard
CURRENT_TIMESTAMP, stop using the old proprietary getdate(). Using the
BETWEEN will make your code much easier to read and will get the dates
in the right order.
Re: Picking rows based on getdate Hugo Kornelis
2/1/2006 11:42:03 PM
[quoted text, click to view]

Hi Julian,

In addition to Aaron's remarks, you also seem to have your logic the
wwrong way around.

WHERE PromoEffective>=GETDATE()

selects only promos that will be effective AFTER the current date/time,
i.e. in the future

AND PromoExpires<=GETDATE()

selects only promost that will expire BEFORE the current date/time, i.e.
in the past.

Unless you have some very strange data in your table, no single row will
ever match the combined criteria that the promo should be effective in
the future, yet be already expired.

--
Re: Picking rows based on getdate Hugo Kornelis
2/2/2006 10:49:10 PM
[quoted text, click to view]

Hi Joe,

But it will also make your code return incorrect results.

For date comparison, BETWEEN should be avoided.

SELECT Column1, Column2, ...
FROM Sales
WHERE CURRENT_TIMESTAMP >= promo_effective_date
AND CURRENT_TIMESTAMP < DATEADD(day, 1, promo_expires_date);

Or, if you have an index on the promo_expires_date and want to give the
optimizer the option to use it:

SELECT Column1, Column2, ...
FROM Sales
WHERE promo_effective_date <= CURRENT_TIMESTAMP
AND promo_expires_date > DATEADD(day, -1, CURRENT_TIMESTAMP);

(Though I have to admit that this one is quite un-selfdocumenting; a
better way would be to store the first day the promo is no longer
effective instead of the last day it is and use promo_expired_date >
CURRENT_TIMESTAMP)

--
AddThis Social Bookmark Button