且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在sqlserver中显示当前日期记录?

更新时间:2022-11-08 23:18:20

不要将日期存储为字符串:不仅效率极低;当您尝试将它们与其他日期值进行比较时,它不仅会导致大量问题;它也很容易失败。

为什么?因为如果您有两台PC输入数据,那么您的数据库几乎肯定会受到PC配置的支配!如果您有一台PC输入设置为欧洲格式且一台设置为美国的数据,则日期字符串数据不可信:2012年2月6日06-02-12,或2012年6月6日,或12月12日2006年2月,或者......



你得到了这张照片吗?更改您的数据库。将日期存储为Date或DateTime列,并将其作为参数化查询从软件中传递出来。然后你可以使用正确的SQL:

Don't store dates as strings: not only is it extremely inefficient; not only does it cause massive problems when you try to compare them with other date values; it is also very prone to failure.
Why? Because if you have two PCs entering data, then your database will almost certainly be at the mercy of the PC configuration! If you have one PC entering data that is set to European format and one set to USA, then date string data cannot be trusted: is 06-02-12 the sixth of Feb 2012, or the second of June 2012, or the twelfth of Feb 2006, or...

You get the picture? Change your database. Store dates as Date or DateTime columns, and pass them through from your software as parameterised queries. Then you can use "proper" SQL:
SELECT * FROM MyTable where DATEDIFF(d, EntryDate, GETDATE()) = 0

返回今天的所有记录。