简体   繁体   中英

DateTime in not working in SQL Server

From my database I am trying to fetch all records after a particular date. For this I am using the query

SELECT * 
FROM Events_tbl 
WHERE lastupdated > '07-18-2011' and Venue=8

This particular piece of code is working in my localhost. But when I upload to server it is not returning anything. Could someone help please?

I am really stuck.

In my SQL Server database the datetime value is

2011-07-19 19:37:50.727

I am using pipeten server in UK. I am working on asp.net c#

Thanks

I suggest you don't use a text format for your query in the first place - use a parameterized query and specify the parameter value as a DateTime . That way you don't need to worry about formatting at all, and you keep your code away from your data.

There's no reason for you to perform a conversion either to or from text here - so don't. Keep the values in their most appropriate data type.

(This goes for all data values, by the way - and parameterized queries also help to protect you from SQL injection attacks.)

use the ISO dateformat (YYYYMMDD) it is safe across all languages

SELECT * from Events_tbl where lastupdated > '20110718' and Venue=8

The date in the where clause is in US date format but your server is in UK. So you need to swap the month and date in the clause. In UK, it's dd/mm/yyyy not mm/dd/yyyy

Most probabbly your local machine localization is different from servers one, also look on request you make by using string litterals. Is your column in varchar format, or in DateTime format?

By the way working with date time values always use special operands/functions. Like:

Msdn: DateTime

Regards.

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