Substring after a dot in SQL Server - sql

I have a field which holds data like this
Product.Company.Price.Item
I want to substring any string after the last dot. so it will look like this
Item
I know there is a substring.index function in MySQL. How can I implement this in SQL Server?

select reverse(substring(reverse(fieldName),1,charindex('.',reverse(fieldname))-1))
This will do what you want, but I wouldn't use it as part of a where expression
declare #x varchar(50) = 'Product.Company.Price.Item'
select substring(#x,len(#x)-charindex('.',reverse(#x))+2,99)

Hope this Query works fine for your Case:
DECLARE #VAR VARCHAR(MAX)='Product.Company.Price.Item'
SELECT RIGHT(#VAR, CHARINDEX('.', REVERSE(#VAR) + '.') - 1) AS VARL

If it's always 4 parts then you could use parsename() as
select parsename('Product.Company.Price.Item', 1)
or use right() and charindex() as
select right(str, p)
from
(
values ('Product.Company.Price.Item'), ('Other.row')
) t(str) cross apply
(
values (charindex('.', reverse(str))-1)
) tt(p)

You can try this using functions like Reverse(), Left(), and the CHARINDEX().
DECLARE #Sentence VARCHAR(100) = 'Product.Company.Price.Item';
SELECT REVERSE(LEFT(REVERSE(#Sentence),
CHARINDEX('.',REVERSE(#Sentence))- 1)) AS [Last_Word]

Related

SQL server Rex fetch string in wildcard

For example, I have got a string with table name and the schema like:
[dbo].[statistical]
How can fetch just the table name statistical out from this string?
This is what PARSENAME is used for:
SELECT PARSENAME('[dbo].[statistical]', 1)
SELECT PARSENAME('[adventureworks].[dbo].[statistical]', 1)
SELECT PARSENAME('[adventureworks]..[statistical]', 1)
SELECT PARSENAME('[statistical]', 1)
SELECT PARSENAME('dbo.statistical', 1)
-- all examples return 'statistical'
You could alternatively try this:
declare #s varchar(100) = 'asd.stadfa';
select reverse(substring(s, 1, charindex('.', s) - 1)) from (
select reverse(#s) s
) a
charindex returns first occurence of character, so you reverse initial string to make last dot first. Then you just use substring to extract first part of reversed string, which is what you are looking for. Finally, you need to apply reverse one more time to reverse back extracted string :)

How to get number from a string in sql

I have a string like this 'sdf,11-df,12-asd,sadfsdf'. But I need only numbers from this string in multiple rows like this.
11
12
I need numbers between (,) and (-) that's the actual requirement. String can be like 'sd47f,11-df,12-asd,sadfsdf,12-ds,32-fsdfsd' anything, But numbers will always between (,) and (-)
or find numbers between (,) and (-) it will also help me.
Thanks in advance.
I need any solution guys please help,
Following will work with SQL Server 2016 or higher:
DECLARE #str varchar(50) = 'sd47f,11-df,12-asd,sadfsdf,12-ds,32-fsdfsd'
SELECT
LEFT(Value, CHARINDEX('-', Value)-1)
FROM STRING_SPLIT(#str, ',')
WHERE PATINDEX('%[0-9]-%',Value) > 0
try this
DECLARE #data nvarchar(max) = 'sdf,11-df,12-asd,sadfsdf'
select substring(#data,PATINDEX('%[0-9]%',#data),2)
union all
select substring(#data,PATINDEX('%[-]%',REVERSE(#data))-1,2)
Update
You can also use a function which returns as a table
CREATE FUNCTION dbo.udf_GetNumeric (#strAlphaNumeric VARCHAR(256))
RETURNS TABLE AS RETURN ( select LEFT(value,2) as Value from
string_split(#strAlphaNumeric,',') where PATINDEX('%[0-9]-%',value)
> 0 )
END GO
SELECT * from
dbo.udf_GetNumeric('sd47f,11-df,12-asd,sadfsdf,12-ds,32-fsdfsd')

Sql substring using "/" as indicator where to cut

Hi I want to cut a string in sql at 2 points with the "/" as an indicator where to cut. The string looks like this: "test1/test2/test3/test4/test5/test6" and I need the parts of test 2 and test 5 but they are not static, so I have to use the "/" befor and after them to set the points where to cut, any suggestions?
You could achieve this by using XML:
DECLARE #x NVARCHAR(100) = 'test1/test2/test3/test4/test5/test6';
DECLARE #xml XML = cast(('<X>'+replace(#x,'/' ,'</X><X>')+'</X>') as xml);
WITH cte AS(
SELECT N.value('.', 'varchar(10)') as value, ROW_NUMBER() OVER (ORDER BY (SELECT(0))) AS rn
FROM #xml.nodes('X') as T(N)
)
SELECT *
FROM cte
WHERE rn IN (1, 5)
You can use the function CHARINDEX()
For example this query will work for you
declare #text varchar(100)='test1/test2/test3/test4/test5/test6'
select
SUBSTRING (#text,CHARINDEX('/',#text)+1,CHARINDEX('/',#text,(CHARINDEX('/',#text)+1))-CHARINDEX('/',#text)-1),
SUBSTRING (#text,CHARINDEX('/',#text,CHARINDEX('/',#text,CHARINDEX('/',#text,CHARINDEX('/',#text)+1)+1)+1)+1,CHARINDEX('/',#text,(CHARINDEX('/',#text)+1))-CHARINDEX('/',#text)-1)
Just incase you want to substring something else, I'm sharing the code below and then you can work on it
declare #text varchar(100)='test1/test2/test3/test4/test5/test6'
select
SUBSTRING (#text,CHARINDEX('/',#text)+1,CHARINDEX('/',#text,(CHARINDEX('/',#text)+1))-CHARINDEX('/',#text)-1) --test2
,SUBSTRING (#text,CHARINDEX('/',#text,CHARINDEX('/',#text)+1)+1,CHARINDEX('/',#text,(CHARINDEX('/',#text)+1))-CHARINDEX('/',#text)-1) --test3
,SUBSTRING (#text,CHARINDEX('/',#text,CHARINDEX('/',#text,CHARINDEX('/',#text)+1)+1)+1,CHARINDEX('/',#text,(CHARINDEX('/',#text)+1))-CHARINDEX('/',#text)-1) --test4
,SUBSTRING (#text,CHARINDEX('/',#text,CHARINDEX('/',#text,CHARINDEX('/',#text,CHARINDEX('/',#text)+1)+1)+1)+1,CHARINDEX('/',#text,(CHARINDEX('/',#text)+1))-CHARINDEX('/',#text)-1) --test5
If the items that you want are always the second and the second to last you can use CHARINDEX to find the first and second '/' and SUBSTRING to pull that value. Then REVERSE the string to get the second last. e.g.
DECLARE #data varchar(128) = 'test1/test2/test3/test4/test5/test6';
SELECT substring(#data,charindex('/',#data,1)+1,charindex('/',#data,charindex('/',#data,1)+1)-(charindex('/',#data,1)+1)),
reverse(substring(reverse(#data),charindex('/',reverse(#data),1)+1,charindex('/',reverse(#data),charindex('/',reverse(#data),1)+1)-(charindex('/',reverse(#data),1)+1)))

Separate a string in SQL in two parts separated by (hyphen) - and store the two parts in a different variable

I need to separate a string in SQL,
Example,
a='001A01-001A05'
What I need in OUTPUT is,
x='001A01'
y='001A05'
Use LEFT, CHARINDEX and SUBSTRING
DECLARE #char VARCHAR(500)= '001A01-001A05'
SELECT LEFT(#char, Charindex('-', #char) - 1),
Substring(#char, Charindex('-', #char) + 1, Len(#char))
Prdp's answer would be my first choice, but if SQL Server 2012+, another option is PARSENAME()
Declare #a varchar(25)='001A01-001A05'
Select x=ParseName(Replace(#a,'-','.'),2)
,y=ParseName(Replace(#a,'-','.'),1)
Returns
x y
001A01 001A05
try this using LEFT and RIGHT
DECLARE #str varchar(max)= '001A01-001A05'
select left(#str, charindex('-', #str)-1),
right(#str, charindex('-', #str)-1)

How to remove a specific character from the string

Using SQL Server 2008
String like: 'C/123232323' or '/343434343443' or 'C2323232322'
From the string i want to remove C and /
Tried Query
Select replace ('/1233434', 'C/', '')
The above query is working if C/ both is there. if / only there then the replace is not working. if C only there then the replace is not working. How to achieve for both condition
Expected output
123232323
343434343443
2323232322
Need Query output
You can achieve this by nesting replace() like so:
select replace(replace('C/12341234','/',''),'C','')
Probably not the prettiest but it works :)
You could use two nested REPLACE:
WITH SampleData(string) AS(
SELECT 'C/123232323' UNION ALL
SELECT '/343434343443' UNION ALL
SELECT 'C2323232322'
)
SELECT REPLACE(REPLACE(string,'C',''),'/','')
FROM SampleData
Use Patindex + Substring
DECLARE #str VARCHAR(50)='/343434343443' ---or '/343434343443' or 'C2323232322'
SELECT Substring(#str, Patindex('%[0-9]%', #str), Len(#str))