Solved

Query Problem With DatePart

Posted on 2008-10-13
3
210 Views
Last Modified: 2010-03-20
Hello,
I am attempting to filter my CaseNumber column (Text) by the first 2 digits by using the following SQL query and I keep getting an error.

SELECT     MAX(Mid(CaseNumber, 3, 6)) AS CaseValue
FROM         ServiceCalls
WHERE     (LEFT(CaseNumber, 2) = DatePart(yy, NOW()))
0
Comment
Question by:Gunit2507
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
PaulKeating earned 500 total points
ID: 22707110
LEFT() returns a string. DATEPART() returns an integer. I guess the message you are getting says "implicit conversion..."

Furthermore, DATEPART(yy ... ) returns a 4-digit year, not a 2-digit year as you seem to think: year and yyyy and yy  all mean the same thing.

GROUP BY CONVERT(INT, LEFT(CaseNumber, 2))
HAVING      CONVERT(INT(LEFT(CaseNumber, 2)) = (DatePart(yy, NOW())) % 100)

0
 
LVL 5

Assisted Solution

by:PaulKeating
PaulKeating earned 500 total points
ID: 22707136
Unmatched parens: fix is

GROUP BY CONVERT(INT, LEFT(CaseNumber, 2))
HAVING      CONVERT(INT, LEFT(CaseNumber, 2)) = (DatePart(yy, NOW()) % 100)
0
 

Author Comment

by:Gunit2507
ID: 22707228
This seems to work:

SELECT     MAX(Mid(CaseNumber, 3, 6)) AS CaseValue
FROM         ServiceCalls
WHERE     (LEFT(CaseNumber, 2) = RIGHT(DatePart('yyyy', NOW()), 2))
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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