Click here to Skip to main content
15,902,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a table which contains date field .the datatype of the field is varchar.I need to compare these dates in one query also in another query i need to calculate previous date from current date also.how i should write this query?it gives me conversion error.

my query is as follows without converting any date fields to datetime format of sql server.

SQL
select SuppliedDate,VrNo,OrderNo, (First_name + ' ' + Last_name) as Name,DoB,Gender,ShippingType,Address,City,State,Country,ZipCode,DrName,PName,vr_date,Status,Remark
from TransactionMaster where
((SuppliedDate=null and (vr_date >=  CONVERT(CHAR(12), DATEADD(dd, -1, '11/30/2013'), 112) AND vr_date <= '11/30/2013')) and AdminEdit='0')


It doesnt return me any rows.
if i convert fields to datetime format it gives me conversion error.
Posted
Updated 16-Dec-13 23:14pm
v2
Comments
An@nd Rajan10 17-Dec-13 4:45am    
please write your code and mention which line have error,that is helpful for get currct solution

There are several reasons to not store datetime value as varchar.

Try to use CONVERT[^] together with Date And Time[^] functions.
 
Share this answer
 
Comments
thatraja 17-Dec-13 4:56am    
5!
Maciej Los 17-Dec-13 4:58am    
Thank you ;)
Yes, it will.

Do not store dates as strings: that is what the DateTime datatype is there for.

You can't compare dates stored as strings without converting them to DateTime first: if you try you will get a string comparison, which compares character by character. And converting strings to dates on the fly normally just exposes that your data is not consistent or in the right format - but too late to do anything useful about it.

Always convert user entered date strings to DateTime values as early as possible, and work with the DateTime value from then on, converting back to a string only at the last minute for presentation back to the user.
 
Share this answer
 
Comments
thatraja 17-Dec-13 4:56am    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900