2

I have a skeleton of a code which I began to write for the following purpose:

use EAS
go
  declare @entityID,int
  declare @text,text
  declare @col1,varchar --col1 is between <b>col1</b>
  declare @col2,varchar   --col2 are between >col2</td>

  @entityID=entity_ID
  @text = maAttributes_Text.value
  @col1 = select SUBSTRING(@text,CHARINDEX('<b>',@text)+len('<b>'),
  charindex('</b>',@text))
  @col2 = select substring(@text, CHARINDEX('>',@text)+LEN('>'),charindex('</td>',@text))

  begin
  add to `table2` [entity_id], [caption], [value]
  @entityID, @col1, @col2
  end

I need to access a different table, table1, which contains fields entity_id, caption and value, as well as a few others which are unimportant.

entity_id | caption      | value
----------|--------------|-------
12345     |Specifications| *html snippet*

What my code is supposed to do is, to find rows in table1 which have the value of 'Specifications' in field caption, then extract the entity_id value, store it, and then run a loop which takes all the rows of the html table and adds the info into another sql table.

For example, after running the code for one line of table1, table2 will look like as follows:

table2
entity_ID|Caption|Value
---------|-------|-----
12345    |length |10
12345    |width  |10
12345    |height |10
et al.

Where all the information in table2 came from the html snippet which is stored in value field of table1.

My instincts tell me that I should use both a loop (I cannot make a loop in sql for the life of me) and the STRING_SPLIT function. However, I am not sure how to implement them.

After finishing the first row of table1, it will continue to the next row and the next row, as many rows WHERE caption in ('specifications')

EDIT: I understand my wording was pretty confusing. I will try to reexplain.

I have table1 which contains an ID number which is tied to an html snippet. The html snippet is the code for a table which contains many rows and two columns. My desired function will take the html snippet and parse it by row, and add it to table2 where the one ID number from table1 will be attached to the several rows of the html table. Therefore when the first few rows of the HTML SNIPPET are extracted, table2 will appear as so:

table2
entity_ID|Caption|Value
---------|-------|-----
12345    |length |10
12345    |width  |10
12345    |height |10  

wherein entity_ID is the unique ID number, and it is tied to several rows from within the HTML snippet. Caption is taken from col1 of the html snippet, and Value is taken from col2 of the html snippet.

EDIT 2:
A simple example of my situation:

table1

ID|Caption       |Value  
--|--------------|-----  
1 |Specifications|*html snippet*  
2 |Specifications|*html snippet2*  
etc.

the code will take information from table1 and put it into `table2' as follows:

table2

ID|SpecName|Spec    
--|--------|----  
1 |Length  |10  
1 |Width   |10  
1 |Height  |10  

SpecName and Spec are fields from the html table that the html snippet created. Length, Width, Height are the information that the snippet is producing. 10, is the value for each of the SpecNames.

<table  class="inline_sd_table"> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Accessories Optional</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Accommodates Ceiling Height</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>ADA Compliance</b></td>      <td class="inline_sd_even_cell">See Additional Information above</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Blower Air Mover Type</b></td>      <td class="inline_sd_odd_cell">Fan</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Blower Included</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Boost Mode</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>CFM (Airflow)</b></td>      <td class="inline_sd_even_cell">190</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Color/Finish</b></td>      <td class="inline_sd_odd_cell">Almond</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Control Feature Filter Reminder</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Damper Included</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Delay Shut Off</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Digital Clock</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Dishwasher Safe Filters</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Duct Connector Size</b></td>      <td class="inline_sd_odd_cell">3-1/4" x 10", 7" Round Non-Ducted</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>ENERGY STAR&reg; qualified</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Filter Included</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Filter Type</b></td>      <td class="inline_sd_even_cell">Aluminum</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Flue Accommodates ceilings up to xx ft</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Heat Sentry - Temperature Sensor</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>HVI Certified</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Light Bulb Quantity</b></td>      <td class="inline_sd_even_cell">1</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Light Bulbs Included</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Lighting Function</b></td>      <td class="inline_sd_even_cell">One-level</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Lighting Type</b></td>      <td class="inline_sd_odd_cell">Incandescent</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Max Sones/100 CFM</b></td>      <td class="inline_sd_even_cell">4.1</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Minimum Height Above Cook Top</b></td>      <td class="inline_sd_odd_cell">18"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Mounting Type</b></td>      <td class="inline_sd_even_cell">Under Cabinet</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Nightlight</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Non-Stick Coated</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Product Depth (inches)</b></td>      <td class="inline_sd_odd_cell">17-1/2"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Product Height (inches)</b></td>      <td class="inline_sd_even_cell">6"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Product Width (inches)</b></td>      <td class="inline_sd_odd_cell">42"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Rated Amps</b></td>      <td class="inline_sd_even_cell">2.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Remote Control</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Horizontal Rectangular High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Horizontal Rectangular Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Vertical Rectangular High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Vertical Rectangular Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Vertical Round High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Vertical Round Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Switch Control Settings</b></td>      <td class="inline_sd_even_cell">2</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Switch Style</b></td>      <td class="inline_sd_odd_cell">Rocker</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Timer</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>UL Listed</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Voltage</b></td>      <td class="inline_sd_even_cell">120</td> </tr> </table> 

2
  • Are you creating or parsing HTML? Either way, SQL has XML manipulation functions which you should probably be using over string concatenation or substringing. Commented Aug 8, 2017 at 21:48
  • @Xedni I am parsing an html snippet which is stored in a cell of a sql table. Commented Aug 9, 2017 at 13:50

3 Answers 3

3

Your example is a little hard to follow, but does this help?

I modified a Parse/Split function to accept two non-like parameters. In this case '>' and '<'.

I should add, the TVF is very performant. The results are returned in 0.068 seconds.

Example

Declare @S varchar(max) = '<table  class="inline_sd_table"> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Accessories Optional</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Accommodates Ceiling Height</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>ADA Compliance</b></td>      <td class="inline_sd_even_cell">See Additional Information above</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Blower Air Mover Type</b></td>      <td class="inline_sd_odd_cell">Fan</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Blower Included</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Boost Mode</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>CFM (Airflow)</b></td>      <td class="inline_sd_even_cell">190</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Color/Finish</b></td>      <td class="inline_sd_odd_cell">Almond</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Control Feature Filter Reminder</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Damper Included</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Delay Shut Off</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Digital Clock</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Dishwasher Safe Filters</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Duct Connector Size</b></td>      <td class="inline_sd_odd_cell">3-1/4" x 10", 7" Round Non-Ducted</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>ENERGY STAR&reg; qualified</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Filter Included</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Filter Type</b></td>      <td class="inline_sd_even_cell">Aluminum</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Flue Accommodates ceilings up to xx ft</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Heat Sentry - Temperature Sensor</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>HVI Certified</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Light Bulb Quantity</b></td>      <td class="inline_sd_even_cell">1</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Light Bulbs Included</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Lighting Function</b></td>      <td class="inline_sd_even_cell">One-level</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Lighting Type</b></td>      <td class="inline_sd_odd_cell">Incandescent</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Max Sones/100 CFM</b></td>      <td class="inline_sd_even_cell">4.1</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Minimum Height Above Cook Top</b></td>      <td class="inline_sd_odd_cell">18"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Mounting Type</b></td>      <td class="inline_sd_even_cell">Under Cabinet</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Nightlight</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Non-Stick Coated</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Product Depth (inches)</b></td>      <td class="inline_sd_odd_cell">17-1/2"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Product Height (inches)</b></td>      <td class="inline_sd_even_cell">6"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Product Width (inches)</b></td>      <td class="inline_sd_odd_cell">42"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Rated Amps</b></td>      <td class="inline_sd_even_cell">2.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Remote Control</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Horizontal Rectangular High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Horizontal Rectangular Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Vertical Rectangular High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Vertical Rectangular Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Vertical Round High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Vertical Round Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Switch Control Settings</b></td>      <td class="inline_sd_even_cell">2</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Switch Style</b></td>      <td class="inline_sd_odd_cell">Rocker</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Timer</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>UL Listed</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Voltage</b></td>      <td class="inline_sd_even_cell">120</td> </tr> </table> '

Select Item  = max(case when RetCol=1 then RetVal end)
      ,Value = max(case when RetCol=0 then RetVal end)
 From (
        Select RetGrp=(Row_Number() over (Order By RetSeq)-1)/2
              ,RetCol=Row_Number() over (Order By RetSeq) % 2 
              ,RetPos
              ,RetVal
         From  [dbo].[udf-Str-Extract](@S,'>','<')
         Where RetVal>''
      ) A
 Group By RetGrp
 Order By RetGrp

Returns

enter image description here

The UDF if Interested

CREATE FUNCTION [dbo].[udf-Str-Extract] (@String varchar(max),@Delimiter1 varchar(100),@Delimiter2 varchar(100))
Returns Table 
As
Return (  

with   cte1(N)   As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
       cte2(N)   As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A ),
       cte3(N)   As (Select 1 Union All Select t.N+DataLength(@Delimiter1) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter1)) = @Delimiter1),
       cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter1,@String,s.N),0)-S.N,8000) From cte3 S)

Select RetSeq = Row_Number() over (Order By N)
      ,RetPos = N
      ,RetVal = left(RetVal,charindex(@Delimiter2,RetVal)-1) 
 From  (
        Select *,RetVal = Substring(@String, N, L) 
         From  cte4
       ) A
 Where charindex(@Delimiter2,RetVal)>1

)
/*
Max Length of String 1MM characters

Declare @String varchar(max) = 'Dear [[FirstName]] [[LastName]], ...'
Select * From [dbo].[udf-Str-Extract] (@String,'[[',']]')
*/

EDIT - Illustration of Raw Results

If you were to simply run

Select * from  [dbo].[udf-Str-Extract](@S,'>','<')

The Results would be:

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

The results you received are almost exactly what I am looking for. However, I do not understand how the function works. For instance why does the function know when exactly to extract the text, because there are other cases where there is text (or whitespace) between > and <, which is undesired information.
@javajunkee See EDIT
@javajunkee The beauty of using a TVF is that it can easly be used within a CROSS APPLY (i.e. no looping)
@JohnCappalletti I understand why blank spaces are not picked up now. If I were to want to implement this code into my sql server, I would need both the UDF and the TVF since the TVF makes a call to the UDF?
@javajunkee UDF is a generic term. There are two types of functions (both UDFs). 1 Scalar-Valued Function and 2) Table-Valued Function. You only need the UDF supplied (which is a TVF).
|
2

What about this? Note, I had to update the HTML blob because it had an unencoded &, so I just manually changed it to &amp;. But as long as that's set up, this should work:

declare @x xml = cast('<table  class="inline_sd_table"> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Accessories Optional</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Accommodates Ceiling Height</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>ADA Compliance</b></td>      <td class="inline_sd_even_cell">See Additional Information above</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Blower Air Mover Type</b></td>      <td class="inline_sd_odd_cell">Fan</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Blower Included</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Boost Mode</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>CFM (Airflow)</b></td>      <td class="inline_sd_even_cell">190</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Color/Finish</b></td>      <td class="inline_sd_odd_cell">Almond</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Control Feature Filter Reminder</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Damper Included</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Delay Shut Off</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Digital Clock</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Dishwasher Safe Filters</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Duct Connector Size</b></td>      <td class="inline_sd_odd_cell">3-1/4" x 10", 7" Round Non-Ducted</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>ENERGY STAR&amp;reg; qualified</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Filter Included</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Filter Type</b></td>      <td class="inline_sd_even_cell">Aluminum</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Flue Accommodates ceilings up to xx ft</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Heat Sentry - Temperature Sensor</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>HVI Certified</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Light Bulb Quantity</b></td>      <td class="inline_sd_even_cell">1</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Light Bulbs Included</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Lighting Function</b></td>      <td class="inline_sd_even_cell">One-level</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Lighting Type</b></td>      <td class="inline_sd_odd_cell">Incandescent</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Max Sones/100 CFM</b></td>      <td class="inline_sd_even_cell">4.1</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Minimum Height Above Cook Top</b></td>      <td class="inline_sd_odd_cell">18"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Mounting Type</b></td>      <td class="inline_sd_even_cell">Under Cabinet</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Nightlight</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Non-Stick Coated</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Product Depth (inches)</b></td>      <td class="inline_sd_odd_cell">17-1/2"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Product Height (inches)</b></td>      <td class="inline_sd_even_cell">6"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Product Width (inches)</b></td>      <td class="inline_sd_odd_cell">42"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Rated Amps</b></td>      <td class="inline_sd_even_cell">2.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Remote Control</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Horizontal Rectangular High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Horizontal Rectangular Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Vertical Rectangular High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Vertical Rectangular Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Vertical Round High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Vertical Round Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Switch Control Settings</b></td>      <td class="inline_sd_even_cell">2</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Switch Style</b></td>      <td class="inline_sd_odd_cell">Rocker</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Timer</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>UL Listed</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Voltage</b></td>      <td class="inline_sd_even_cell">120</td> </tr> </table> ' as xml)


select 
    Name = t.c.value('(td/b)[1]', 'varchar(250)'),
    Value = t.c.value('(td)[2]', 'varchar(250)')
from @x.nodes('/table/tr') as t(c)

2 Comments

A good solution, and my first thought as well. I just did want to chase the XML characters +1
Took the liberty of adding a dbFiddle so OP can see a working example. dbfiddle.uk/…
1

Example of Using a TVF within a CROSS APPLY

Insert Into Table2
Select ID = A.Entity_ID
      ,B.*
 From Table1 A
 Cross Apply (
                Select Top 10000
                       SpecName  = max(case when RetCol=1 then RetVal end)
                      ,Spec      = max(case when RetCol=0 then RetVal end)
                 From (
                        Select RetGrp=(Row_Number() over (Order By RetSeq)-1)/2
                              ,RetCol=Row_Number() over (Order By RetSeq) % 2 
                              ,RetPos
                              ,RetVal
                         From  [dbo].[udf-Str-Extract](A.value,'>','<')
                         Where RetVal>''
                      ) B1
                 Group By RetGrp
                 Order By RetGrp
             ) B


Select * from Table2

20 Comments

This is precisely what I am looking for. How would I modify this so that instead of inserting String values on the second line, the values will be pulled from table1 so that I can be looped for all rows of table1?
@JognCappelletti I apologize for the nuisance, but where is [dbo].[udf-Str-Extract] coming from? Is it an object you created?
@javajunkee in my first answer I included the function look under "The UDF if Interested"
@javajunkee FYI, feel free to rename it . Many do like my naming conventions.
I ran this without the insert, and I was getting errors around row 13, which I figured out was the A.value clause. So I replaced it with a variable that simply holds the html snippet and figured I would write a loop to work through all the rows of table1, but then sql began spitting out the first row of the html table thousands of times. Please advise?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.