Solved

Syntax for Where clause for dates

Posted on 2015-01-23
3
68 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 69

Accepted Solution

by:
Scott Pletcher earned 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

830 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