Solved

Syntax for Where clause for dates

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parse field in SQL View 15 96
SQL Select - Help with CASE 4 41
SQL Syntax join to include values from first table 3 25
SQL Query Syntax Join 4 25
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

932 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

8 Experts available now in Live!

Get 1:1 Help Now