Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. I didn't design it but I need to get data out of it.

I didn't design it but I need to get data out of it.

Scheduled Pinned Locked Moved Database
databasesql-serverdesignsysadminxml
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rnbergren
    wrote on last edited by
    #1

    So lets say I have a table (Ports) with an Identifier and then a compacted not XML field that is semi structured. select * from Ports --Yields this. ID Categories 1 Town:York, Street:Main, Phone:712-542-3423 The Categories field might have two fields in it. Town and Street or it might have 15. I need to get these out into separate fields as soo ID Town Street Phone 1 York Main 712...... ideas on SQL SQL server 2016 is what we are running. Ideas?

    To err is human to really mess up you need a computer

    Richard DeemingR Z M 3 Replies Last reply
    0
    • R rnbergren

      So lets say I have a table (Ports) with an Identifier and then a compacted not XML field that is semi structured. select * from Ports --Yields this. ID Categories 1 Town:York, Street:Main, Phone:712-542-3423 The Categories field might have two fields in it. Town and Street or it might have 15. I need to get these out into separate fields as soo ID Town Street Phone 1 York Main 712...... ideas on SQL SQL server 2016 is what we are running. Ideas?

      To err is human to really mess up you need a computer

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Not particularly simple, since different rows could have different "attributes" within the categories column. Something like this should work:

      DROP TABLE IF EXISTS #T;

      CREATE TABLE #T
      (
      ID int,
      PropertyName varchar(50),
      PropertyValue varchar(50)
      );

      INSERT INTO #T
      (
      ID,
      PropertyName,
      PropertyValue
      )
      SELECT
      T.ID,
      LTRIM(LEFT(V.value, CHARINDEX(':', V.value) - 1)) As PropertyName,
      LTRIM(SUBSTRING(V.value, CHARINDEX(':', V.value) + 1, LEN(V.Value))) As PropertyValue
      FROM
      YourTable As T
      CROSS APPLY string_split(T.Categories, ',') As V
      ;

      DECLARE @columns nvarchar(max) = STUFF
      (
      (
      SELECT DISTINCT ',' + QUOTENAME(PropertyName)
      FROM #T
      FOR XML PATH(''), TYPE
      ).value('.', 'nvarchar(max)')
      , 1, 1, ''
      );

      DECLARE @query nvarchar(max) = N'SELECT ID, ' + @columns
      + N' FROM (SELECT ID, PropertyName, PropertyValue FROM #T) As T'
      + N' PIVOT (Max(PropertyValue) FOR PropertyName In (' + @columns + N')) As P';

      EXECUTE (@query);

      DROP TABLE #T;

      The @query will look something like:

      SELECT ID, [Phone],[Street],[Town]
      FROM (SELECT ID, PropertyName, PropertyValue FROM #T) As T
      PIVOT (Max(PropertyValue) FOR PropertyName In ([Phone],[Street],[Town])) As P

      STRING_SPLIT (Transact-SQL) - SQL Server | Microsoft Docs[^] Using PIVOT and UNPIVOT - SQL Server | Microsoft Docs[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      R 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Not particularly simple, since different rows could have different "attributes" within the categories column. Something like this should work:

        DROP TABLE IF EXISTS #T;

        CREATE TABLE #T
        (
        ID int,
        PropertyName varchar(50),
        PropertyValue varchar(50)
        );

        INSERT INTO #T
        (
        ID,
        PropertyName,
        PropertyValue
        )
        SELECT
        T.ID,
        LTRIM(LEFT(V.value, CHARINDEX(':', V.value) - 1)) As PropertyName,
        LTRIM(SUBSTRING(V.value, CHARINDEX(':', V.value) + 1, LEN(V.Value))) As PropertyValue
        FROM
        YourTable As T
        CROSS APPLY string_split(T.Categories, ',') As V
        ;

        DECLARE @columns nvarchar(max) = STUFF
        (
        (
        SELECT DISTINCT ',' + QUOTENAME(PropertyName)
        FROM #T
        FOR XML PATH(''), TYPE
        ).value('.', 'nvarchar(max)')
        , 1, 1, ''
        );

        DECLARE @query nvarchar(max) = N'SELECT ID, ' + @columns
        + N' FROM (SELECT ID, PropertyName, PropertyValue FROM #T) As T'
        + N' PIVOT (Max(PropertyValue) FOR PropertyName In (' + @columns + N')) As P';

        EXECUTE (@query);

        DROP TABLE #T;

        The @query will look something like:

        SELECT ID, [Phone],[Street],[Town]
        FROM (SELECT ID, PropertyName, PropertyValue FROM #T) As T
        PIVOT (Max(PropertyValue) FOR PropertyName In ([Phone],[Street],[Town])) As P

        STRING_SPLIT (Transact-SQL) - SQL Server | Microsoft Docs[^] Using PIVOT and UNPIVOT - SQL Server | Microsoft Docs[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        R Offline
        R Offline
        rnbergren
        wrote on last edited by
        #3

        Thank you. I was hoping there was something easier than this. I have like 30 fields to actually deal with. Oh well. Brute force it is.

        To err is human to really mess up you need a computer

        1 Reply Last reply
        0
        • R rnbergren

          So lets say I have a table (Ports) with an Identifier and then a compacted not XML field that is semi structured. select * from Ports --Yields this. ID Categories 1 Town:York, Street:Main, Phone:712-542-3423 The Categories field might have two fields in it. Town and Street or it might have 15. I need to get these out into separate fields as soo ID Town Street Phone 1 York Main 712...... ideas on SQL SQL server 2016 is what we are running. Ideas?

          To err is human to really mess up you need a computer

          Z Offline
          Z Offline
          ZurdoDev
          wrote on last edited by
          #4

          If you have the option of pulling the data into C# it would be a lot easier (likely) to change the data in C# and put it back into SQl.

          Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

          1 Reply Last reply
          0
          • R rnbergren

            So lets say I have a table (Ports) with an Identifier and then a compacted not XML field that is semi structured. select * from Ports --Yields this. ID Categories 1 Town:York, Street:Main, Phone:712-542-3423 The Categories field might have two fields in it. Town and Street or it might have 15. I need to get these out into separate fields as soo ID Town Street Phone 1 York Main 712...... ideas on SQL SQL server 2016 is what we are running. Ideas?

            To err is human to really mess up you need a computer

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Depending on how dynamic the table is (I would think Ports would be rather static) I would brute force the categories into separate table(s) and either use Richards pivot or a monstrous view with 30 left outer joins. Then when you get a new Ports just run the brute again to parse the Categories.

            Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups