[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Syntax for Where clause for dates

Posted on 2015-01-23
3
Medium Priority
?
79 Views
Last Modified: 2015-01-23
I have a syntax error in the following and clause.

I am trying to say where OrderDateTime is >= '2014-01-01' and OrderDateTime < '2015-01-01'


(dbo.OeOrders.OrderDateTime >= "'"+YEAR(getdate())-1 + '-01-01' AND      dbo.OeOrders.OrderDateTime < "'"+YEAR(getdate()) +'-01-01') 

Open in new window



thanks

Glen
0
Comment
Question by:GPSPOW
3 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40567145
(dbo.OeOrders.OrderDateTime >= CAST(YEAR(getdate())-1 AS varchar(4)) + '0101' AND
 dbo.OeOrders.OrderDateTime < CAST(YEAR(getdate()) AS varchar(4)) + '0101'

Format 'YYYYMMDD' is always interpreted correctly, whereas YYYY-MM-DD requires specific date/language settings to work properly.
0
 

Author Closing Comment

by:GPSPOW
ID: 40567221
Thanks

Works great!
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 40567222
You could also write:
YEAR(dbo.OeOrders.OrderDateTime) >= YEAR(GETDATE()) - 1
AND YEAR(dbo.OeOrders.OrderDateTime) < YEAR(GETDATE())

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question