简体   繁体   中英

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()

Effect: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

Using {0:dd.MM.yyyy}

Effect: none

Using CONVERT(varchar(20), GetDate(), 104)

Effect: that works!

Thanks!

Description

I would not convert to a varchar and doing string comparrisson. The performance is much better if you compare trainstartdate using the >= and < .

You can use the T-SQL getDate() method to get the current date.

getDate() returns the current datetime with the time. 2012-02-14 14:51:08.350

DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) return only the current date. `2012-02-14 00:00:00.000

DATEADD(dd, 1, DATEDIFF(dd, 0, GETDATE())) returns only the date of tomorow. 2012-02-15 00:00:00.000

Sample

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.

More Information

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.

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.

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:

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).

If trainstartdate contains the start date/time, you can get all of today's trains as follows:

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.

试试这个.. YYYY应该是小写字母yyyy

String.Format("{0:dd.MM.yyyy}", DateTime.Now)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM