Solved

error

Posted on 2008-10-16
16
163 Views
Last Modified: 2012-05-05
I have a data set which I need to count the monthly entries. For example

Jan-08   1
Feb-08   2
Mar-08   3

To do this I have put together some code which like this;

SELECT     CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20)) AS Month, COUNT([Rdate]) AS Rs
FROM         table1
GROUP BY [Rdate]
ORDER BY RIGHT([Rdate], 4), CAST(LEFT([Rdate], CHARINDEX('-', [Rdate]) - 1) AS INT)

However I am getting a error message which looks like this;

Invalid lenght passed to SUBSTRING function.

I am not sure what is wrong?
0
Comment
Question by:Mr_Shaw
  • 5
  • 5
  • 5
  • +1
16 Comments
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
try this:

SELECT     CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20)) AS Month, COUNT([Rdate]) AS Rs
FROM         table1
GROUP BY [Rdate]
ORDER BY RIGHT([Rdate], 4), CAST(LEFT([Rdate],
case when CHARINDEX('-', [Rdate]) - 1) >= 2 then CHARINDEX('-', [Rdate]) - 1)  else 0 end
AS INT)
0
 
LVL 37

Expert Comment

by:momi_sabag
Comment Utility
it means the length you pass to the left / right functions is illegal
0
 
LVL 6

Expert Comment

by:mirzas
Comment Utility
Try this:



SELECT     CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20)) AS Month, COUNT([Rdate]) AS Rs
FROM         table1
GROUP BY [Rdate]
ORDER BY [Rdate]

0
 

Author Comment

by:Mr_Shaw
Comment Utility
I used the code

SELECT     CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20)) AS Month, COUNT([Rdate]) AS Rs
FROM         table1
GROUP BY [Rdate]
ORDER BY RIGHT([Rdate], 4), CAST(LEFT([Rdate],
case when CHARINDEX('-', [Rdate]) - 1) >= 2 then CHARINDEX('-', [Rdate]) - 1)  else 0 end
AS INT)

I am getting the following error

An expression of non-boolean type specified in a context where a condition is expected, near ')'

Not sure what that really means?
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
whoops....try this:

SELECT     CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20)) AS Month, COUNT([Rdate]) AS Rs
FROM         table1
GROUP BY [Rdate]
ORDER BY RIGHT([Rdate], 4),
CAST(LEFT([Rdate],
case when CHARINDEX('-', [Rdate]) >= 2 then CHARINDEX('-', [Rdate])-1  else 0 end)
AS INT)
0
 
LVL 6

Expert Comment

by:mirzas
Comment Utility
The query I sent you does not work?
0
 

Author Comment

by:Mr_Shaw
Comment Utility
Thanks the code work,

However things are not being grouped together properly. An example of my results are;

Month           Rs
1-2007      2
5-2007      1
5-2007      1
5-2007      1
6-2007      1

As you can see 5-2007 has three seperate entries.

0
 
LVL 6

Expert Comment

by:mirzas
Comment Utility
Try this



SELECT     CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20)) AS Month, COUNT([Rdate]) AS Rs
FROM         table1
GROUP BY [Rdate]
ORDER BY CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20)) AS Month
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Expert Comment

by:mirzas
Comment Utility
whoops...
this one.. sorry.

SELECT     CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20)) AS Month, COUNT([Rdate]) AS Rs
FROM         table1
GROUP BY [Rdate]
ORDER BY CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20))
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
no problem

SELECT     CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20)) AS Month, COUNT([Rdate]) AS Rs
FROM         table1
GROUP BY CAST(MONTH([Rdate]) AS VarChar(20)) + '-' + CAST(YEAR([Rdate]) AS VarChar(20))
ORDER BY RIGHT([Rdate], 4),
CAST(LEFT([Rdate],
case when CHARINDEX('-', [Rdate]) >= 2 then CHARINDEX('-', [Rdate])-1  else 0 end)
AS INT)
0
 

Author Comment

by:Mr_Shaw
Comment Utility
This code still does not group the months together
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
mine does.
0
 
LVL 6

Accepted Solution

by:
mirzas earned 500 total points
Comment Utility
SELECT CAST(YEAR([Rdate]) AS VarChar(20)) + '-' + CAST(MONTH([Rdate]) AS VarChar(20)) AS Month, Count(*)  from
table1
GROUP BY CAST(YEAR([Rdate]) AS VarChar(20)) + '-' + CAST(MONTH([Rdate]) AS VarChar(20))
ORDER BY CAST(YEAR([Rdate]) AS VarChar(20)) + '-' + CAST(MONTH([Rdate]) AS VarChar(20))


This one works fine.
0
 

Author Comment

by:Mr_Shaw
Comment Utility
I am now getting an aggregation error saying that Rdate is not part of order by or group by
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
try this:

select MonthVal, count(*)
from
(
SELECT  CAST(MONTH([Rdate]) AS VarChar(20)) + '-'
        + CAST(YEAR([Rdate]) AS VarChar(20)) AS MonthVal,
RIGHT([Rdate], 4),
        CAST(LEFT([Rdate],
                  case when CHARINDEX('-', [Rdate]) >= 2
                       then CHARINDEX('-', [Rdate]) - 1
                       else 0
                  end) AS INT) AS OrderByCol, RDate
FROM    table1
) a
GROUP BY MonthVal
ORDER BY MIN(OrderByCol)
0
 

Author Closing Comment

by:Mr_Shaw
Comment Utility
Thanks, it worked fine :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

772 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

11 Experts available now in Live!

Get 1:1 Help Now