SQL: Invalid length parameter passed to the LEFT or SUBSTRING function - sql

I am getting the following error while trying to execute the below query:
'Invalid length parameter passed to the LEFT or SUBSTRING function'
select A.NUMBER,
SUBSTRING(A.DESCRIPTION,
CHARINDEX(','+CAST(A.Description as VARCHAR(255)) + ',','from')+4,
CHARINDEX(','+CAST(A.Description as VARCHAR(255))+ ',','to')- 1)
from dbo.ACTIVITYM1 A
where A.DESCRIPTION like 'Reassignment from%'
Schema Details:
Activitym1 : Table Name
Description : Column
Number : Column.
Kindly let me know, what is the cause of this error.
A.Description contains something like below
"Reassignment from PSM_Support to PPM_Support"
I am trying to get PSM_Support in 1 column and PPm_Support in another column.

Here's your substring broken out:
SELECT SUBSTRING(
A.DESCRIPTION,
CHARINDEX(','+CAST(A.Description as VARCHAR(255))+',','from')+4, -- start
CHARINDEX(','+CAST(A.Description as VARCHAR(255))+ ',','to')- 1 -- length
)
I would check on the third parameter (length) by putting it into its own column. It must be > 0.

You have understood the syntax of CHARINDEX incorrectly. You have to search 'from' or 'to' in Description but not Description in 'from' or 'to'. I hope you understood the issue here. Just reverse them and you will get it correct.

Related

SQL join with concat and substring

So I'm attempting to do a join that requires both CONCAT and SUBSTRING.
Table one has a column with a date and location e.g. '02:00 IND'
Table two has a column with date/time e.g. '2020-10-10 02:00:00.000000' and another column with location e.g. 'IND'.
This is the statement that I'm trying but it isn't working:
SELECT *
FROM FIRST_TABLE
INNER JOIN SECOND_TABLE on FIRST_TABLE.TIME_LOCATION =
CONCAT(SUBSTRING(SECOND_TABLE.TIME,12,5) , SECOND_TABLE.LOCATION);
I am receiving the below error:
[SQL0171] Argument 1 of function
SUBSTRING not valid. Cause . . . . . : The data type, length, or value
of argument 1 of function SUBSTRING specified is not valid. Recovery .
. . : Refer to the DB2 for IBM i SQL Reference topic collection in the
Database category in the IBM i Information Center for more information
on scalar functions. Correct the arguments specified for the function.
Try the request again
There will be no space when you concat the substrings.
It will look like this:
02:00IND
I am guessing it is just returning a blank result as you have not mention it was returning an error :)
I got it to work if anyone ever has a similar issue. I am a beginner in SQL so I don't know if this will be helpful or not, I'm sure there is a better way of doing it.
Anyway, my issue was that the data types were incompatible. I cast the timestamp as time which left me with a 'HH.MM.SS' format. After that I had to cast it into a NVARCHAR to make a substring that excluded the seconds. And THEN I had to replace the '.' with a ':' to make the values match. It was a lot of work but I figured it out!
SELECT *
FROM FIRST_TABLE
INNER JOIN SECOND_TABLE on FIRST_TABLE.TIME_LOCATION =
CONCAT(CONCAT(REPLACE(SUBSTRING(CAST(TIME(SECOND_TABLE.TIME)AS NVARCHAR(8)), 1, 5),
'.', ':'), ' '), DSP134.LSDTID);

Getting an error: Argument data type varchar is invalid for argument 2 of substring function

I'm trying to get a substring from the value of a column and I'm getting the following error Argument data type varchar is invalid for argument 2 of substring function.
The column type is NvarChar(50) and is a system column for an application, so I can't modify it.
Ideally I'd just be able to select the substring as part of the query without having to alter the table, or create a view or another table.
Here's my query
SELECT SUBSTRING(INVOICE__, ':', 1)
FROM dwsystem.dbo.DWGroup
Im trying to select only everything in the string after a specific character. In this case the : character.
Use charindex with : as the first argument
select substring(invoice__,charindex(':',invoice__)+1,len(invoice__))
from dwsystem.dbo.dwgroup
SUBSTRING parameter is start position and end position so both parameter will be number like below
SELECT SUBSTRING(INVOICE__, 1, 1)
FROM dwsystem.dbo.DWGroup
you can use SUBSTRING_INDEX as you used mysql
SELECT SUBSTRING_INDEX(INVOICE__,':',-1);
example
SELECT SUBSTRING_INDEX('mytestpage:info',':',-1); it will return
info

ORA-01722: invalid number error

I am running the below mentioned query in my Oracle client and i am getting
ORA-01722: invalid number
error. I know the issue is due to the TAG_VALUE column being of type "varchar2" and i am converting it to number and then using that field in where clause. I have tried using "CAST" function but that is also not helping.
If i run the query neglecting the last where condition with code WHERE (P.TAG_VALUE > '100') then i am getting the result but including the last where clause gives me error.
SELECT DISTINCT
count(P.CREATED_DATETIME)
FROM
(
select OUTPUT_TAG_ID,TO_NUMBER(TAG_VAL,'9999.99') AS
TAG_VALUE,TAG_VAL_TS,CREATED_DATETIME
from OV80STG.PRCSD_DATA_OUTPUT_ARCHIVE
where MODEL_CODE='MDLADV1538'
AND TAG_VAL <> 'U_Transfer_rate'
) P
WHERE
(P.TAG_VALUE > '100')
Any suggestion will be appreciated. Thanks.
Remove the single quotes from around the value in the where, you don't need them when its an integer. query will be like this:
SELECT DISTINCT
COUNT(P.CREATED_DATETIME)
FROM
(
SELECT
OUTPUT_TAG_ID,
TO_NUMBER(TAG_VAL, '9999.99') AS TAG_VALUE,
TAG_VAL_TS,
CREATED_DATETIME
FROM OV80STG.PRCSD_DATA_OUTPUT_ARCHIVE
WHERE MODEL_CODE = 'MDLADV1538'
AND TAG_VAL <> 'U_Transfer_rate'
) P
WHERE(P.TAG_VALUE > 100);
TO_NUMBER function returns a numeric value so, as mentioned in comment, you shouldn't compare it with string value.
I solved the issue by including outer where clause inside the subquery and then I got the required result without any error.

Remove string in SQL Server

In my table Products in SQL Server, I have a column UrlLink with values that look like this:
Id UrlLink
-----------------------------------
1 domain/product1.html?7
2 domain/product2.html?34
3 domain/product294.html?6576
4 domain/product54.html?765
How to remove parameter
?7, ?34, ?6576, ?765
from column UrlLink?
Thanks!
To remove the query string part from the UrlLink column in the table, you need to use Left and CharIndex in your UPDATE statement.
UPDATE Products
SET UrlLink = LEFT(UrlLink, CHARINDEX('?',UrlLink)-1)
Using left and charindex should work:
select left(UrlLink, charindex('?',UrlLink)-1) from Products;
This would return everything before the first occurrence of a ?. You might want to add some null checks if parameter isn't mandatory in the UrlLink column.
Try with this code:
DECLARE #myvar varchar(100);
SET #myvar = 'domain/product1.html?7';
SELECT REVERSE(SUBSTRING((REVERSE(#myvar)), (CHARINDEX('?',REVERSE(#myvar))+1),500)) AS result ;
GO
Simple and won't fail in case you have a Urllink that does not contain?.
In case of multiple ? removes everything from the first ? since in URL this is the only ? with special significance.
select id,left(Urllink,charindex('?',Urllink+'?')-1)
from Products
You just have to find ? in your url and take string upto it. You can use LEFT/SUBSTRING for getting substring and CHARINDEX for finding ? in your string. Check out my query below
select id, substring(urllink,1,charindex('?',urllink)-1)
from products
In case multiple ? symbols are there in the UrlLink column values and if you want to take the value after the last ? symbol. Then use a combination of SUBSTRING and CHARINDEX .
Query
SELECT
[Id],
SUBSTRING([UrlLink], 1,
LEN([UrlLink]) - CHARINDEX('?', REVERSE([UrlLink]), 1)) AS [UrlLink]
FROM [Products];
Demo

Substring and Charindex - issues with minus operator

I am using SQL Server 2012. In the column PROJECT_NAME I have line items, all with the same format, that look like this:
PROJECT_NAME
--------------
Caulk, Norman v BPI
Caulk, Norman v BWD
Carper, Robert v ECH
I am trying to extract the first name (second name in the text string) and am using this query:
select
substring(Project_name,(charindex(',',PROJECT_NAME,0)),((CHARINDEX(' v ',PROJECT_NAME)-
(charindex(',',PROJECT_NAME)))))
from RPT_PROJ_MAIN pm
When I run this query I get the following error:
Invalid length parameter passed to the LEFT or SUBSTRING function.
I have isolated all of the different expressions and they all work fine on their own. If I replace the minus operator with a + then the query runs fine and I cannot figure out why?
That means that despite you saying that all rows have the same format some of them don't.
Specifically if a value has no v <something> at the end you'll get exactly that error, because the third parameter to SUBSTRING() function will have a negative value
Here is SQLFiddle demo
Hi I have solution for this just used below mentioned trick to handle this issue. Require to Declare value you want to minus. Just see the example below.
Declare #Val numeric=2
PRINT LEFT('My Name is Bhumesh Shah',charindex('is','My Name is Bhumesh Shah'))
PRINT LEFT('My Name is Bhumesh Shah',charindex('is','My Name is Bhumesh Shah')-#val)
Output:
My Name i
My Name