Replace Matching Row String From Another Column - sql

I have this table:
ID NewName OldName Link
1 NewName1 OldName1 OldName2|OldName3
2 NewName2 OldName2 OldName1|OldName3
3 NewName3 OldName3 OldName1|OldName2
What I want to happen is to change all the OldName on the Link column to the NewName. Like this:
ID NewName OldName Link
1 NewName1 OldName1 NewName2|NewName3
2 NewName2 OldName2 NewName1|NewName3
3 NewName3 OldName3 NewName1|NewName2
Can anyone suggest what's the best way to do this?

You are looking to Change the value of Link according OldNames to its with New Names:
First you will need to split your Link data delimited by |into row & then Join with Your Table
SELECT TTT.ID,
TTT.[NewName],
TTT.OldName,
[Link] = STUFF(
(
SELECT
'|'+[Link]
FROM
(
SELECT AA.ID,
AA.[NewName],
AA.OldName,
T.[NewName] [Link]
FROM
(
SELECT ID,
NewName,
OldName,
split.x.value('.', 'NVARCHAR(MAX)') DATA
FROM
(
SELECT ID,
NewName,
OldName,
CAST('<M>'+REPLACE(Link, '|', '</M><M>')+'</M>' AS XML) AS String
FROM <table_name>
) AS a
CROSS APPLY String.nodes('/M') AS split(x)
) AA
INNER JOIN <table_name> T ON T.OldName = AA.DATA
) TT
WHERE TT.ID = TTT.ID FOR XML PATH('')
), 1, 1, '')
FROM <table_name> TTT;
Result :
ID NewName OldName Link
1 NewName1 OldName1 NewName2|NewName3
2 NewName2 OldName2 NewName1|NewName3
3 NewName3 OldName3 NewName1|NewName2

MSSQL:
To store delimited-values from other column/other sources to a specific row, you may use FOR XML PATH, look at this SO thread:
UPDATE YourTable
SET Link = SUBSTRING(
(
SELECT '|' + T2.NewName
FROM YourTable T2
WHERE '|'+T2.Link+'|' LIKE '%|'+YourTable.OldName+'|%'
FOR XML PATH ('')
), 2, 1000);
MYSQL:
If you want to store delimited-values from other column/other sources to a specific row, you may use mysql GROUP_CONCAT function:
UPDATE table t1
SET Link = (
SELECT GROUP_CONCAT(t2.NewName SEPARATOR '|')
FROM table t2 WHERE FIND_IN_SET(t2.OldName, REPLACE(t1.Link, '|', ','))
)
I assumed that you want to replace any old values in the Link column with its new value.
See the results in action on dbfiddle.uk

If the Link column have only two names always, then we try self joining twice to match the respective new names which should be used for replacement. The join condition is ugly, but this is the price paid for storing denormalized data in your table.
WITH cte AS
(
SELECT t1.Link, t2.NewName AS NewNameLeft, t3.NewName AS NewNameRight
FROM yourTable t1
LEFT JOIN yourTable t2
ON SUBSTRING(t1.Link, 1, CHARINDEX('|', t1.Link) - 1) = t2.OldName
LEFT JOIN yourTable t3
ON SUBSTRING(t1.Link,
CHARINDEX('|', t1.Link) + 1,
LEN(t1.Link) - CHARINDEX('|', t1.Link)) = t3.OldName
)
UPDATE cte
SET Link = NewNameLeft + '|' + NewNameRight
WHERE NewNameLeft IS NOT NULL AND NewNameRight IS NOT NULL;
Note that this answer assumes that each old name appears only once in the table. I default to not doing an update unless both left and right new names are found.

I guess you just need REPLACE
select id, NewName, OldName, replace(link, 'OldName', 'NewName') Link
from your_data
and if you need to do it directly in table then use
update your_data
set link = replace(link, 'OldName', 'NewName')

Related

Concatening in a specific order given by a number

I have a table like this:
I want to concatenate the Product name in the given Product_order by ID.
Therefore, I should get something like: CC-TC-CA for ID 1.
you can use string_agg()- it'll work sql server 2016+
select id, string_Agg(product,',') as pname
from tablename
group by id
OR you can use stuff()
SELECT id,
STUFF((SELECT ',' + product
FROM tablename AS T1
WHERE T1.id = T2.id
FOR XML PATH('')), 1, 1, '')
FROM tablename AS T2
GROUP BY id
If you can use a stored procedure instead of a single query the += operator can do what you're looking for.
DECLARE #Product_order VARCHAR(100) = '';
SELECT #Product_order += Product + '-' FROM [table] WHERE id = 1 ORDER BY Product_Order;
SELECT SUBSTRING(#Product_order, 0, LEN(#Product_order));
Update: I've learned that returning multiple rows and using in an assignment in the select clause is unsupported behavior in SQL Server.

How to combine return results of query in one row

I have a table that save personnel code.
When I select from this table I get 3 rows result such as:
2129,3394,3508,3534
2129,3508
4056
I want when create select result combine in one row such as:
2129,3394,3508,3534,2129,3508,4056
or distinct value such as:
2129,3394,3508,3534,4056
You should ideally avoid storing CSV data at all in your tables. That being said, for your first result set we can try using STRING_AGG:
SELECT STRING_AGG(col, ',') AS output
FROM yourTable;
Your second requirement is more tricky, and we can try going through a table to remove duplicates:
WITH cte AS (
SELECT DISTINCT VALUE AS col
FROM yourTable t
CROSS APPLY STRING_SPLIT(t.col, ',')
)
SELECT STRING_AGG(col, ',') WITHIN GROUP (ORDER BY CAST(col AS INT)) AS output
FROM cte;
Demo
I solved this by using STUFF and FOR XML PATH:
SELECT
STUFF((SELECT ',' + US.remain_uncompleted
FROM Table_request US
WHERE exclusive = 0 AND reqact = 1 AND reqend = 0
FOR XML PATH('')), 1, 1, '')
Thank you Tim

Getting Number of Common Values from 2 comma-seperated strings

I have a table that contains comma-separated values in a column In Postgres.
ID PRODS
--------------------------------------
1 ,142,10,75,
2 ,142,87,63,
3 ,75,73,2,58,
4 ,142,2,
Now I want a query where I can give a comma-separated string and it will tell me the number of matches between the input string and the string present in the row.
For instance, for input value ',142,87,', I want the output like
ID PRODS No. of Match
------------------------------------------------------------------------
1 ,142,10,75, 1
2 ,142,87,63, 2
3 ,75,73,2,58, 0
4 ,142,2, 1
Try this:
SELECT
*,
ARRAY(
SELECT
*
FROM
unnest(string_to_array(trim(both ',' from prods), ','))
WHERE
unnest = ANY(string_to_array(',142,87,', ','))
)
FROM
prods_table;
Output is:
1 ,142,10,75, {142}
2 ,142,87,63, {142,87}
3 ,75,73,2,58, {}
4 ,142,2, {142}
Add the cardinality(anyarray) function to the last column to get just a number of matches.
And consider changing your database design.
Check This.
select T.*,
COALESCE(No_of_Match,'0')
from TT T Left join
(
select ID,count(ID) No_of_Match
from (
select ID,unnest(string_to_array(trim(t.prods, ','), ',')) A
from TT t)a
Where A in ('142','87')
group by ID
)B
On T.Id=b.id
Demo Here
OutPut
If you install the intarray extension, this gets quite easy:
select id, prods, cardinality(string_to_array(trim(prods, ','), ',')::int[] & array[142,87])
from bad_design;
Otherwise it's a bit more complicated:
select bd.id, bd.prods, m.matches
from bad_design bd
join lateral (
select bd.id, count(v.p) as matches
from unnest(string_to_array(trim(bd.prods, ','), ',')) as l(p)
left join (
values ('142'),('87') --<< these are your input values
) v(p) on l.p = v.p
group by bd.id
) m on m.id = bd.id
order by bd.id;
Online example: http://rextester.com/ZIYS97736
But you should really fix your data model.
with data as
(
select *,
unnest(string_to_array(trim(both ',' from prods), ',') ) as v
from myTable
),
counts as
(
select id, count(t) as c from data
left join
( select unnest(string_to_array(',142,87,', ',') ) as t) tmp on tmp.t = data.v
group by id
order by id
)
select t1.id, t1.prods, t2.c as "No. of Match"
from myTable t1
inner join counts t2 on t1.id = t2.id;

How to merge two columns from CASE STATEMENT of DIFFERENT CONDITION

My expected result should be like
----invoiceNo----
T17080003,INV14080011
But right now, I've come up with following query.
SELECT AccountDoc.jobCode,AccountDoc.shipmentSyskey,AccountDoc.docType,
CASE AccountDoc.docType
WHEN 'M' THEN
JobInvoice.invoiceNo
WHEN 'I' THEN
(STUFF((SELECT ', ' + RTRIM(CAST(AccountDoc.docNo AS VARCHAR(20)))
FROM AccountDoc LEFT OUTER JOIN JobInvoice
ON AccountDoc.principalCode = JobInvoice.principalCode AND
AccountDoc.jobCode = JobInvoice.jobCode
WHERE (AccountDoc.isCancelledByCN = 0)
AND (AccountDoc.docType = 'I')
AND (AccountDoc.jobCode = #jobCode)
AND (AccountDoc.shipmentSyskey = #shipmentSyskey)
AND (AccountDoc.principalCode = #principalCode) FOR XML
PATH(''), TYPE).value('.','NVARCHAR(MAX)'),1,2,' '))
END AS invoiceNo
FROM AccountDoc LEFT OUTER JOIN JobInvoice
ON JobInvoice.principalCode = AccountDoc.principalCode AND
JobInvoice.jobCode = AccountDoc.jobCode
WHERE (AccountDoc.jobCode = #jobCode)
AND (AccountDoc.isCancelledByCN = 0)
AND (AccountDoc.shipmentSyskey = #shipmentSyskey)
AND (AccountDoc.principalCode = #principalCode)
OUTPUT:
----invoiceNo----
T17080003
INV14080011
Explanation:
I want to select docNo from table AccountDoc if AccountDoc.docType = I.
Or select invoiceNo from table JobInvoice if AccountDoc.docType = M.
The problem is what if under same jobCode there have 2 docType which are M and I, how I gonna display these 2 invoices?
You can achieve this by using CTE and FOR XML. below is the sample code i created using similar tables you have -
Create table #AccountDoc (
id int ,
docType char(1),
docNo varchar(10)
)
Create table #JobInvoice (
id int ,
invoiceNo varchar(10)
)
insert into #AccountDoc
select 1 , 'M' ,'M1234'
union all select 2 , 'M' ,'M2345'
union all select 3 , 'M' ,'M3456'
union all select 4 , 'I' ,'I1234'
union all select 5 , 'I' ,'I2345'
union all select 6 , 'I' ,'I3456'
insert into #JobInvoice
select 1 , 'INV1234'
union all select 2 , 'INV2345'
union all select 3 , 'INV3456'
select *
from #AccountDoc t1 left join #JobInvoice t2
on t1.id = t2.id
with cte as
(
select isnull( case t1.docType WHEN 'M' THEN t2.invoiceNo WHEN 'I' then
t1.docNo end ,'') invoiceNo
from #AccountDoc t1 left join #JobInvoice t2
on t1.id = t2.id )
select invoiceNo + ',' from cte For XML PATH ('')
You need to pivot your data if you have situations where there are two rows, and you want two columns. Your sql is a bit messy, particularly the bit where you put an entire select statement inside a case when in the select part of another query. These two queries are virtually the same, you should look for a more optimal way of writing them. However, you can wrap your entire sql in the following:
select
Jobcode, shipmentsyskey, [M],[I]
from(
--YOUR ENTIRE SQL GOES HERE BETWEEN THESE BRACKETS. Do not alter anything else, just paste your entire sql here
) yoursql
pivot(
max(invoiceno)
for docType in([M],[I])
)pvt

Grouping file paths in a Microsoft SQL database

I have a table with a list of folders that looks like this:
Path Size
C:\ParentFolder\A 123
C:\ParentFolder\A\B 442434
C:\ParentFolder\A\B\C 13413412
C:\ParentFolder\D 2422341234
C:\ParentFolder\D\E 3342
C:\ParentFolder\D\E\F 2
C:\ParentFolder\D\E\G 2
...
I'm looking for some combination of SUM, GROUP BY, and PATINDEX/LTRIM/SUBSTRING/etc. which would give me back this:
Path SumSize
C:\ParentFolder\A 13855969
C:\ParentFolder\D 2422344580
...
C:\ParentFolder is a known prefix, but A,D,etc. are variable folder names. Do I need to write a function to accomplish that or can I use some combination of string functions?
select r.Path, sum(Size) as SumSize
from MyTable m
inner join (
select Path
from MyTable
where charindex('\', Path, len('C:\ParentFolder\') + 1) = 0
) r on charindex(r.Path, m.Path, 0) = 1
group by r.Path
SQL Fiddle example here
Starting with your testing set,
CREATE TABLE #MyTable (Folder varchar(100) not null, Size bigint not null)
INSERT #MyTable values
('C:\ParentFolder\A' , 123)
,('C:\ParentFolder\A\B' , 442434)
,('C:\ParentFolder\A\B\C' , 13413412)
,('C:\ParentFolder\D' , 2422341234)
,('C:\ParentFolder\D\E' , 3342)
,('C:\ParentFolder\D\E\F' , 2)
,('C:\ParentFolder\D\E\G' , 2)
you'd first determine what folders you want to summarize. I do so here by loading them into a temp table:
DECLARE #Targets table (Folder varchar(100) not null)
INSERT #Targets values
('C:\ParentFolder\A')
,('C:\ParentFolder\D')
From here it's easy, using the like clause:
SELECT ta.Folder, sum(Size) TotalSize
from #Targets ta
left outer join #MyTable mt
on mt.Folder like ta.Folder + '%'
group by ta.Folder
Complications may ensue if your folders contain reserved characters used by the like clause: % _ ] [ and a few others.
Assuming that there is always an entry for the highest level dir (i.e if there is a c:\xxx\yyy\zzz there will always be a c:\xxx\yyy how about
;with roots (root) as (
select distinct
path + '\'
from
thetable
where
--only include paths with 2 x \
len(path) - 2 = len(replace(path, '\', ''))
)
select
roots.root,
sum(thetable.size)
from
roots
inner join
thetable on left(thetable.path + '\', len(roots.root)) = roots.root
group by
roots.root
--If the folder name is always one character
select
LEFT(folder,CHARINDEX('r\',folder)+2) as folder_group
,SUM(size) as sumsize
from #mytable
GROUP BY
LEFT(folder,CHARINDEX('r\',folder)+2)
--If the folder name has a variable length
select
CASE WHEN CHARINDEX('\',folder,CHARINDEX('\',folder,CHARINDEX('\',folder)+1)+1) = 0 THEN folder
ELSE LEFT(folder,CHARINDEX('\',folder,CHARINDEX('\',folder,CHARINDEX('\',folder)+1)+1) -1) END AS folder_group
,SUM(size) as sumsize
from #mytable
GROUP BY
CASE WHEN CHARINDEX('\',folder,CHARINDEX('\',folder,CHARINDEX('\',folder)+1)+1) = 0 THEN folder
ELSE LEFT(folder,CHARINDEX('\',folder,CHARINDEX('\',folder,CHARINDEX('\',folder)+1)+1) -1) END
select path, (select sum(Size)
from Paths p2 where p2.Path like p1.Path+'%') as total
from Paths p1
where charIndex('\',Path, len('C:\ParentFolder\')+1) = 0