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. SQL database table , Data separated by ","

SQL database table , Data separated by ","

Scheduled Pinned Locked Moved Database
databasequestion
2 Posts 2 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.
  • P Offline
    P Offline
    phokojoe
    wrote on last edited by
    #1

    I thought it will work for a series of data in one column but it did not. Actually, if i have 1,2,3 in one column and would like to put 1 in column A, 2 in column B and 3 in coulumn C of another table. I had something like: insert test (a) select substring(aa,1,patindex('%,%',aa)-1)as a from table1 insert test (b) select substring(aa,3,patindex('%,%',aa)-1)as b from table1 insert test (c) select substring(aa,5,patindex('%,%',aa)-1)as c from table1 but what happens is that it does not put data in 1 row. instead it gives me 3 rows like this: 1 NULL NULL null 2 NULL NULL NULL 3 How do I put this in one row phokojoe

    M 1 Reply Last reply
    0
    • P phokojoe

      I thought it will work for a series of data in one column but it did not. Actually, if i have 1,2,3 in one column and would like to put 1 in column A, 2 in column B and 3 in coulumn C of another table. I had something like: insert test (a) select substring(aa,1,patindex('%,%',aa)-1)as a from table1 insert test (b) select substring(aa,3,patindex('%,%',aa)-1)as b from table1 insert test (c) select substring(aa,5,patindex('%,%',aa)-1)as c from table1 but what happens is that it does not put data in 1 row. instead it gives me 3 rows like this: 1 NULL NULL null 2 NULL NULL NULL 3 How do I put this in one row phokojoe

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      Each INSERT statement creates the number of rows selected - 1 in the case of an INSERT...VALUES statement, the number of rows returned by the SELECT in the case of INSERT...SELECT. To put all the data in a single row, you simply need to select all the data you need for that row:

      INSERT test (a, b, c)
      SELECT
      SUBSTRING(aa, 1, PATINDEX('%,%',aa)-1 ),
      SUBSTRING(aa, 3, PATINDEX('%,%',aa)-1 ),
      SUBSTRING(aa, 5, PATINDEX('%,%',aa)-1 )
      FROM table1

      Stability. What an interesting concept. -- Chris Maunder

      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