Solved

Invalid length parameter passed to the LEFT or SUBSTRING function

Posted on 2011-09-14
6
3,277 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

740 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