Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I have FrmDt and ToDt as datetime column in sql server2000. I am comparing dates in these columns with two datetimepicker like this-
SQL
SQL query- WHERE FrmDt>='" & dtp1.Value.Date & "' AND ToDt<='" & dtp2.Value.Date & "'


In Query Analyzer, I tried this FrmDt>='01/21/2015' AND ToDt<='01/21/2016'. Its giving me correct output. But I am not getting the output in Vb.net? Anything missing?

Thanks and regards
Posted
Updated 22-Mar-15 22:41pm
v2
Comments
Sujith Karivelil 23-Mar-15 4:25am    
I think the problem is with the different format dates in comparison.
Rizwan Gazi 23-Mar-15 4:27am    
I am displaying Dates in two DTPs like dd/MMM/yyyy
Kornfeld Eliyahu Peter 23-Mar-15 4:31am    
You should use parametrized query to resolve formatting problems (and also SQL injection)...

1 solution

Don't do it like that: compare date values as dates, not strings.
When you concatenate strings with dates in the date value is converted to a string in the current culture format, and the two strings are then compared character by character instead of using a date comparison.
That means the the first of July this year '01/07/2015' is before the second of January 1066 '02/01/1066' because the comparison stops at the first different character.

Instead, use parameterised queries, and pass through the DateTime values as DateTime parameters. The comparisons will then be Date based, instead of string based.
 
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