Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

error

Posted on 2008-10-16
16
Medium Priority
?
173 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
ID: 22731179
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
ID: 22731182
it means the length you pass to the left / right functions is illegal
0
 
LVL 6

Expert Comment

by:mirzas
ID: 22731199
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Mr_Shaw
ID: 22731406
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
ID: 22731462
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
ID: 22731479
The query I sent you does not work?
0
 

Author Comment

by:Mr_Shaw
ID: 22731542
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
ID: 22731572
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
 
LVL 6

Expert Comment

by:mirzas
ID: 22731579
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
ID: 22731623
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
ID: 22731706
This code still does not group the months together
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22731758
mine does.
0
 
LVL 6

Accepted Solution

by:
mirzas earned 2000 total points
ID: 22731862
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
ID: 22731873
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
ID: 22731929
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
ID: 31506744
Thanks, it worked fine :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

971 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