?
Solved

Invalid length parameter passed to the LEFT or SUBSTRING function

Posted on 2011-09-14
6
Medium Priority
?
3,775 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
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 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.
Suggested Courses

850 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