Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Finding Date in a Range

Author  Topic 

azamsharp
Posting Yak Master

201 Posts

Posted - 2006-10-06 : 11:01:46
Hi,

I want to select a result only if today's date lies between the startdate and endDate.

Like:

SELECT StartDate, EndDate
FROM Dates
WHERE GETDATE() BETWEEN StartDate AND EndDate

The above query does not work.


Mohammad Azam
www.azamsharp.net

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2006-10-06 : 11:21:22
The query should work if the datatypes of the columns StartDate and EndDate are in fact DateTime.

Try This:
CREATE TABLE #Dates
(
StartDate DATETIME,
EndDate DATETIME
)

INSERT INTO #Dates
SELECT
'2006-01-01',
'2007-01-01'
UNION ALL
SELECT
'2007-01-01',
'2008-01-01'
UNION ALL
SELECT
'2006-10-06',
'2006-10-07'
UNION ALL
SELECT
'2005-01-01',
'2006-01-01'

SELECT
StartDate,
EndDate
FROM
#Dates
WHERE
GETDATE() BETWEEN StartDate AND EndDate


What are the datatypes of the 2 columns?

Either that or your machine date is wrong.


Duane.
Go to Top of Page

azamsharp
Posting Yak Master

201 Posts

Posted - 2006-10-06 : 11:26:03
Got it!
Thanks :)

Mohammad Azam
www.azamsharp.net
Go to Top of Page
   

- Advertisement -