Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (6.1k points)

I am trying:

SELECT * 
FROM dbo.March2010 A
WHERE A.Date >= 2010-04-01;

 A.Date looks like: 2010-03-04 00:00:00.000

But, this is not working.

Can anybody give a reference for why?

1 Answer

0 votes
by (12.7k points)
edited by
select *  
from dbo.March2010 A 
where A.Date >= Convert(datetime, '2010-04-01' )

In this query, 2010-4-01 is treated as a mathematical expression. Therefore, in essence, it reads

select *  
from dbo.March2010 A 
where A.Date >= 2005; 

(2010 minus 4 minus 1 is 2005, converting this to a proper datetime and adding single quotes will solve this problem.)

So the parser here might allow you to get away with

select *  
from dbo.March2010 A 
where A.Date >= '2010-04-01'

It will be doing the conversion for you, but in my view, it is less readable than explicitly converting to a datetime for the maintenance programmer that will come after you. 

Want to be an expert in SQL programming? Join Intellipaat's SQL Certification course and learn in depth.

Related questions

Browse Categories

...