Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 944
Zach Avatar asked Feb 04 '26 06:02

Zach


2 Answers

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

like image 145
Jon Skeet Avatar answered Feb 06 '26 19:02

Jon Skeet


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

SELECT * from Events_tbl where lastupdated > '20110718' and Venue=8
like image 39
SQLMenace Avatar answered Feb 06 '26 19:02

SQLMenace