如何比较数据库日期时间与currentdate,只需要匹配日期而不是时间 [英] How compare database datetime to currentdate ,need to match only with date not time
问题描述
您好使用名为OrderMaster列BookDate(nvarchar(50))的表以datetime格式存储数据(即2013年下半年01:25)。所以我只需要与currentdate(来自aspxdateedit)进行比较控制)没有时间在数据库中,所以请帮我急。我需要将我的代码上传到服务器。
[edit]删除亵渎(甚至误导)[/ edit]
提前致谢..............
Hi Am using a table called OrderMaster column BookDate(nvarchar(50)) stored data in datetime format(i.e,22/03/2013 01:25 PM).so i need to compare only with currentdate(which coming from aspxdateedit control ) not with time which is in database,so kindly help me its urgent.i need to upload my code to server.
[edit]removed profanity(mispelled even)[/edit]
Thanks in advance..............
推荐答案
首先要注意的是,字符串格式的strongin日期是一个坏主意 - 总是使用适当的格式,或者当你进行任何比较时它都不能正常工作。
我会将数据库更改为使用DateTime或Date格式而不是字符串 - 然后比较是微不足道的。
但如果你不能,你会必须要将字符串转换为日期(这是一个痛苦,因为日期不是以ISO格式存储),然后才能转换,或者使用子字符串你的基于文本的日期,并依赖于字符串比较 - 这只适用于平等,而不是其他任何东西。
更改数据库 - 它节省了很多麻烦在将来。
The first thing to note is that strongin dates in string format is a bad idea - always use the appropriate format or it doesn''t work well when you come to do any comparisons.
I would change the database to use a DateTime or Date format instead of a string - then teh comparison is trivial.
But if you can''t, you will have to either convert the string to a Date (which is a pain since the date is not stored in ISO format) before you can convert, or use a substring of your text based date and rely on a string comparison - which will only work well for equals, not for anything else.
Change the database - it saves a lot of hassle in the future.
正如OriginalGriff所写:表的设计不好。将BookDate更改为DATETIME类型。
请参阅 CAST和CONVERT功能 [ ^ ]。
在SQL Server 2005中,您需要拒绝时间部分,如下所示:
As OriginalGriff wrote: bad design of table. Change BookDate to DATETIME type.
See CAST and CONVERT functions[^].
In SQL Server 2005, you need to reject time part as is shown below:
DECLARE @myDate DATETIME
SET @myDate = GETDATE()
SELECT @myDate AS FullDate, CONVERT(DATETIME, CONVERT(VARCHAR(10), @myDate,121)) AS ShortDate
对于较新的版本,请参阅: http://msdn.microsoft.com/en-us/library/ms186724%28v=sql.100%29.aspx [ ^ ]
这篇关于如何比较数据库日期时间与currentdate,只需要匹配日期而不是时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!