Skip to content
Advertisement

Base on SQL column content insert the data into another table

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

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

output table

From below data logic for inserting into output CustomerContentTemp table

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

Advertisement

Answer

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.

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.

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

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement