Solved

error

Posted on 2008-10-16
16
164 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
 

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 500 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

910 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