Substring and Charindex - issues with minus operator - sql

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

Related

Druid SQL: get substring issue

There is the table column which holds the comma-separated values, e.g:
abc321,rd512,spwewr
I need to extract the substring which starts from the user-defined pattern.
Example:
Input Pattern | Expected result
abc abc321
r rd512
spwe spwewr
b NULL
Following fails in Druid SQL:
SELECT SUBSTRING('abc321,rd512,spwewr', POSITION('r' IN 'abc321,rd512,spwewr'), 2)
This is the known Druid bug:
" Substring operator converter does not handle non-constant literals correctly":
https://issues.apache.org/jira/browse/CALCITE-2226
I think the way to go is to use REGEXP_EXTRACT() or REGEXP_LIKE()
but I cannot figure out the specific syntax.
select regexp_extract('abc321,rd512,spwewr', 'rd[^,]+', 0)

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

Translate function not returning relevant string in amazon redshift

I am trying to use a simple Translate function to replace "-" in a 23 digit string. The example of one such string is "1049477-1623095-2412303" The expected outcome of my query should be 104947716230952412303
The list of all "1049477-1623095-2412303" is present in a single column "table1". The name of the column is "data"
My query is
Select TRANSLATE(t.data, '-', '')
from table1 as t
However, it is returning 104947716230952000000 as the output.
At first, I thought it is an overflow error since the resulting integer is 20 digit so I also tried to use following
SELECT CAST(TRANSLATE(t.data,'-','') AS VARCHAR)
from table1 as t
but this is not working as well.
Please suggest a way so that I could have my desirable output
This is too long for a comment.
This code:
select translate('1049477-1623095-2412303', '-', '')
is going to return:
'104947716230952412303'
The return value is a string, not a number.
There is no way that it can return '104947716230952000000'. I could only imagine that happening if somehow the value is being converted to a numeric or bigint type.
Try regexp_replace()
Taking your own example, execute:
select regexp_replace('[string / column_name]','-');
It can be achieve RPAD try below code.
SELECT RPAD(TRANSLATE(CAST(t.data as VARCHAR),'-','') ,20,'00000000000000000000')

How to substring records with variable length

I have a table which has a column with doc locations, such as AA/BB/CC/EE
I am trying to get only one of these parts, lets say just the CC part (which has variable length). Until now I've tried as follows:
SELECT RIGHT(doclocation,CHARINDEX('/',REVERSE(doclocation),0)-1)
FROM Table
WHERE doclocation LIKE '%CC %'
But I'm not getting the expected result
Use PARSENAME function like this,
DECLARE #s VARCHAR(100) = 'AA/BB/CC/EE'
SELECT PARSENAME(replace(#s, '/', '.'), 2)
This is painful to do in SQL Server. One method is a series of string operations. I find this simplest using outer apply (unless I need subqueries for a different reason):
select *
from t outer apply
(select stuff(t.doclocation, 1, patindex('%/%/%', t.doclocation), '') as doclocation2) t2 outer apply
(select left(tt.doclocation2), charindex('/', tt.doclocation2) as cc
) t3;
The PARSENAME function is used to get the specified part of an object name, and should not used for this purpose, as it will only parse strings with max 4 objects (see SQL Server PARSENAME documentation at MSDN)
SQL Server 2016 has a new function STRING_SPLIT, but if you don't use SQL Server 2016 you have to fallback on the solutions described here: How do I split a string so I can access item x?
The question is not clear I guess. Can you please specify which value you need? If you need the values after CC, then you can do the CHARINDEX on "CC". Also the query does not seem correct as the string you provided is "AA/BB/CC/EE" which does not have a space between it, but in the query you are searching for space WHERE doclocation LIKE '%CC %'
SELECT SUBSTRING(doclocation,CHARINDEX('CC',doclocation)+2,LEN(doclocation))
FROM Table
WHERE doclocation LIKE '%CC %'

This simple sql query yields empty result

I am using PhpMysql and this simple query yields an empty result ("MySQL returned an empty result set").
Here is the query: SELECT * FROM TABLE 2 WHERE prenom LIKE '%Scott%'
My table has a row called prenomand there is a 'Scott' value. I have tried with other first names (which I know are in the database) and it does not work either.
The strange thing is that when I use the wildcards between One letter only i.e: SELECT * FROM TABLE 2 WHERE prenom LIKE '%S%' it works..
I need help please!
Ok so the pb was that due to the collation type instead of SCOTT, the file was encoded with spaces S C O T T. I had to reformat the file in utf8 to fix this!