Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2004, 06:34 AM
Registered User
 
Join Date: Feb 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Raz Muhammad Send a message via Yahoo to Raz Muhammad
Default CurrentDate Comparison in Where clause fails why

this query doesnot return any row
where as all the current records are expected,can any body help me
in this scenario.

select * from table where datecolumn=getDate()

 
Old September 24th, 2004, 10:50 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi raz,

GetDate() returns a data and time accurate to some milli seconds. It's very unlikely you have records that match those milli seconds exactly hence you get no results.

One way to fix this is to convert the dates to a varchar and then compare both varchars. Something like this should work:

WHERE Cast(MyDate as varchar(20)) = Cast(GetDate() as varchar(20))

You may need to use Convert and a specific style instead to get both sites of the comparison in the same format.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Biscuit by Portishead (Track 9 from the album: Dummy) What's This?
 
Old September 24th, 2004, 05:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Raz,
Quote:
quote:where as all the current records are expected
Does that mean current DAY's records or current TIME's records? Hope youa re looking for today's records.

If so rephrase your query to this...
Code:
select * from table where convert(varchar(10),datecolumn,121) = convert(varchar(10),getDate(),121)
This would return today's records.

If you are looking for TIME specific resultset, then Imar's solution should be what you would stick with.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old September 25th, 2004, 03:31 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Vijay is absolutely right. With my solution, you'd get something like Sep 25 2004 10:29AM which also includes the time element.

The convert statement Vijay showed returns something like 2004-09-25
so you are effectively selecting records where the date matches....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old September 28th, 2004, 03:04 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

HI Raz,

Solutions from Vijay and Imar are the correct and perfect ways. In another way you can use .....

Select * from Table1 where day(DateCol)=day(Getdate())
And Month(dateCol)=Month(getdate())
AND Year (dateCol)=Year(getdate())

Hope u have now number of choices to select to fit in ur case.

Cheers


B. Anant





Similar Threads
Thread Thread Starter Forum Replies Last Post
comparison using a sequence pcase XSLT 1 December 3rd, 2007 07:33 PM
Date Comparison jroxit Classic ASP Databases 5 October 5th, 2007 05:39 PM
Name and Address Comparison subhanak Access VBA 3 May 16th, 2006 06:40 AM
Date comparison lily611 General .NET 5 January 4th, 2005 07:08 AM
Table comparison damnnono_86 Access 7 October 29th, 2003 10:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.