日期未在sql server 2000中进行比较 [英] Date Not being compared in sql server 2000

查看:125
本文介绍了日期未在sql server 2000中进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我在sql server2000中将FrmDt和ToDt作为datetime列。我将这些列中的日期与两个datetimepicker进行比较,例如:

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 & "'





在查询分析器中,我尝试了这个FrmDt> ='01/21 / 2015'AND Tott< ='01/21/2016 。它给了我正确的输出。但是我没有在Vb.net中获得输出?有什么遗漏?



谢谢和问候



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

推荐答案

不要这样做:比较日期值作为日期,而不是字符串。

当您将日期值中的日期串联连接到当前文化格式的字符串时,然后逐个字符地比较两个字符串而不是使用日期比较。

这意味着今年7月1日'01 / 07/2015'是在1066年1月2日'02 / 01/1066'之前,因为比较在第一个不同的角色停止。



相反,使用参数化查询,并将DateTime值作为DateTime参数传递。然后比较将基于日期,而不是基于字符串。
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.


这篇关于日期未在sql server 2000中进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆