简体   繁体   English

日期时间在 SQL 服务器中不起作用

[英]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在我的 SQL 服务器数据库中,日期时间值为

2011-07-19 19:37:50.727

I am using pipeten server in UK.我在英国使用 pipeten 服务器。 I am working on asp.net c#我正在研究 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 .我建议您首先不要对查询使用文本格式 - 使用参数化查询并将参数值指定为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.) (顺便说一句,这适用于所有数据值 - 参数化查询也有助于保护您免受 SQL 注入攻击。)

use the ISO dateformat (YYYYMMDD) it is safe across all languages使用 ISO 日期格式 (YYYYMMDD) 它在所有语言中都是安全的

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. where 子句中的日期是美国日期格式,但您的服务器在英国。 So you need to swap the month and date in the clause.因此,您需要交换子句中的月份和日期。 In UK, it's dd/mm/yyyy not mm/dd/yyyy在英国,它是 dd/mm/yyyy 而不是 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?您的列是varchar格式还是DateTime格式?

By the way working with date time values always use special operands/functions.顺便说一句,使用日期时间值总是使用特殊的操作数/函数。 Like:喜欢:

Msdn: DateTime Msdn:日期时间

Regards.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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