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 August 18th, 2003, 11:21 PM
Authorized User
 
Join Date: Jun 2003
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to arshad mahmood
Default GETDATE() dose not.........

Hello Sir
How can i retreive the records froem table where data =system date.
i have wrote the following query but it gives the error:
SELECT * FROM Table WHERE Table.[Date]=CURDATE();
it gives the syntax error.
Can Anyone guide me what is the problem with this.
I have also use the getdate() but it dose not retutn
any result.
Thanks



arshad mahmood
__________________
arshad mahmood
 
Old August 19th, 2003, 12:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

bc it matches the time also:
use
Code:
SELECT *
FROM Deputy
WHERE ApplyDate = CAST(GETDATE() AS varchar(12))

Always:),
Hovik Melkomian.
 
Old August 19th, 2003, 07:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

CURDATE() is not a valid SQL Server function, which is why you got the syntax error. Use Getdate() or Current_Timestamp (SQL-92 standard) instead.

If your query using GetDate() is something like:
Code:
    SELECT * FROM YourTable WHERE SomeDateColumn=GetDate();
then this will only select rows whose date column has the value of exactly 'right now', which is quite unlikely. Another poster suggested a WHERE Clause along the lines of:
Code:
 ... WHERE SomeDateColumn = CAST(GETDATE() AS varchar(12))
but this assumes that your date column is a character string datatype exactly formatted to the server's default date format. We leave aside the discussion for another day why storing dates this way is a bad idea.

Since columns of type datetime (or smalldatetime) store both the date and the time, and since 'Getdate()' or its equivalent, 'current_timestamp' return a datetime datatype, your WHERE clause is testing for equality of both the date and the time. Since it is unlikely that you have any rows inserted into your database with the time value of exactly right now, this is why you are not getting any rows returned.

I assume you only care about the date portion, and are interested in rows where the date column is equal to today's date, without regards to the time.

To do this, you need to ignore the time portion of the datetime column. There are a few ways to do this, but the most straightforward is to convert the datetime value to a string which is only long enough to hold the date portion of the value. You then convert the resultant string back to a datetime. Since the string only contains a date, the time is set to midnight by default. Thus, you set the time portion of the values to midnight, which effectively ignores the time:
Code:
    ...WHERE CAST(CONVERT(char(8),SomeDateColumn,112) as datetime) = CAST(CONVERT(char(8),GetDate(),112) as datetime);
The CONVERT function is SQL Server proprietary and takes a style parameter (the '112' above, see BOL) which indicates that the function should return datetime in the form 'yyyymmdd'. This string is CAST back to a datetime, thus setting the time to midnight. Since this is done for both sides of the equality test, the effect is that the date portions only are compared.



Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
onClick dose not work pecal Javascript How-To 4 November 24th, 2008 07:24 PM
getdate Not Working ~Bean~ SQL Server 2000 4 September 7th, 2005 11:31 PM
GETDATE? drachx SQL Server 2000 2 February 17th, 2005 08:05 AM
Using GETDATE in an In-Line function Mitch SQL Server 2000 13 March 25th, 2004 08:46 AM
seconds missing in getdate() stufflebeem SQL Server 2000 1 October 24th, 2003 04:52 PM





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