Solved

error

Posted on 2008-10-16
16
167 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
[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
  • 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
find SQL job run average duration 24 54
query analyser in sql server 2016 express edition 2 25
Regarding Disk IO 3 42
Trigger C# code inside the SQL Server 6 29
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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