extract word from string in sql server [duplicate] - sql

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

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>

Query to pad left of a field with 0's [duplicate]

This question already has answers here:
Formatting Numbers by padding with leading zeros in SQL Server
(14 answers)
Closed 7 years ago.
I have been working on a query (in sql Server TSQL) which fills left of a number with 0's so output is always 5 digit.
So:
Select MuNumber From Mytable
for data 11,011,2132,1111
Creates output like
00011
02134
01111
I tried Lpad Function but numer of 0's can be different.
if Munumber is 1 we need 0000 and If MyNumber is 34 we need 000
Assuming that MuNumber is VARCHAR simply use RIGHT
SELECT RIGHT('00000' + MuNumber, 5)
FROM Mytable
Otherwise you need to convert it first
SELECT RIGHT('00000' + CONVERT(VARCHAR(5), MuNumber), 5)
FROM Mytable
And in general you can use this pattern:
DECLARE #num INT = 10;
SELECT RIGHT(REPLICATE('0', #num) + CONVERT(VARCHAR(5), MuNumber), #num)
FROM Mytable
Try this
select right('00000'+cast(col as varchar(5)),5) from table
You can use the user defined function udfLeftSQLPadding where you can find the source codes at SQL Pad Leading Zeros
After you create the function on your database, you can use it as follows
select
dbo.udfLeftSQLPadding(MuNumber,5,'0')
from dbo.Mytable
Another option:
declare #n int = 6
select stuff(replicate('0', #n), 6-len(n), len(n), n)
from (values('123'), ('2493'), ('35')) as x(n)

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;

Split data from one colunn into two new columns in SQL on multiple rows [duplicate]

This question already has answers here:
How to split a comma-separated value to columns
(38 answers)
Closed 8 years ago.
I'm using SQL Express 2012, and I'm rather new to it so this website has been a blessing so far! I'm now stuck on a query that I've not found a suitable answer to.
I have a table called Claims Passed. In this I have a column called Client_Name, in this is a list of names, these contain first and second names split by a space (e.g John Smith). I've created two new columns, Client_First_Name and Client_Surname.
What I'm trying to do is get the get the first name in to first name column and the surname into the surname column.
I came across something like this but it was only for one row, not all the rows in one go.
How can i do this?
This is the basic syntax you need, assuming each name has only one first name:
UPDATE [Claims Passed]
SET Client_First_Name = SUBSTRING(Client_Name, 1, CHARINDEX(' ', Client_Name) - 1),
Client_Surname = SUBSTRING(Client_Name,CHARINDEX(' ', Client_Name) + 1, LEN(Client_Name)
The problem on your approach is when a client has more than one surname and one last name this won't work. You could try something like this:
INSERT INTO Client_First_Name VALUES(SELECT Client_Name LIKE '% %' THEN LEFT(Client_Name, Charindex(' ', Client_Name) - 1));
INSERT INTO Client_Surname VALUES(SELECT Client_Name LIKE '% %' THEN RIGHT(Client_Name, Charindex(' ', Reverse(Client_Name)) - 1));