J. Davidson
J. Davidson

Reputation: 3307

Comparing current date in stored procedure

I am trying to check in a stored procedure a date in table if it is equal to Today's date.

  Code is 
    DECLARE @p0 datetime
    Set @p0 =GETDATE()
    Select * from testtable 
    where dateCol=@p0

This doesn't work it just gives empty rows. How can I accomplish that? Thanks

Upvotes: 1

Views: 1677

Answers (2)

Hart CO
Hart CO

Reputation: 34784

If dateCol is just the date, not DATETIME, you can use:

SELECT *
FROM Table
WHERE dateCol = CAST(GETDATE() AS DATE)

Upvotes: 1

Darren
Darren

Reputation: 70738

You need:

SET @p0 = CONVERT(CHAR(10),GETDATE(),103)  

GETDATE() returns the date and time. You probably have records with todays date but not at this exact time.

Also you don't really need to store it in @p0. You could use the expression directly in the WHERE clause.

Upvotes: 0

Related Questions