From: Luigi on
Hi all,
having a table in SQL Server 2000 with a DateTime field and some values like:

ReferenceDate

2009-05-26 22:30:00.000
2009-05-27 12:30:00.000
2009-05-27 22:10:00.000
2009-05-30 22:40:00.000
2009-05-31 14:55:00.000

how can I write a Select that gets records that have DateReference =
getdate()?

So records where this field fall inside actual date.

Thanks in advance.

Luigi

From: Uri Dimant on
Hi
SELECT * FROM tbl WHERE ReferenceDate>=GETDATE() AND
ReferenceDate<DATEADD(d,1,GETDATE())

Note if you have huge table to check for ranges ,created clustered index
on ReferenceDate




"Luigi" <Luigi(a)discussions.microsoft.com> wrote in message
news:7EA7E91C-7898-478F-A973-1C1D3A6A8034(a)microsoft.com...
> Hi all,
> having a table in SQL Server 2000 with a DateTime field and some values
> like:
>
> ReferenceDate
>
> 2009-05-26 22:30:00.000
> 2009-05-27 12:30:00.000
> 2009-05-27 22:10:00.000
> 2009-05-30 22:40:00.000
> 2009-05-31 14:55:00.000
>
> how can I write a Select that gets records that have DateReference =
> getdate()?
>
> So records where this field fall inside actual date.
>
> Thanks in advance.
>
> Luigi
>


From: Luigi on
"Uri Dimant" wrote:

> Hi
> SELECT * FROM tbl WHERE ReferenceDate>=GETDATE() AND
> ReferenceDate<DATEADD(d,1,GETDATE())
>
> Note if you have huge table to check for ranges ,created clustered index
> on ReferenceDate

Perfect, thank you Uri.

Luigi
From: Plamen Ratchev on
Since you have time portion different than midnight, you have to use half-open interval like this:

SELECT ReferenceDate
FROM Table
WHERE ReferenceDate >= DATEADD(DAY, DATEDIFF(DAY, '20010101', CURRENT_TIMESTAMP), '20010101')
AND ReferenceDate < DATEADD(DAY, DATEDIFF(DAY, '20010101', CURRENT_TIMESTAMP), '20010102');

--
Plamen Ratchev
http://www.SQLStudio.com
From: Luigi on
A little problem.
Making this script:

Select DATEADD(day, DATEDIFF(day, 0, '18/01/2010 23:00:00'), 0)

I obtain this error:

Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value.

How can I solve?

Luigi