Connecting Tech Pros Worldwide Help | Site Map

Date Compare in SQL Server Stored Procedure

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 08:13 AM
mhk
Guest
 
Posts: n/a
Default Date Compare in SQL Server Stored Procedure

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


  #2  
Old July 23rd, 2005, 08:13 AM
Simon Hayes
Guest
 
Posts: n/a
Default 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

  #3  
Old July 23rd, 2005, 08:13 AM
David Portas
Guest
 
Posts: n/a
Default 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
--

  #4  
Old July 23rd, 2005, 08:13 AM
Erland Sommarskog
Guest
 
Posts: n/a
Default 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, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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 220,662 network members.