Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL charindex throwing Invalid length parameter passed to the LEFT or SUBSTRING function because of period?

Tags:

sql

tsql

I've got the lines below in the where clause of my query, but I keep getting this error:

Msg 537, Level 16, State 3, Line 3
Invalid length parameter passed to the LEFT or SUBSTRING function.

    SUBSTRING(
        [email], 
        1, 
        CHARINDEX('@',[email])-1
    ) =
    SUBSTRING(
        [email], 
        CHARINDEX('@',[email])+1,
        CHARINDEX('.', [email])
    )

The error is originating from CHARINDEX('.', [email])

If I change the period to a letter, I don't get the error. Every record has a period in it, and even if one didn't, the charindex function would return 0, which wouldn't cause this error to throw. I must be missing something simple. Please help!

EDIT.

I tried throwing it inside an isnull, isnull(CHARINDEX('.', [email]), 1) just in case it was returning null for some reason, but that didn't work either.

like image 683
Anthony L Avatar asked Mar 30 '12 16:03

Anthony L


People also ask

How do I fix invalid length parameter passed to the left or substring function in SQL?

Solution / Work Around: This error message can easily be avoided by making sure that the integer value passed as the length to either the LEFT substring function or SUBSTRING string function is not negative. One way of checking it within the LEFT or SUBSTRING function is with the use of the CASE function.

What is invalid length parameter passed to the left or substring function?

This error is caused by passing a negative value to the length parameter of the SUBSTRING, LEFT and RIGHT string functions. This usually occurs in conjunction with the CHARINDEX function wherein the character being searched for in a string is not found and 1 is subtracted from the result of the CHARINDEX function.

How do you fix invalid length parameter passed to the right function?

To avoid this error, always make sure that you pass a non-negative value to the length parameter of the SUBSTRING, LEFT and RIGHT functions. If used in conjunction with the CHARINDEX function, you can use the NULLIF function together with the ISNULL function to check if the character separator is found.

What is substring and Charindex in the SQL Server?

The CHARINDEX() function returns the substring position inside the specified string. It works reverse to the SUBSTRING function. The substring() returns the string from the starting position however the CHARINDEX returns the substring position.


2 Answers

The error is originating from

CHARINDEX('@',[email])-1

If there is no @ symbol in the data, charindex returns 0. You subtract one from that to get -1, which is invalid in the substring function.

Try this instead.

CHARINDEX('@',[email] + '@')-1

This forces there to be a match, making sure CharIndex will always return a value >= 1, which will cause your substring function to succeed.

like image 70
George Mastros Avatar answered Oct 16 '22 15:10

George Mastros


I'm not sure this is your only problem. I am guessing that you are trying to look at the first part of an e-mail address and compare it with the first part of the domain. For example if the e-mail address is "[email protected]" then you are looking for "name" and "company". Substring does not take 2 positions, it takes a position and a length. Therefore to get "company" you would need to do this:

SUBSTRING(
    [email],
    CHARINDEX('@', [email]) + 1,
    CHARINDEX('.', [email]) - CHARINDEX('@', [email]) - 1
)

The +1 and -1 are to account for the fact that CHARINDEX would give you the position of the "@" so it would include the "@" in the result.

Unfortunately this won't always work because if you have an address like "[email protected]" then the position of the first "." will be less than the position of the "@" resulting in a negative number.

Therefore you will need to do this:

SUBSTRING(
    [email],
    CHARINDEX('@', [email]) + 1,
    CHARINDEX('.', [email], CHARINDEX('@', [email])) - CHARINDEX('@', [email]) - 1
)

This would make sure you are looking for the first "." after the "@". However, this still doesn't work if you don't have a "@" but you do have a "." (such as "invalidemail.companay.com"). Therefor you could do the solution above to add a '@' to the end, but a better way would be like this:

SUBSTRING(
    [email],
    CHARINDEX('@', [email]) + 1,
    CASE WHEN 
        CHARINDEX('.', [email], CHARINDEX('@', [email])) - CHARINDEX('@', [email]) - 1 < 0 
    THEN 0 
    ELSE 
        CHARINDEX('.', [email], CHARINDEX('@', [email])) - CHARINDEX('@', [email]) - 1
    END
)
like image 1
Doug Lampe Avatar answered Oct 16 '22 14:10

Doug Lampe