Solved

Invalid length parameter passed to the LEFT or SUBSTRING function

Posted on 2011-09-14
6
3,181 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

821 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