I need to perform a substring operation in SQL Server and get a string from start of hyphen character (-).
My input string is :
'44345434595-E535-12349-5273-202003-16785'
and I want to extract the string from 4th instance of hyphen to 5th instance of hyphen and my desired result is : 202003
You can use string_split function in sqlserver
declare #str varchar(max)= '44345434595-E535-12349-5273-202003-16785'
select * from (select value, row_number() over (order by charindex('-' + value + '-', '-' + #str + '-')) rn
from string_split(#str, '-')) t1
where t1.rn in (5)
Since there's no guarantee of ordering using string_split function, we need to sort by position based on your -
row_number() over (order by charindex('-' + value + '-', '-' + #str + '-'))
If your input string has fixed format you can set position and lenght directly
SELECT SUBSTRING(yourColumn, 29, 5) FROM YourTable
If the Format is Fixed You can use Substring for that:
DECLARE #STR VARCHAR(100)
SELECT #STR='44345434595-E535-12349-5273-202003-16785'
SELECT SUBSTRING(#STR, 29, 6)
Output: 202003
Below is the code that might suit your requirement and will work if the input has exactly 5 hypen.
DECLARE #data varchar(50)= '44345434595-E535-12349-5273-202003-16785'
select
reverse(
SUBSTRING
(
SUBSTRING
(
reverse(#data),
charindex('-',reverse(#data),1)+1,
len(#data)
),
1,
charindex('-',SUBSTRING(reverse(#data),
charindex('-',reverse(#data),1)+1,
len(#data)))-1)
);
Related
I have texts like "DBName_TemplateDB_TESTDB01234_document" and "DBName_TemplateDB_TESTDB01234678_document". From both texts need to extract string between second underscore() and last underscore() like "TESTDB01234" and "TESTDB01234678".
Can you please help how to string in SQL Server using SUBSTRING and CHARINDEX?
Example:
Input Text: 'DBName_TemplateDB_TESTDB01234_document'
Output: TESTDB01234
Input Text: 'DBName_TemplateDB_TESTDB01234678_document'
Output: TESTDB01234678
I tried to extract and it's working only from the first underscore like below.
declare #Dbname varchar(max) = '#new#-TESTDB01234_document'
select substring( LEFT(#DbName,charindex('_',#DbName)-1),charindex('TEST',#DbName),len(LEFT(#DbName,charindex('_',#DbName)))-1)
Will this work for you?
--OPTION ONE: SUBSTRING
DECLARE #Start INT = CHARINDEX('TEST', #Dbname);
DECLARE #End INT = LEN(#Dbname) - (CHARINDEX('_', REVERSE(#Dbname)) - 1) - #Start;
SELECT SUBSTRING(#Dbname, #Start, #End) AS [Name]
--OPTION TWO: DOUBLE REPLACE
SELECT REPLACE(REPLACE(#Dbname, 'DBName_TemplateDB_', ''), '_document', ''); AS [Name]
--OPTION THREE: STRING_SPLIT
SELECT TOP 1 value AS [Name]
FROM STRING_SPLIT(#Dbname, '_')
WHERE value LIKE 'Test%'
I added a few options, sorry if its outside the scope of the question.
Using charindex
declare #Dbname varchar(max) = 'DBName_TemplateDB_TESTDB01234678_document'
select right(left(#DbName,third - 1), third - second - 1)
from (
select charindex('_',#DbName,charindex('_',#DbName) + 1) second,
charindex('_',#DbName,charindex('_',#DbName,charindex('_',#DbName) + 1) + 1) third
) t
If you are looking for data between the second and the last underscore, I will give the following approach as there might be more underscores between these ones:
declare #Dbname varchar(max) = 'DBName_TemplateDB_TESTDB01234678_1_2_3_document'
DECLARE #DbnameXML XML = '<a>' + REPLACE(#Dbname, '_', '</a><a>') + '</a>';
SELECT STRING_AGG([value], '_') WITHIN GROUP (ORDER BY [value_id])
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY T.c) - 1
,T.c.value('.', 'VARCHAR(128)')
,COUNT(1) OVER()
FROM #DbnameXML.nodes('a') T(c)
) DS ([value_id], [value], [values_count])
WHERE [value_id] > 1
AND [value_id] + 1 < [values_count]
I have a set of strings like this:
CAP BCP0018 36
MFP ACZZ1BD 265
LZP FEI-12 3
I need to extract only the last values from the right and before the space, like:
36
265
3
how will the select statement look like? I tried using the below statement, but it did not work.
select CHARINDEX(myField, ' ', -1)
FROM myTable;
Perhaps the simplest method in SQL Server is:
select t.*, v.value
from t cross apply
(select top (1) value
from string_split(t.col, ' ')
where t.col like concat('% ', val)
) v;
This is perhaps not the most performant method. You probably would use:
select right(t.col, charindex(' ', reverse(t.col)) - 1)
Note: If there are no spaces, then to prevent an error:
select right(t.col, charindex(' ', reverse(t.col) + ' ') - 1)
Since you have mentioned CHARINDEX() in question, I am assuming you are using SQL Server.
Try below
declare #table table(col varchar(100))
insert into #table values('CAP BCP0018 36')
insert into #table values('MFP ACZZ1BD 265')
insert into #table values('LZP FE-12 3')
SELECT REVERSE(LEFT(REVERSE(col),CHARINDEX(' ',REVERSE(col)) - 1)) FROM #table
Functions used
CHARINDEX ( expressionToFind , expressionToSearch ) : returns position of FIRST occurence of an expression inside another expression.
LEFT ( character_expression , integer_expression ) : Returns the left part of a character string with the specified number of characters.
REVERSE ( string_expression ) : Returns the reverse order of a string value
I have a bit of problem regarding sql select statement.
I have a column value that look like this
2>4>5 or
28>30>52 or
300>410>500 or
2>4>5>8
My question is, how can i get the value from RIGHT after the >
character, so the select statement from the value above will return
4
30
410
5
Thanks in advance
If you need second value from right, then try:
SELECT SUBSTRING_INDEX( SUBSTRING_INDEX(your_column, '>', -2), '>', 1);
EDIT
One solution for sql server:
DECLARE #str varchar(max);
set #str = '2>4>5>8';
SELECT reverse( substring(
substring( reverse(#str), charindex( '>', reverse(#str) )+1, len(#str) ), 0,
charindex( '>', substring( reverse(#str), charindex( '>', reverse(#str) )+1, len(#str) ) )
) );
This is similar to extracting the n-th element from a delimited string. The only difference is that in this case we want the n-th-to-last element. The change can be achieved with a double use of reverse. Assuming the table is MyTable and the field is MyColumn, here's one way:
SELECT
Reverse(
CAST('<x>' + REPLACE(Reverse(MyColumn),'>','</x><x>') + '</x>' AS XML).value('/x[2]', --x[2] because it's the second element in the reversed string
'varchar(5)' --Use something long enough to catch any number which might occur here
))
FROM
MyTable
With credit to #Shnugo for his efforts here: Using T-SQL, return nth delimited element from a string
You can't cast as an int where I've put varchar(5)since at that stage the strings are still reversed. If you need to convert to an integer, do that by wrapping a convert/cast on the outside.
;WITH cte1(Value)
AS
(
SELECT '2>4>5' Union all
SELECT '28>30>52' Union all
SELECT '300>410>500' Union all
SELECT '2>4>5>8'
)
SELECT
SUBSTRING(
(
REVERSE(SUBSTRING(((REVERSE((SUBSTRING(Value, RIGHT(CHARINDEX('>', Value), Len(Value)) + 1, Len(Value)))))),
CHARINDEX('>',((REVERSE((SUBSTRING(Value, RIGHT(CHARINDEX('>', Value), Len(Value)) + 1, Len(Value)))))))+1,LEN(Value)))
),CHARINDEX('>',(
REVERSE(SUBSTRING(((REVERSE((SUBSTRING(Value, RIGHT(CHARINDEX('>', Value), Len(Value)) + 1, Len(Value)))))),
CHARINDEX('>',((REVERSE((SUBSTRING(Value, RIGHT(CHARINDEX('>', Value), Len(Value)) + 1, Len(Value)))))))+1,LEN(Value)))
))+1,LEN(Value))
AS ExpectedValue
FROM cte1
I have a table that has a Prefix of variable length that always ends with . and a suffix of variable length that always begins with -. The - can also be found in the string of text that I am looking for, so using REVERSE() is likely the best way to identify the suffix -.
ex: Prefix1.ABC-123-Suffix1
I'd like the SUBSTRING() result to look like: ABC-123
I was able to create both a SUBSTRING() to remove the Prefix and a SUBSTRING() to remove the suffix, but I'm having a hard time applying both. The returned text can be variable length.
I'm trying to avoid declaring a variable, but am open to it. I'd prefer to only use SUBSTRING(), REVERSE(), and CHARINDEX() if possible.
This is what I have so far:
SELECT [Col1]
,SUBSTRING((Col1),CHARINDEX('.', (Col1)) + 1, 999) AS [StrippedPrefix]
,REVERSE(SUBSTRING(REVERSE(Col1),CHARINDEX('-', REVERSE(Col1)) + 1, 999)) AS [StrippedSuffix]
--new SUBSTRING() with both Prefix and Suffix stripped
FROM [Table1]
Declare #YourTable table (Col1 varchar(50))
Insert Into #YourTable values
('Prefix1.ABC-123-Suffix1')
Select *
,String=Substring(Col1,charindex('.',Col1)+1,len(Col1)-charindex('.',Col1)-charindex('-',reverse(Col1)))
From #YourTable
Returns
Col1 String
Prefix1.ABC-123-Suffix1 ABC-123
As per M Ali's suggestion (Even better)
Select *
,String=PARSENAME(REPLACE(Col1 , '-', '.') , 3) + '-' + PARSENAME(REPLACE(Col1 , '-', '.') , 2)
From #YourTable
Here the varaiable is only used as demo - should work with your table as well:
DECLARE #s nvarchar(100) = 'Prefix1.ABC-123-Suffix1'
SELECT #s,
SUBSTRING(#s, 1, CHARINDEX('.', #s)-1) StrippedPrefix,
SUBSTRING(#s, LEN(#s)-CHARINDEX('-', REVERSE(#s))+2, CHARINDEX('-', REVERSE(#s))) StrippedSuffix,
SUBSTRING(#s, CHARINDEX('.', #s)+1, LEN(#s)-CHARINDEX('.', #s)-CHARINDEX('-', REVERSE(#s))) NewString
Here is one way
DECLARE #str VARCHAR(100)= 'Prefix1.ABC-123-Suffix'
SELECT Reverse(Stuff(intr_str, 1, Charindex('-', intr_str), ''))
FROM (SELECT Reverse(Stuff(#str, 1, Charindex('.', #str), '')) AS intr_str) a
Result : ABC-123
Suppose I have this query.
SELECT
proj.refno [Reference No.],
proj.projname [NNNN],
TotalCost= '$' + CONVERT(NVARCHAR(100),cast(ROUND((cast(ship.volfinish as int) * data.price)/1000,2) as decimal(5,2)))
FROM
projects proj
INNER JOIN
projdata data ON proj.controlno = data.controlno
INNER JOIN
shipment ship ON data.ctrlno = ship.dctrlno
WHERE
proj.refno IN ('item1', 'item2','item3')
ORDER BY
proj.refno
with this output:
Reference No. NNNN TotalCost
GR-NFS52 abc123 StudentsTitle123 (NNNN: xxxxxxxxxxxxx) $215.45
GR-PFS53 def456 StudentsTitle456 (NNNN: xxxxxxxxxxxxx) $259.55
GR-SSFS43 ghi789 StudentsTitle789 (NNNN: xxxxxxxxxxxxx) $242.35
How can I make the NNNN column used the substring function with this output. Cause I'm not into t-sql.
NNNN
xxxxxxxxxxxxx
xxxxxxxxxxxxx
xxxxxxxxxxxxx
Assuming you have pattern like NNNN: xxxxxxxxxxx) in your strings you can extract this number using some simple manipulation over the string value using charindex and substring:
declare #str nvarchar(max)
select #str = 'Students (NNNN: 9781410314291)'
select substring(#str,
charindex('ISBN:', #str) + 6,
charindex(')', #str, charindex('NNNN:', #str)) - charindex('NNNN:', #str) - 6)
Here we first find position of NNNN: substring, then position of first occurence of closing bracket ) after this substing and taking part of string between these positions - it is exactly number you need.
In your particular case you can use outer apply in select query in order to make it more readable by avoiding multiple copy-pasting the same charindex('NNNN:', proj.projname) expression:
select
proj.refno [Reference No.],
substring(proj.projname,
CALC.pos_from,
charindex(')', proj.projname, CALC.pos_from) - CALC.pos_from - 6) as [NNNN],
....
FROM projects proj
.....
outer apply (select charindex('NNNN:', proj.projname) as pos_from) as CALC
Try this:
DECLARE #str nvarchar(max) = 'Novels for Students, vol. 52 (ISBN: 9781410314291)'
SELECT
REPLACE(STUFF(#str, 1, PATINDEX('% '+REPLICATE('[0-9]', 13) + '%', #str), ''), ')', '')
Result:
9781410314291