?
Solved

Invalid length parameter passed to the LEFT or SUBSTRING function

Posted on 2011-09-14
6
Medium Priority
?
3,444 Views
Last Modified: 2012-05-12
hi there,
This SQL stament is giving me an error and i haven't been able to resolved it. any ideas?

Error:
Msg 537, Level 16, State 2, Line 18
Invalid length parameter passed to the LEFT or SUBSTRING function.


Drainage_District_Assessment data type Float


Thanks,
COHFL
SELECT 
 Case when 
 CAST(LEFT((Drainage_District_Assessment* 100),CHARINDEX('.',(Drainage_District_Assessment * 100))-1)as varchar(5)) = '0' 
  then '' else 
  'something'
 end
 FROM TAX_ROLL

Open in new window

0
Comment
Question by:COHFL
[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
6 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36539681
That typically means that a given string does not contain the character you are searching for in CHARINDEX() -- i.e., '.' -- so when you -1, it results in an invalid length.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36539698
You can usually fix this by wrapping the statement with a CASE CHARINDEX(...) WHEN 0 ...; however, it looks like you are doing this to determine if a number less the digits after the decimal place is 0 after multiplying it by 100. Therefore, this may be easier:

SELECT 
   CASE FLOOR(Drainage_District_Assessment * 100)
      WHEN 0 THEN ''
      ELSE 'something'
   END
FROM TAX_ROLL
;

Open in new window

0
 
LVL 13

Expert Comment

by:LIONKING
ID: 36539699
Try selecting just the second parameter of your LEFT function and see if it returns any negative numbers. Something like:

SELECT CHARINDEX('.',(Drainage_District_Assessment * 100))-1
FROM TAX_ROLL

If you get negatives, there's your error.
Let us know.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 36542609
First check length greater than minimum length  and then after apply the logic.

If fieldvalue length is less than the minimum length applied in sub string function then it will throw an error.
0
 

Author Comment

by:COHFL
ID: 36542909
mwvisa1 your suggestion worked but on my else statement i notice the same code gets used once again how can i accommodate your solution to the statement now?

here is the code:  
SELECT 
CASE FLOOR(Drainage_District_Assessment * 100)
  WHEN 0 then '' 
  else isnull(CAST(RIGHT('00000' + CAST(LEFT(Drainage_District_Assessment* 100,CHARINDEX('.',Drainage_District_Assessment* 100)-1)  as char(5)), 5) as CHAR(5)), '00000') 
end
FROM TAX_ROLL

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1000 total points
ID: 36543032
Try like this:

SELECT
CASE FLOOR(Drainage_District_Assessment * 100)
  WHEN 0 then ''
  ELSE CAST(RIGHT('00000'+CONVERT(VARCHAR(5), FLOOR(ISNULL(Drainage_District_Assessment, 0) * 100)), 5) AS CHAR(5))
END
FROM TAX_ROLL
;

Note the ISNULL() is done before the multiplication by 100, so that you will always end up with at least 0. With that in mind, should the initial CASE be:

CAST(CASE FLOOR(ISNULL(Drainage_District_Assessment, 0) * 100))
  WHEN 0 then '00000'

  ELSE RIGHT('00000'+CONVERT(VARCHAR(5), FLOOR(ISNULL(Drainage_District_Assessment, 0) * 100)), 5)
END AS CHAR(5))

Hope that helps!
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

752 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