Solved

Query Problem With DatePart

Posted on 2008-10-13
3
207 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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