Converting comma delimited string to multiple columns in sql server

后端 未结 4 946
借酒劲吻你
借酒劲吻你 2020-12-22 03:49

I want to extract specific strings separated by a comma and parse across the specific columns in SQL server 2008. The table structure in SQL server is as follows:

         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-22 04:18

    Correct my line of thinking here...

    Instead of trying to "Comma" delimit a field it would be more prudent to have a second table where you can put your name/value pairs in.

    Modify SAMP to have the following field:
    ID - integer - Primary Key Auto increment
    
    Create a table NVP
    ID - integer - Primary Key Auto increment
    SAMPID - integer Foreign key SAMP.ID
    Name - varchar(255) - or any realistic size
    Value - varchar(255) - or any realistic size
    
    This will allow for the following:
    1.  Unlimited fields
    2.  Faster Data Access
    3.  Since you are not trying to shove several values into 1 field, you now don't have to worry about running out of space.
    4.  Less code to worry about trying to split/join data
    5.  No longer restricted where you can't store a "," as one of your names or values.
    

    SQL tables should always be relational to take advantage of the power SQL has to offer.

提交回复
热议问题