Make substring using a specific delimiter in SQL - sql-server

I want to make a substring of a column value using a specific delimiter.I tried SUBSTRING_INDEX,but it doesn't work for SQL.Is there any way to achieve this??
Column values are:
ARTCSOFT-1111
ARTCSOFT-1112
ARTCSOFT-1113
and I want to achieve the same thing in SQL:
SUBSTRING_INDEX(Code,'SOFT-',1))
i.e I want the number after SOFT- in my substring.I can't use only - because before SOFT- there is chance that - may occur(rare case,but I don't want to take a chance)

Try using just SUBSTRING . For example
SELECT
SUBSTRING(code, CHARINDEX('SOFT-', code) + 5, LEN(code)) AS [name] from dbo.yourtable
hope this helps.
Tested Result:

SELECT RIGHT(Code , CHARINDEX ('-' ,REVERSE(Code))-1)
Read this as: Get the rightmost string after the first '-' in a reversed string - which is the same as the string after the last '-' character.

Try This Query:
select substring(col,charindex('-',col)+1,len(col)-charindex('-',col)) from #Your_table
Explanation of Query:
Here Charindex find the '-' delimeter [length] IN Given String and now that Result[length+1] is our starting point and ending length is [len(col)-starting length] gives ending point and then use substring Function to split a string according to our requirement.
Result of Query:
Required_col
1111
1112
1113

Related

SQL Server - remove left part of string before a specific character

I have a VARCHAR value that looks like this:
5.95 $ Additional fees
How can I remove everything left from character '$' (including that character) ? So that I get the following result:
Additional fees
The '$' is always present.
STUFF and CHARINDEX would be the simpliest way, in my opinion:
SELECT STUFF(YourColumn,1, CHARINDEX('$',YourColumn),'')
FROM (VALUES('5.95 $ Additional fees'))V(YourColumn);
Note that as $ has a whitespace afterwards, the value returned will have a leading whitespace (' Additional fees'). You could use TRIM (or LTRIM and RTRIM on older versions of SQL Server) to remove this, if it isn't wanted.
I haven't assumed that the portion string to be replaced is CHARINDEX('$',YourColumn)+1, as we have one sample. As far as we know, you could also have values such as '10.99$Base Cost'. If the +1 was used, it would return 'ase Cost' for such a value.
Hello do it like below syntax
declare #temp nvarchar(max)='5.95 $ Additional fees'
select SUBSTRING(#temp,charindex('$',#temp)+1,len(#temp)-1)
You can use SUBSTRING get the particular string and CHARINDEX function to get index of special character, in your case $.
DECLARE #Var VARCHAR(100)
SET #Var = '5.95 $ Additional fees'
SELECT SUBSTRING(#Var, CHARINDEX('$', #Var) + 1, LEN(#Var) - LEN(LEFT(#Var, CHARINDEX('$', #Var))))

How to replace string in "select" statement

I need to add comma after every six digits ,but I don't know its length and I can't use loops.
Thanks in advance.
I've tried REGEXP_REPLACE DB2 function, but it doesn't recognize my column as string.
For example , I need to replace "123456123456" with "123456, 123456".
Try this:
select rtrim(xmlcast(xmlquery('fn:replace($s, "([0-9]{6})", "$1, ")' passing str as "s") as varchar(4000)), ', ')
from table(values ('123456123456')) t(str);

SQL Server - How to get last numeric value in the given string

I am trying to get last numeric part in the given string.
For Example, below are the given strings and the result should be last numeric part only
SB124197 --> 124197
287276ACBX92 --> 92
R009321743-16 --> 16
How to achieve this functionality. Please help.
Try this:
select right(#str, patindex('%[^0-9]%',reverse(#str)) - 1)
Explanation:
Using PATINDEX with '%[^0-9]%' as a search pattern you get the starting position of the first occurrence of a character that is not a number.
Using REVERSE you get the position of the first non numeric character starting from the back of the string.
Edit:
To handle the case of strings not containing non numeric characters you can use:
select case
when patindex(#str, '%[^0-9]%') = 0 then #str
else right(#str, patindex('%[^0-9]%',reverse(#str)) - 1)
end
If your data always contains at least one non-numeric character then you can use the first query, otherwise use the second one.
Actual query:
So, if your table is something like this:
mycol
--------------
SB124197
287276ACBX92
R009321743-16
123456
then you can use the following query (works in SQL Server 2012+):
select iif(x.i = 0, mycol, right(mycol, x.i - 1))
from mytable
cross apply (select patindex('%[^0-9]%', reverse(mycol) )) as x(i)
Output:
mynum
------
124197
92
16
123456
Demo here
Here is one way using Patindex
SELECT RIGHT(strg, COALESCE(NULLIF(Patindex('%[^0-9]%', Reverse(strg)), 0) - 1, Len(strg)))
FROM (VALUES ('SB124197'),
('287276ACBX92'),
('R009321743-16')) tc (strg)
After reversing the string, we are finding the position of first non numeric character and extracting the data from that position till the end..
Result :
-----
124197
92
16

trimming a substring in sql server column

I have data in a text column that looks like xxxxx.x.xx
I need to get the string into a view with a format of xxxxx.x (removing the trailing .xx).
I am not sure how to do this. Any help would be appreciated.
I think I would need to get the length from the start to the 2nd "." and then Left that length.
I also wonder, can this be done as a column expression or would it need a function?
Here is another twist. How would I handle the same issue if the char length is variable such as xxxxxx.xx.x and xxxxx.x.x?
You can also try this:
SELECT
REVERSE(
SUBSTRING(
REVERSE(#word),
CHARINDEX('.', REVERSE(#word))+1,
LEN(REVERSE(#word))
)
)
FROM yourTable
Here is a SQLFiddle
This solution is generic, so it would work also for the case in which you have xx.xxxx.xx.xxx or any other number of letters sepparated by dots.
This can be achieved using the expression:
left(#s, charindex('.', #s, charindex('.', #s) + 1) - 1)
(where #s is your varchar value)
Online demo: http://www.sqlfiddle.com/#!3/d41d8/23448
select 'xxxxx.x.xx' as original
,Replace( 'xxxxx.x.xx',Right('xxxxx.x.xx',3),'') as new

How can i find the pattern identified by PATINDEX()

Which pattern is identified by PATINDEX in the below statement? Could any one help me analyse it?
How can we find which of ('I','II','III') is identified ?
select PATINDEX ('%[I,II,III]%','sjfhasjdg II')
Please help me finding it.
This is not how you use PATINDEX. , is not an alternation operator.
You are telling it to find characters in the set I,II,III which just repeats a lot of characters so can be simplified to "find the first location of either I or ,"
You could try
WITH SearchTerms(Term)
AS (SELECT 'I'
UNION ALL
SELECT 'II'
UNION ALL
SELECT 'III'),
ToBeSearched(string)
AS (SELECT 'sjfhasjdg II')
SELECT string,
Term,
Charindex(Term, string) AS Location
FROM ToBeSearched
JOIN SearchTerms
ON Charindex(Term, string) > 0
Returns
string Term Location
------------ ---- -----------
sjfhasjdg II I 11
sjfhasjdg II II 11
Of course both I and II match as anything that matches the second will always match the first.

Resources