Click here to Skip to main content
14,691,183 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 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 Anything missing?

Thanks and regards
Updated 22-Mar-15 23:41pm
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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