How to split string by forward slash in SQL Server? [duplicate] - sql

This question already has answers here:
How do I split a delimited string so I can access individual items?
(46 answers)
Closed 4 years ago.
There is no default split function in SQL Server 2012 that I'm using.
I want to split the string (ex: /Folder1/Folder2/) by /.
if string is /Folder1/ then output should be Folder1,
if string is /Folder1/Folder2/ then output should be Folder2,
if string is /Folder1/Folder2/Folder3/ then output should be Folder3.

Try this:
declare #tbl table (path varchar(100));
insert into #tbl values
('/Folder1/'),
('/Folder1/Folder2/'),
('/Folder1/Folder2/Folder3/');
select *,
replace(substring(path, len(path) - charindex('/', reverse(path), 2) + 1, 1000), '/', '')
from #tbl

Related

how to get the string before first occurrence of a special character [duplicate]

This question already has answers here:
How to Select a substring in Oracle SQL up to a specific character?
(8 answers)
Closed 1 year ago.
i have a column containing hostnames in the format of :
oraclehost.server.region.company.net
How to extract the oraclehost part from the hostname i.e the string before the first ..
Please sugges.Thanks.
SELECT REGEXP_SUBSTR(HOSTNAMES, '[^.]+', 1, 1) FROM MYTABLE;
Alternatively, substr + instr combination which would probably perform better for large data sets:
substr(hostnames, 1, instr(hostnames, '.') - 1)
For example:
SQL> with mytable (hostnames) as
2 (select 'oraclehost.server.region.company.net' from dual)
3 select substr(hostnames, 1, instr(hostnames, '.') - 1) result
4 from mytable;
RESULT
----------
oraclehost
SQL>

SQL server split string into columns by delimiter (dynamic length) [duplicate]

This question already has answers here:
how to separate string into different columns?
(5 answers)
How to split a comma-separated value to columns
(38 answers)
Closed 3 years ago.
SQL server cannot use MySQL split_index function, and my environment face accessibility blocking to use some function on server like "CREATE" "INSERT"
Are there any method to split strings by fixed delimiter into columns ?
Has 3 delimiters but length is dynamic.
e.g.
STRING : sometimes - "AA.0.HJ", sometimes - "AABBCC.099.0",sometimes - "0.91.JAH21"
The combinations of substring is not work.
SUBSTRING(STRING ,
CHARINDEX('.', STRING )+1,
LEN(STRING )-CHARINDEX('.', STRING )
Origin:
STRING
AA.0.HJ
AABBCC.099.0
0.91.JAH21
Target :
STRING First Second Third
AA.0.HJ AA 0 HJ
AABBCC.099.0 AABBCC 099 0
0.91.JAH21 0 91 JAH21
What is the solution in this situation ?
An xml-based solution
declare #tmp table (STRING varchar(500))
insert into #tmp
values
('AA.0.HJ')
,('AABBCC.099.0')
,('0.91.JAH21')
;WITH Splitted
AS (
SELECT STRING
,CAST('<x>' + REPLACE(STRING, '.', '</x><x>') + '</x>' AS XML) AS Parts
FROM #tmp
)
SELECT STRING
,Parts.value(N'/x[1]', 'varchar(50)') AS [First]
,Parts.value(N'/x[2]', 'varchar(50)') AS [Second]
,Parts.value(N'/x[3]', 'varchar(50)') AS [Third]
FROM Splitted;
Output:
You can use parsename
Declare #t table (name varchar(50))
insert into #t values ('AA.0.HJ')
insert into #t values ('AABBCC.099.0')
select parsename(name,3),parsename(name,2),parsename(name,1) from #t

extract word from string in sql server [duplicate]

This question already has answers here:
Find a specific substring using Transact-SQL
(6 answers)
Closed 5 years ago.
I need to extract part of a string in sql server. Lets say I have this string in a column...
Name1=Bill Gates&Name2=Microsoft&Address1=The streetadress
How can I extract the text that is equal to Name2 eg Microsoft?
declare #string varchar(100) = 'Name1=Bill Gates&Name2=Microsoft&Address1=The streetadress'
select replace(PARSENAME (replace(#string, '&', '.'), 2), 'Name2=', '');
One old fashioned way of handling this is to just use basic string functions like CHARINDEX and SUBSTRING.
SELECT
SUBSTRING(col,
CHARINDEX('Name2', col) + 6,
CHARINDEX('&', col, CHARINDEX('Name2', col)) -
CHARINDEX('Name2', col) - 6) AS name
FROM yourTable
Note that this solution assumes that the key value pairs are fixed in the order you showed us. My query uses the ambersand after the second name as a termination marker. If this be not present, e.g. if Name2 could possibly the last key in the string, then my query would have to be updated.
Demo here:
Rextester

Split string into new column [duplicate]

This question already has answers here:
How do I split a delimited string so I can access individual items?
(46 answers)
Closed 7 years ago.
There is a column containing following e.g. abcd/ef/g/hij.
Characters between the / are dynamic not fix.
I want to split in a select query the content into 4 separate new columns.
The already answered question is different, I want to split the content in a string seperated by / into new columns.
You can use REPLACE to replace '/' with '.'. Then use PARSENAME to get each separate part of the string:
CREATE TABLE #tmp (str VARCHAR(50))
INSERT INTO #tmp VALUES
('abcd/ef/g/hij'),
('1111111/222/33/4444')
SELECT PARSENAME(x.s, 4) AS [1], PARSENAME(x.s, 3) AS [2],
PARSENAME(x.s, 2) AS [3], PARSENAME(x.s, 1) AS [4]
FROM #tmp
CROSS APPLY (SELECT REPLACE(str, '/', '.')) AS x(s)
Output:
1 2 3 4
---------------------
abcd ef g hij
1111111 222 33 4444
If you ask me, fastest ad-hoc method would be to turn your data into xml and use nodes() method:
declare #temp table (data nvarchar(max))
insert into #temp
select 'abcd/ef/g/hij' union all
select '1/2/3'
select t.data, n.c.value('.', 'nvarchar(max)')
from #temp as t
outer apply (select cast('<t>' + replace(t.data, '/', '</t><t>') + '</t>' as xml) as data) as d
outer apply d.data.nodes('t') as n(c)
You need to find the position of the / characters using CHARINDEX and slice the string up that way. It will be a large expression, because to find the third slash, you need to use the 3rd parameter of CHARINDEX, passing the result of another CHARINDEX, which also has its 3rd parameter being used. Except for the last (fourth) fragment, you also need to use CHARINDEX to find and remove text after the next slash.
Something like this will extract the text after the third slash:
RIGHT(s, CHARINDEX('/', s, CHARINDEX('/', s, CHARINDEX('/', s)+1)+1)+1)
I leave the rest to you.

How to concatenate multiple rows into one field in sql server [duplicate]

This question already has answers here:
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 8 years ago.
Using simple query , I can do something like
SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;
and get:
shopping
fishing
coding
but instead I just want 1 row, 1 col:
shopping, fishing, coding
for ref-- Can I concatenate multiple MySQL rows into one field?
I want to do this in sql server ??
SQL Server doesn't have great support for aggregate string concatenation. But you can do:
select stuff((select ', ' + hobbies
from peoples_hobbies
where person_id = 5
for xml path ('')
), 1, 2, '') as hobbies;