Hi I am using SP and passing a date field (@From) from the Frontend. Now i want to search all those records whose @From value is greater than or equalto @CreatedDate(field in the database) Here @CreatedDate is datetime field in the database And @From is the parameter that am passing in SP and its a varchar(50) datatype Pls help me Any help is greatly appreciated. Bye
If your field in the DB is datetime type why is your front end passing a varchar type? Shouldnt it be datetime too? Keeping datatypes consistent will maintain data integrity and avoid unnecessary screw ups converting back and forth. SELECT * FROM YourTable WHERE CreatedDate <= Convert(Datetime, @From)
You can use a string representation of a date to compare, really no need to cast or anything. One big issue is that you need to force either the US mm/dd/yyyy or the 'universal' yyyy-mm-dd format on the string. Also note that the date delimiter must be "-" or "/", and the time delimiter must be ":". Especially if you use a "." as date or time delimiter, you will often get a run-time error. Check out Frank Kalis's very complete article on datetime issues here on the main site.
<shameless plug> http://www.sql-server-performance.com/articles/dev/datetime_datatype_p1.aspx </shameless plug>
[quote user="FrankKalis"] <shameless plug> http://www.sql-server-performance.com/articles/dev/datetime_datatype_p1.aspx </shameless plug> [/quote] Please dont be modest []
Dinesh, that might turn out a performance killer, since an index on the date column could not be used.