sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
mhk's Avatar

Date Compare in SQL Server Stored Procedure


Question posted by: mhk (Guest) on July 23rd, 2005 09:13 AM
Hi,

i have "req_date" column of "datetime" type in Database table besides
other columns.

From my Web page, i am calling the Stored Procedure with variable
parameter "Search_Date" of Varchar(60) type.

the value, i am passing to Stored procedure through "Search_Date" is
compared to req_date column of table.

My question is that how to do this comparision of date in WHERE part of
Select statement within Stored Procedure?

Thanks

3 Answers Posted
Simon Hayes's Avatar
Guest - n/a Posts
#2: Re: Date Compare in SQL Server Stored Procedure

Why are you using a varchar parameter when the correct data type is
datetime? I would fix the front end to use the proper data type, rather
than hack in something on the server side - it will also help to
prevent invalid data and SQL injection attacks.

If you really want to do it on the server, you can CAST() or CONVERT()
to datetime, but that's a workaround, not a solution.

Simon

David Portas's Avatar
Guest - n/a Posts
#3: Re: Date Compare in SQL Server Stored Procedure

Use a DATETIME parameter rather than a VARCHAR otherwise you'll just
have to convert the value. Don't rely on implict conversion for date
strings because it is subject to regional settings on the server and
the connection.

Assuming @search_date is a DATETIME:

WHERE req_date = @search_date

or, if you just what to search on the date and ignore the time:

WHERE req_date >= @search_date AND req_date <
DATEADD(DAY,1,@search_date)

--
David Portas
SQL Server MVP
--

Erland Sommarskog's Avatar
Erland Sommarskog July 23rd, 2005 09:13 AM
Guest - n/a Posts
#4: Re: Date Compare in SQL Server Stored Procedure

David Portas (REMOVE_BEFORE_REPLYING_dportas@acm.org) writes:[color=blue]
> Use a DATETIME parameter rather than a VARCHAR otherwise you'll just
> have to convert the value. Don't rely on implict conversion for date
> strings because it is subject to regional settings on the server and
> the connection.[/color]

It's worth pointing out here that the regional settings that David
are talking about are *not* those of Windows. Instead SQL Server
has its own settings that can affect how date literals are interpreted.




--
Erland Sommarskog, SQL Server MVP, Join Bytes!

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp
 
Not the answer you were looking for? Post your question . . .
196,844 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,844 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors