Date comparison

Date comparison

Post by vasy » Thu, 16 Jan 2003 02:57:51



How can I compare 2 dates? I have smaldatetime type of field in a table.
Although when I preview data in a table directly I see 1/1/1998, 12/1/2000
and so on. Stored procedure returns something like 1998-05-14 00:00:00.
I need to select only records with particular date from the table.
If I do comparison like this:
WHERE table.datefield=GETDATE()
my stored procedure returns nothing. It's because hours, minutes and seconds
are different.
I can use YEAR, MONTH and DAY functions with both dates, but I think there
is a simpler way to compare just dates.
Thank you
vasya
 
 
 

Date comparison

Post by Bob Barrow » Thu, 16 Jan 2003 03:17:27


Please look up an article entitled "Using Date and Time
data" in BOL.

GETDATE() returns the current date AND time. Since you
don't seem to be entering times into your column, simply
use CONVERT to extract the date portion from GETDATE() as
follows:
WHERE table.datefield= CONVERT(char(8),GETDATE(),112)

An implicit conversion will convert the result of the
CONVERT function to a datetime which can be compared with
what's in your column.

HTH,
Bob Barrows

Quote:>-----Original Message-----
>How can I compare 2 dates? I have smaldatetime type of
field in a table.
>Although when I preview data in a table directly I see
1/1/1998, 12/1/2000
>and so on. Stored procedure returns something like 1998-
05-14 00:00:00.
>I need to select only records with particular date from
the table.
>If I do comparison like this:
>WHERE table.datefield=GETDATE()
>my stored procedure returns nothing. It's because hours,
minutes and seconds
>are different.
>I can use YEAR, MONTH and DAY functions with both dates,
but I think there
>is a simpler way to compare just dates.
>Thank you
>vasya

>.


 
 
 

Date comparison

Post by Alejandro Mes » Thu, 16 Jan 2003 03:07:11


Try,

where table.datefield = cast(convert(char(8), getdate(), 112) as datetime)

AMB

 
 
 

Date comparison

Post by Nigel Rivet » Thu, 16 Jan 2003 03:07:15


where dte = convert(varchar(8), getdate(), 112)

Quote:>-----Original Message-----
>How can I compare 2 dates? I have smaldatetime type of
field in a table.
>Although when I preview data in a table directly I see
1/1/1998, 12/1/2000
>and so on. Stored procedure returns something like 1998-
05-14 00:00:00.
>I need to select only records with particular date from
the table.
>If I do comparison like this:
>WHERE table.datefield=GETDATE()
>my stored procedure returns nothing. It's because hours,
minutes and seconds
>are different.
>I can use YEAR, MONTH and DAY functions with both dates,
but I think there
>is a simpler way to compare just dates.
>Thank you
>vasya

>.

 
 
 

Date comparison

Post by Narayana Vyas Kondredd » Thu, 16 Jan 2003 03:10:03


Try something like this:

SELECT <Column List>
FROM <Table Name>
WHERE <Date Column> = CAST(CONVERT(char, GETDATE(), 112) AS datetime)

--
HTH,
Vyas, MVP (SQL Server)

http://vyaskn.tripod.com/


Quote:> How can I compare 2 dates? I have smaldatetime type of field in a table.
> Although when I preview data in a table directly I see 1/1/1998, 12/1/2000
> and so on. Stored procedure returns something like 1998-05-14 00:00:00.
> I need to select only records with particular date from the table.
> If I do comparison like this:
> WHERE table.datefield=GETDATE()
> my stored procedure returns nothing. It's because hours, minutes and
seconds
> are different.
> I can use YEAR, MONTH and DAY functions with both dates, but I think there
> is a simpler way to compare just dates.
> Thank you
> vasya

 
 
 

Date comparison

Post by Aaron Bertrand [MVP » Thu, 16 Jan 2003 03:08:14


WHERE
    dateCOLUMN >= CONVERT(CHAR(8), GETDATE(), 112)
    AND
    dateCOLUMN < CONVERT(CHAR(8), GETDATE()+1, 112)

--
Aaron Bertrand, SQL Server MVP
http://www.aspfaq.com/

Please reply in the newsgroups, but if you absolutely
must reply via e-mail, please take out the TRASH.


Quote:> How can I compare 2 dates? I have smaldatetime type of field in a table.
> Although when I preview data in a table directly I see 1/1/1998, 12/1/2000
> and so on. Stored procedure returns something like 1998-05-14 00:00:00.
> I need to select only records with particular date from the table.
> If I do comparison like this:
> WHERE table.datefield=GETDATE()
> my stored procedure returns nothing. It's because hours, minutes and
seconds
> are different.
> I can use YEAR, MONTH and DAY functions with both dates, but I think there
> is a simpler way to compare just dates.
> Thank you
> vasya

 
 
 

Date comparison

Post by Alejandro Mes » Thu, 16 Jan 2003 03:38:07


Could you post the following result?

select top 5 yourColDate
from yourTable
order by yourColDate

AMB

 
 
 

Date comparison

Post by Alejandro Mes » Thu, 16 Jan 2003 03:56:17


What about,

select *
from yourTable
where yourColDate = '19980625'

or



select *
from yourTable

any output?

AMB

 
 
 

Date comparison

Post by Aaron Bertrand [MVP » Thu, 16 Jan 2003 04:03:16


Quote:> When I exclude WHERE clause from my SP then I see records for let's say

3/10/2002 date.

Do you mean October 3, 2002? Or March 10, 2002?  Are you sure there are
records for today's date?

--
Aaron Bertrand, SQL Server MVP
http://www.aspfaq.com/

Please reply in the newsgroups, but if you absolutely
must reply via e-mail, please take out the TRASH.

 
 
 

Date comparison

Post by vasy » Thu, 16 Jan 2003 04:05:59


I'm sorry gentlemen
I just was so stupid. I'm working with remote SQL Server and I did not realized that GETDATE is running there too and although I change the system date on my local machine to the date of records I see directly in a table, GETDATE returns today's date and there are no records for today.
All statements I was given are working. It was my fault.
Sorry and thanks to all of you
vasya


  What about,

  select *
  from yourTable
  where yourColDate = '19980625'

  or



  select *
  from yourTable

  any output?

  AMB

 
 
 

Date comparison

Post by vasy » Thu, 16 Jan 2003 04:15:53


Thank you again and sorry. I was wrong with understanding GETDATE. See my
explanation in response to Alejandro
vasya



Quote:> > When I exclude WHERE clause from my SP then I see records for let's say
> 3/10/2002 date.

> Do you mean October 3, 2002? Or March 10, 2002?  Are you sure there are
> records for today's date?

> --
> Aaron Bertrand, SQL Server MVP
> http://www.aspfaq.com/

> Please reply in the newsgroups, but if you absolutely
> must reply via e-mail, please take out the TRASH.

 
 
 

Date comparison

Post by Mark » Thu, 16 Jan 2003 12:04:09


Here's another way nobody has thought of yet.

WHERE CAST(table.datefield AS int) = CAST(GETDATE() AS INT)

And this will probably execute a few milliseconds faster as well.

Mark


>How can I compare 2 dates? I have smaldatetime type of field in a table.
>Although when I preview data in a table directly I see 1/1/1998, 12/1/2000
>and so on. Stored procedure returns something like 1998-05-14 00:00:00.
>I need to select only records with particular date from the table.
>If I do comparison like this:
>WHERE table.datefield=GETDATE()
>my stored procedure returns nothing. It's because hours, minutes and seconds
>are different.
>I can use YEAR, MONTH and DAY functions with both dates, but I think there
>is a simpler way to compare just dates.
>Thank you
>vasya

 
 
 

Date comparison

Post by oj » Thu, 16 Jan 2003 12:22:47


Mark,

this is not what you really want. also, this will negate the benefit of an
index on your datetime column.

if date is all you care then cast(getdate() as int) will not suffice.

select CAST(cast('20030113' as datetime) AS int),
    CAST(GETDATE() AS INT)

--
-oj
http://www.rac4sql.net


Quote:> Here's another way nobody has thought of yet.

> WHERE CAST(table.datefield AS int) = CAST(GETDATE() AS INT)

> And this will probably execute a few milliseconds faster as well.

> Mark

 
 
 

Date comparison

Post by Steve Kas » Thu, 16 Jan 2003 12:32:08


Forgot to add:  CAST(CAST(GETDATE() AS FLOAT) AS INT) does truncate.
Good luck findout out from Books Online which CASTs round and which ones
truncate, though.

SK


>Mark,

>this is not what you really want. also, this will negate the benefit of an
>index on your datetime column.

>if date is all you care then cast(getdate() as int) will not suffice.

>select CAST(cast('20030113' as datetime) AS int),
>    CAST(GETDATE() AS INT)

 
 
 

Date comparison

Post by Steve Kas » Thu, 16 Jan 2003 12:31:10


You better hope he's not a morning person.

SK


>Mark,

>this is not what you really want. also, this will negate the benefit of an
>index on your datetime column.

>if date is all you care then cast(getdate() as int) will not suffice.

>select CAST(cast('20030113' as datetime) AS int),
>    CAST(GETDATE() AS INT)

 
 
 

1. SQL & Date comparisons

G'day everyone,

I am wanting to know if the following is possible;

SELECT name, date, time
FROM logfile
WHERE date (is in the same month as the current date ...???)

If you haven't noticed I need some help with the WHERE clause, is what I am
asking possible??

Can you help? Thankyou if you can!

Nath.

2. IUS9.13: SBSPACE full

3. Date comparison on SQL Server after 4 billion milliseconds

4. TO_DATE problem

5. Date Comparison

6. Problem installing SQL6.5 SP5a

7. Date comparison w/SQL Server 6.5

8. Oracle and Solaris

9. Date Comparison

10. Date comparison

11. Return Date Comparison as Boolean Column?

12. Date comparison in JOIN

13. Need help with date comparison.