[英]Sql query with the current date
I've got a simple query where I want to put the current date 我有一个简单的查询,我想把当前日期
var query = @"
SELECT trainid, trainnum
FROM trains
WHERE CONVERT(varchar(10), trainstartdate, 104)=" +
" " +
// so that matches the '104' format
String.Format("{0:dd.MM.YYYY}", DateTime.Now) +
" " +
"ORDER BY trainnum";
But when running I get the error message: 但是在运行时我收到错误消息:
Cannot call methods on numeric. .Net SqlClient Data Provider
How do I specify current date the right way? 如何以正确的方式指定当前日期? Thanks!
谢谢!
Using GETDATE() 使用GETDATE()
Effect: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
效果:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
Using {0:dd.MM.yyyy} 使用{0:dd.MM.yyyy}
Effect: none
效果:
none
Using CONVERT(varchar(20), GetDate(), 104) 使用CONVERT(varchar(20),GetDate(),104)
Effect: that works!
效果:
that works!
Thanks! 谢谢!
I would not convert to a varchar and doing string comparrisson. 我不会转换为varchar并执行字符串比较。 The performance is much better if you compare
trainstartdate
using the >=
and <
. 如果使用
>=
和<
来比较trainstartdate
,性能要好得多。
You can use the T-SQL
getDate()
method to get the current date. 您可以使用
T-SQL
getDate()
方法获取当前日期。
getDate() returns the current datetime with the time.
getDate()返回当前日期时间。
2012-02-14 14:51:08.350
DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) return only the current date.
DATEADD(dd,0,DATEDIFF(dd,0,GETDATE()))仅返回当前日期。 `2012-02-14 00:00:00.000
`2012-02-14 00:00:00.000
DATEADD(dd, 1, DATEDIFF(dd, 0, GETDATE())) returns only the date of tomorow.
DATEADD(dd,1,DATEDIFF(dd,0,GETDATE()))仅返回tomorow的日期。
2012-02-15 00:00:00.000
var query = @"
SELECT trainid, trainnum
FROM trains
WHERE trainstartdate >=
-- today
DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
AND trainstartdate <
-- tommorow
DATEADD(dd, 1, DATEDIFF(dd, 0, GETDATE()))
ORDER BY trainnum"
Note: If you want to be ANSI compliant, CURRENT_TIMESTAMP does the same. 注意:如果您希望符合ANSI标准, CURRENT_TIMESTAMP也会这样做 。
GETDATE()就是你所需要的......
I think 我认为
String.Format("{0:dd.MM.YYYY}", DateTime.Now);
is returning the date with a dot, which makes SQL consider it as a number. 使用点返回日期,这使得SQL将其视为数字。
Try using 尝试使用
String.Format("{0:MM/dd/yyyy}", DateTime.Now);
with a /
instead. 用
/
代替。
var query = @"
SELECT trainid, trainnum
FROM trains
WHERE CONVERT(varchar(10), trainstartdate, 104)=
CONVERT(varchar(20), GetDate(), 104)
ORDER BY trainnum";
将YYYY的格式模式更改为小写字母
{0:dd.MM.yyyy}
You need to be aware that GETDATE() returns the current date and time of day, not only today's date. 您需要知道GETDATE()返回当前日期和时间,而不仅仅是今天的日期。
If you want to return rows matching today's date, you need to extract the date part. 如果要返回与今天日期匹配的行,则需要提取日期部分。 There are a number of ways to do this - eg with SQL Server 2008 you can use the
DATE
data type, but one general way that works with earlier versions of SQL Server is the following: 有很多方法可以做到这一点 - 例如,使用SQL Server 2008,您可以使用
DATE
数据类型,但是与早期版本的SQL Server一起使用的一种通用方法如下:
CONVERT(DATETIME, convert(VARCHAR(10),getdate(),101) )
You can then use the query: 然后,您可以使用查询:
SELECT trainid, trainnum
FROM trains
WHERE trainstartdate = CONVERT(DATETIME, convert(VARCHAR(10),getdate(),101) )
which will work provided you are sure that the date/time in the trains.trainstartdate column is a date only (time of day = 0). 只要您确定train.trainstartdate列中的日期/时间仅为日期(时间= 0),这将起作用。
If trainstartdate contains the start date/time, you can get all of today's trains as follows: 如果trainstartdate包含开始日期/时间,您可以按如下方式获取今天的所有列车:
SELECT trainid, trainnum
FROM trains
WHERE trainstartdate >= CONVERT(DATETIME, convert(VARCHAR(10),getdate(),101) )
AND trainstartdate < DATEADD(dd,1, CONVERT(DATETIME, convert(VARCHAR(10),getdate(),101) ))
By doing it like this rather than converting to a string, you will take advantage of any index there may be on the trainstartdate column. 通过这样做而不是转换为字符串,您将利用trainstartdate列上可能存在的任何索引。
试试这个.. YYYY应该是小写字母yyyy
String.Format("{0:dd.MM.yyyy}", DateTime.Now)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.