0

I have below table name: CustomerContent with the below image data and My product is:is common in ProductContent cell. First colon no need to split (My product is:) if next colons we need to split the ProductContent Cell base text assign the values like below. If slip data content CGM then assign value 37.

my table

CustomerId  ProductContent
100         My product is: Shoes
101         My product is: Diabetic Shoes
102         My product is: Shoes Back Brace
103         My product is: Dexcom G6 (CGM)
104         My product is: Freestyle Libre (CGM)
105         My product is: Shoes Knee Brace
106         My product is: Dexcom G6 (CGM): Freestyle Libre (CGM): Diabetic Shoes
107         My product is: Dexcom G6 (CGM): Freestyle Libre (CGM)
108         My product is: Freestyle Libre (CGM): Diabetic Shoes

I need output like below and insert above data into another table name: CustomerContentTemp contain columns CusmerId and Values like below format.

output table

CustomerId  Values
100         1
101         1
102         8
103         37
104         37
105         14
106         37
106         37
106         1
107         37
107         37
108         37
108         1

From below data logic for inserting into output CustomerContentTemp table

Shoes=1
Diabetic Shoes=1
Shoes Back Brace=8
Dexcom G6 (CGM)=37
Freestyle Libre (CGM)=37
Shoes Knee Brace=14

IF ProductContent cell data is not match then insert value 0.

coder rock
  • 113
  • 6
  • As per the question guide, please do not post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. – Dale K Nov 22 '21 at 19:51
  • Aside from being images which is difficult to work with, your input and output here make no sense at all. But it also seems like you have violated 1NF in your original data by storing multiple values in a single tuple. And worse it seems you want to continue this in your output. – Sean Lange Nov 22 '21 at 19:52
  • i have update question. @DaleK – coder rock Nov 22 '21 at 19:58
  • i have update question. @SeanLange – coder rock Nov 22 '21 at 19:58
  • Your data still doesn't make any sense. Where do these values come from? (1, 8, 37 etc...) – Sean Lange Nov 22 '21 at 20:01
  • We need to assign statically those values and can you last image which you will understand for understanding purpose last image i have added. @SeanLange – coder rock Nov 22 '21 at 20:03
  • Or is that third set of data where those values come from? If that is the case you need to first split your denormalized data on semicolons. Throw out the first value in the split results. Then join to the other table and stuff all that denormalized mess back into your table. – Sean Lange Nov 22 '21 at 20:03
  • yes split then check the content then assign values statatically. @SeanLange – coder rock Nov 22 '21 at 20:04
  • Hands down the biggest issue here is normalization. Storing values like that is just dreadful and incredibly painful to work with. – Sean Lange Nov 22 '21 at 20:06
  • There are lots of ways to split strings. Here is a great article on the topic. https://sqlperformance.com/2021/09/t-sql-queries/split-strings – Sean Lange Nov 22 '21 at 20:06
  • looks like a `left join` and `stuff` to solve your issue.. this OP is not new. – RoMEoMusTDiE Nov 22 '21 at 20:10
  • Does this answer your question? [How Stuff and 'For Xml Path' work in SQL Server?](https://stackoverflow.com/questions/31211506/how-stuff-and-for-xml-path-work-in-sql-server) – RoMEoMusTDiE Nov 22 '21 at 20:10
  • No please can you post your sample answer. @RoMEoMusTDiE – coder rock Nov 22 '21 at 20:14
  • @MazharKhan as I mentioned.. `LEFT JOIN` and `STUFF` – RoMEoMusTDiE Nov 22 '21 at 20:15
  • 1
    Just use replace to remove that prefix from your data. Honestly the more I look at this the more ridiculous it is. You have denormalized data in every table. If you had properly designed your database this entire problem you are facing would not be an issue. – Sean Lange Nov 22 '21 at 20:22
  • Here's the deal. I don't mind helping. But you are basically asking for somebody to do all the work for you. You were shown several places where you can learn how to do all this. There are multiple steps involved here. – Sean Lange Nov 22 '21 at 20:31

1 Answers1

0

My personal choice for a t-sql splitter is this one. https://www.sqlservercentral.com/articles/reaping-the-benefits-of-the-window-functions-in-t-sql-2 There are plenty of other choices and many have advantages and disadvantages. This one for me is the most robust with the least overhead and confusion. To find the code yourself you will need to scroll to the bottom of the article (ideally reading the whole thing so you understand what it is doing).

Now let's take your data and make it consumable so anybody can just copy and paste it. This article (How Stuff and 'For Xml Path' work in SQL Server?) will walk you through that part of the process.

create table CustomerContent
(
    CustomerId int
    , ProductContent varchar(500)
)

insert CustomerContent
select 100, 'My product is: Shoes' union all
select 101, 'My product is: Diabetic Shoes' union all
select 102, 'My product is: Shoes Back Brace' union all
select 103, 'My product is: Dexcom G6 (CGM)' union all
select 104, 'My product is: Freestyle Libre (CGM)' union all
select 105, 'My product is: Shoes Knee Brace' union all
select 106, 'My product is: Dexcom G6 (CGM): Freestyle Libre (CGM): Diabetic Shoes' union all
select 107, 'My product is: Dexcom G6 (CGM): Freestyle Libre (CGM)' union all
select 108, 'My product is: Freestyle Libre (CGM): Diabetic Shoes'

create table CustomerContentTemp
(
    SimpleName varchar(100)
    , Result int
)

insert CustomerContentTemp
select 'Shoes', 1 union all
select 'Diabetic Shoes', 1 union all
select 'Shoes Back Brace', 8 union all
select 'Dexcom G6 (CGM)', 37 union all
select 'Freestyle Libre (CGM)', 37 union all
select 'Shoes Knee Brace', 14

OK. So now have nice and easy to work with data and a splitter function. Here is the first step in your puzzle. You have to split this data apart and get rid of that oddball prefix.

select c.*
    , cct.SimpleName
    , cct.Result
from CustomerContent c
cross apply dbo.DelimitedSplit8K_LEAD(replace(c.ProductContent, 'My product is: ', ''), ':') x
left join CustomerContentTemp cct on cct.SimpleName = ltrim(x.Item)

From this result you will need to apply the logic for using STUFF to get this squished back into the denormalized format you need.

Sean Lange
  • 33,028
  • 3
  • 25
  • 40