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. Fetching record that have a particular value in a comma separated value in column [modified]

Fetching record that have a particular value in a comma separated value in column [modified]

Scheduled Pinned Locked Moved Database
databasehelplounge
5 Posts 3 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.
  • S Offline
    S Offline
    Sumit Prakash Sharma
    wrote on last edited by
    #1

    Good evening I have a table in database like following one --------------------------------------------------------------------- rollNo   |   Name   |   Subjects --------------------------------------------------------------------- 101         |   abc      |   english, maths, social science, science, hindi 102         |   def      |   maths, social science, science 103         |   ghi      |   english, social science, hindi 101         |   jkl      |   english, maths, social science, science, hindi ---------------------------------------------------------------------- now i want to select only those records which have "science" in the "Subjects" column so i use Select * from tblStudent Where Subjects LIKE "%Science%" but it also give the records that have "Social Science" ie record no 3 please help me thanks in advance

    modified on Saturday, May 2, 2009 8:40 AM

    M 1 Reply Last reply
    0
    • S Sumit Prakash Sharma

      Good evening I have a table in database like following one --------------------------------------------------------------------- rollNo   |   Name   |   Subjects --------------------------------------------------------------------- 101         |   abc      |   english, maths, social science, science, hindi 102         |   def      |   maths, social science, science 103         |   ghi      |   english, social science, hindi 101         |   jkl      |   english, maths, social science, science, hindi ---------------------------------------------------------------------- now i want to select only those records which have "science" in the "Subjects" column so i use Select * from tblStudent Where Subjects LIKE "%Science%" but it also give the records that have "Social Science" ie record no 3 please help me thanks in advance

      modified on Saturday, May 2, 2009 8:40 AM

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

      This is where you pay the price for BAD data design, If you were good you would create another table called Subject and link it to your student table in the correct foreign key data structure. Now you're screwed, you have to invent a work around for your crappy design. Options Are: Redesign you data structure correctly - this is the recommended solution. Play around with the text structure (', Science') will get most except where it is the first subject. Try and NOT like "Social Science" Create a split function based on the "," and filter on the required value (most sql devs have a split function floating around in their tools set)

      Never underestimate the power of human stupidity RAH

      I 1 Reply Last reply
      0
      • M Mycroft Holmes

        This is where you pay the price for BAD data design, If you were good you would create another table called Subject and link it to your student table in the correct foreign key data structure. Now you're screwed, you have to invent a work around for your crappy design. Options Are: Redesign you data structure correctly - this is the recommended solution. Play around with the text structure (', Science') will get most except where it is the first subject. Try and NOT like "Social Science" Create a split function based on the "," and filter on the required value (most sql devs have a split function floating around in their tools set)

        Never underestimate the power of human stupidity RAH

        I Offline
        I Offline
        i i i
        wrote on last edited by
        #3

        No doubt DB structure has million dollar worth butt still you can use you can use In "Select Col1 ,Col2 from tbl Where Col2 not In (SELECT Col2 FROM tbl ) " and can also use the XML as now is supported in SQL SERVER

        M 1 Reply Last reply
        0
        • I i i i

          No doubt DB structure has million dollar worth butt still you can use you can use In "Select Col1 ,Col2 from tbl Where Col2 not In (SELECT Col2 FROM tbl ) " and can also use the XML as now is supported in SQL SERVER

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

          Using In won't work, you still need to split col2 into it's parts. Doing it with XML or a table var is the same thing, you still need a function to do the splitting of the course column.

          Never underestimate the power of human stupidity RAH

          I 1 Reply Last reply
          0
          • M Mycroft Holmes

            Using In won't work, you still need to split col2 into it's parts. Doing it with XML or a table var is the same thing, you still need a function to do the splitting of the course column.

            Never underestimate the power of human stupidity RAH

            I Offline
            I Offline
            i i i
            wrote on last edited by
            #5

            Yup I agree with you that Split function has to be used but if that data exists in any other table then we can use in

            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