Solved

Invalid length parameter passed to the LEFT or SUBSTRING function

Posted on 2011-09-14
6
2,980 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
6 Comments
 
LVL 59

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 59

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 59

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 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