Click here to Skip to main content
15,902,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I have a string currentDatefrom and currentDateTo which captures the textbox in asp.net.
date is entered in ddmmyyyy format.

sql 2005 db has currentDAte column as varchar(10) in the dd-mm-yyyy format.

I would like to pass this to sql to build query like
select * from tblcustomers where currentDAte between @currentDATefrom and @currentDATeto.

In order for sql to return correct values ...I will need to convert strings currentDATefrom and currentDAteto to datetime in the dd-mm-yyyy format and reconvert it back to string and pass that in.

can anyone point me to any link that can help me do the conversion from string to datetime...I tried several of these and doesnt seem to work.
Posted
Comments
Sinisa Hajnal 19-Nov-14 2:15am    
Change your database. DO NOT EVER store dates as strings. All you get is inability to DATEDIFF and ORDER BY for only slight convenience of storing the date directly from the UI and in your case not even that. Take the input, turn it into DATETIME object and store it in a database.

It will prevent at least one conversion (of your currentDate column) and might considerably shorten your development time (make scalar function that returns date from your known input format and simply use it wherever you need a conversion
Mycroft Holmes 19-Nov-14 3:51am    
Listen to Sinisa, do NOT ignore this advice it is essential to your continuing sanity!

Hi Please try this one
SQL
select * from tblcustomers where  convert(datetime,currentDAte,105) 
between convert(datetime,STUFF(STUFF(@currentDATefrom,3,0,'-'),6,0,'-'),105)  
and convert(datetime,STUFF(STUFF(@currentDATeto,3,0,'-'),6,0,'-'),105)  

This is not a preferable solution please make your database change as per above comments.
 
Share this answer
 
As Sinisa pointed out, your design is bad. You should not store dates as strings in your database. However have a look at CONVERT[^] function. From all the formats 105 is the right one for you:
SQL
CONVERT(DATE, '30-11-2014', 105)

Assuming your parameters are dates and not strings the query should look like this:
SQL
select * from tblcustomers where CONVERT(DATE, currentDAte, 105) between @currentDATefrom and @currentDATeto

Please note that this will prevent query engine to use any indexes on currentDate column so keep an eye on performance. You should really consider changing your database. It will make your like much easier.
 
Share this answer
 
Try this.


SQL
select * 
from tblcustomers 
where Convert(Date,currentDAte,103) 
       between Convert(Date,@currentDATefrom,103)
             and Convert(Date,@currentDATeto,103)


You can check CAST and CONVERT [^]

SQL Server Functions that helps to convert date and time ...[^]
 
Share this answer
 
v2

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