Forum Discussion
Unpack and Transform Unidata Values
Hi team
New to this community and hoping please for some assistance. I have tried to work this out myself to no avail.
We have an SQL database that is connected to a unidata database. One of the tables is PURCHASES. This table stores Purchase Order header details and also the line level detail. It packs up the line details with a concatenation so if you want to view the line detail you have to unpack the concatenated value from the table into a view. For instance, purchase order 31935 has 4 purchase order lines and the system packs up the data like this (note delimeter is ý) (column name is v8);
206-5234; Primary Standard Efficiency Engine Air Filterý206-5235; Secondary Standard Efficiency Engine Air Filterý299-8229; FILTER AS-FUELýPLACED ONLINE - 00C280438
The system also has a column v120 which represents part number.
With all of that context out of the way - here is what I am trying to acheive;
1. Using vales from v8: 206-5234; Primary Standard Efficiency Engine Air Filterý206-5235; Secondary Standard Efficiency Engine Air Filterý299-8229; FILTER AS-FUELýPLACED ONLINE - 00C280438
2. update v120 with this; 206-5234ý206-5235ý299-8229ýPLACED ONLINE - 00C280438
Essentially take the text from the left of each semicolon (;) and remove other text to leave clean part numbers seperated with the delimeter ý
I hope this makes some form of sense. Hoping that this presents a challenge that someone is willing to assit with. Any assistance would be greatly appreciated.
Cheers Chris
- rodgerkongIron Contributor
I can only find a way with SQL batch or user define function, cannot resolve it with single SQL. This is the method:
1. Find delimeter ý repeatly to split lines, without it means reach the end of content;
2. Find ; in line as field delimeter, part befor it is the part number which you need. If a line has not ; ,means whole line is part number. If no character before delimeter -- like ";FILTER AS-FUEL", means this dose be a line, but has not a part number.
3. Repeat step 2 in lines, pickup part numbers and concat them with delimeter ý to result.
4. Build a user define function with input nvarchar parameter and returns a nvarchar.
5. Update column V120 by the udf returns, with V8 as param.
Here is the code of udf
CREATE FUNCTION ufn_makeV120(@source NVARCHAR(MAX))--replace MAX to your v8 column size RETURNS NVARCHAR(MAX)--replace MAX to your v120 column size AS BEGIN DECLARE @line INT DECLARE @col INT DECLARE @pos INT DECLARE @len INT DECLARE @d NVARCHAR(MAX)--replace max to your v120 column size SET @line = 1 SET @col = 1 SET @pos = 1 SET @len = LEN(@source) SET @d = NULL WHILE ( ( @pos <= @len ) AND ( @len > 0 ) ) BEGIN SET @line = CHARINDEX( N'ý', @source, @pos ) SET @col = CHARINDEX( N';', @source, @pos ) IF (( @line <= 0 ) OR (@line < @col )) -- last line or no desc BEGIN IF ( @line <= 0 ) SET @line = @len + 1 IF ( @col <= 0 ) OR ( @line < @col ) --no desc SET @col = @line --copy to line end END ELSE BEGIN IF ( @col <= 0 ) --no desc SET @col = @line--copy to line end END IF (@d IS NULL) SET @d = SUBSTRING( @source, @pos, @col - @pos ) ELSE SET @d += CONCAT(N'ý', SUBSTRING( @source, @pos, @col - @pos )) SET @pos = @line + 1 END RETURN @d END
You can modify length of the parameter and returns to your need.
Then update table with SQL like this
UPDATE test SET V120 = dbo.ufn_makeV120(V8)
- cgrigg390Copper Contributor
Thankyou so much. I will digest that and give it a shot.
- rodgerkongIron ContributorThis method only work in SQL SERVER Database, If your table was in it, it will work. Unidata might have otherway else.