Solved

Syntax for Where clause for dates

Posted on 2015-01-23
3
64 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:
ScottPletcher 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now