Click here to Skip to main content
15,921,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table in which Date time is stored in Varchar field in the fallowing format
DD-MMM-yyyy-HH:MM:SS, which is like 20-Feb-2013-17:23:34

So, now I wanted to use select query on this table for example,
let table be Employees and JoinDate is Varchar

Now my query look like below
SQL
SELECT EmployeeName,JoinDate FROM Employees WHERE JoinDate BETWEEN '20-Jan-2013-00:00:00' AND '20-Feb-2013-23:59:59'

But my query results unexpected results than what I expected.

Now my question is how to get the employees joined between '20-Jan-2013-00:00:00' AND '20-Feb-2013-23:59:59'.
Posted
v2

HI,


you have to change JoinDate datatype to datetime-- problem resolved, and many additional benefits.


YOU cant use between for varchar datatype like that..Its checking varchar data so that unaccepted results are there...

If you Like this solution, so please vote and mark as solution.
Thanks
Asp.Net C# Help Code[^]
Hemant Singh
 
Share this answer
 
Notice that the dash between the date and time may cause problems.

I suggest something like
SQL
CAST(STUFF(JoinDate,12,1,' ') AS DATETIME)


Notice that the 12 would change if the date does not have leading zero. 01-FEB-2013
 
Share this answer
 

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