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. Problem in String Compare

Problem in String Compare

Scheduled Pinned Locked Moved Database
databasehelp
9 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.
  • J Offline
    J Offline
    Jintal Patel
    wrote on last edited by
    #1

    hi my table is like ProductID CagtegoryID 1 1,2, 2 1, 3 4, 4 2,3, and for SELECT Query if i give the categoryid=1,2 then i want the output like ProductID CagtegoryID 1 1,2, 2 1, 4 2,3,

    Jintal Patel

    C F 2 Replies Last reply
    0
    • J Jintal Patel

      hi my table is like ProductID CagtegoryID 1 1,2, 2 1, 3 4, 4 2,3, and for SELECT Query if i give the categoryid=1,2 then i want the output like ProductID CagtegoryID 1 1,2, 2 1, 4 2,3,

      Jintal Patel

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      I think you need to learn about normalisation. You are storing multiple things in the categoryID column. By the looks of it you have a many to many join. To create a many-to-many join you need an intermediate table. So you have a Product table and a Category table and in between you have a ProductCategory table. The intermediate ProductCategory table contains just the primary keys of the source tables. So, Product table: ProductID 1 2 3 4 Category table: CategoryID 1 2 3 4 ProductCategory table: ProductID CategoryID 1 1 1 2 2 1 3 4 4 2 4 3 To get the results you want:

      SELECT ProductID, CategoryID FROM ProductCategory WHERE CategoryID IN (1,2)

      If you need other columns from the product and category tables you'll need a join too. e.g.

      SELECT p.ProductID, c.CategoryID, p.ProductName, c.CategoryName
      FROM Product AS p
      INNER JOIN ProductCategory AS pc ON p.ProductID = pc.ProductID
      INNER JOIN Category AS c on c.CategoryID = pc.CategoryID
      WHERE c.CategoryID IN (1,2)


      Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

      J 1 Reply Last reply
      0
      • J Jintal Patel

        hi my table is like ProductID CagtegoryID 1 1,2, 2 1, 3 4, 4 2,3, and for SELECT Query if i give the categoryid=1,2 then i want the output like ProductID CagtegoryID 1 1,2, 2 1, 4 2,3,

        Jintal Patel

        F Offline
        F Offline
        Fang Ming
        wrote on last edited by
        #3

        creat a temporary table declare @temp table ( CagtegoryID varchar(100) ) insert the CagtegoryIDs into this table one by one it will be like CagtegoryID %,1,% %,2,% then try this statement select * from your table [your table ] A where exists (select 1 from @temp B where ','+A.CagtegoryID like B.CagtegoryID

        C 1 Reply Last reply
        0
        • C Colin Angus Mackay

          I think you need to learn about normalisation. You are storing multiple things in the categoryID column. By the looks of it you have a many to many join. To create a many-to-many join you need an intermediate table. So you have a Product table and a Category table and in between you have a ProductCategory table. The intermediate ProductCategory table contains just the primary keys of the source tables. So, Product table: ProductID 1 2 3 4 Category table: CategoryID 1 2 3 4 ProductCategory table: ProductID CategoryID 1 1 1 2 2 1 3 4 4 2 4 3 To get the results you want:

          SELECT ProductID, CategoryID FROM ProductCategory WHERE CategoryID IN (1,2)

          If you need other columns from the product and category tables you'll need a join too. e.g.

          SELECT p.ProductID, c.CategoryID, p.ProductName, c.CategoryName
          FROM Product AS p
          INNER JOIN ProductCategory AS pc ON p.ProductID = pc.ProductID
          INNER JOIN Category AS c on c.CategoryID = pc.CategoryID
          WHERE c.CategoryID IN (1,2)


          Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

          J Offline
          J Offline
          Jintal Patel
          wrote on last edited by
          #4

          thanks for Solution i getting my value by Like this SELECT * FROM [DB_Ecommerce].[dbo].[tb_Ecomm_Product] Where Charindex('8',CategoryId)<>0 or Charindex('2',CategoryId)<>0 ;

          Jintal Patel

          C 1 Reply Last reply
          0
          • J Jintal Patel

            thanks for Solution i getting my value by Like this SELECT * FROM [DB_Ecommerce].[dbo].[tb_Ecomm_Product] Where Charindex('8',CategoryId)<>0 or Charindex('2',CategoryId)<>0 ;

            Jintal Patel

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Jintal Patel wrote:

            SELECT * FROM [DB_Ecommerce].[dbo].[tb_Ecomm_Product] Where Charindex('8',CategoryId)<>0 or Charindex('2',CategoryId)<>0 ;

            That is hardly a good solution in the face of properly modelling your data.


            Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

            1 Reply Last reply
            0
            • F Fang Ming

              creat a temporary table declare @temp table ( CagtegoryID varchar(100) ) insert the CagtegoryIDs into this table one by one it will be like CagtegoryID %,1,% %,2,% then try this statement select * from your table [your table ] A where exists (select 1 from @temp B where ','+A.CagtegoryID like B.CagtegoryID

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              That has to be the craziest thing I've ever seen and it won't even return the correct answer. It does not handle cases where the category is the first or last on a list. There fore it there is only one or two categories it won't return anything. If there are three or more categories it will only return those in the middle.


              Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

              F 1 Reply Last reply
              0
              • C Colin Angus Mackay

                That has to be the craziest thing I've ever seen and it won't even return the correct answer. It does not handle cases where the category is the first or last on a list. There fore it there is only one or two categories it won't return anything. If there are three or more categories it will only return those in the middle.


                Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                F Offline
                F Offline
                Fang Ming
                wrote on last edited by
                #7

                take it carefuly Patel's table: ProductID CagtegoryID 1 1,2, 2 1, 3 4, 4 2,3, search condition : categoryid=1,2 i transform it to : CagtegoryID %,1,% %,2,% my statement was "select * from [your table ] A where exists (select 1 from @temp B where ','+A.CagtegoryID like B.CagtegoryID )" the "','+" can handle the leading first Cagtegory. and the data in Patel's table "1,2**,",the last ",**" can handle the last one

                C 1 Reply Last reply
                0
                • F Fang Ming

                  take it carefuly Patel's table: ProductID CagtegoryID 1 1,2, 2 1, 3 4, 4 2,3, search condition : categoryid=1,2 i transform it to : CagtegoryID %,1,% %,2,% my statement was "select * from [your table ] A where exists (select 1 from @temp B where ','+A.CagtegoryID like B.CagtegoryID )" the "','+" can handle the leading first Cagtegory. and the data in Patel's table "1,2**,",the last ",**" can handle the last one

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  Okay - I retract my statement that it won't return the correct data. However it is still a poor solution. The data should have been properly normalised for this query to work efficiently. This solution can't be optimised by the query optimiser and will always run slowly. That might be fine for a handful of rows but it will take a significant amount of time if the tables grow.


                  Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                  F 1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    Okay - I retract my statement that it won't return the correct data. However it is still a poor solution. The data should have been properly normalised for this query to work efficiently. This solution can't be optimised by the query optimiser and will always run slowly. That might be fine for a handful of rows but it will take a significant amount of time if the tables grow.


                    Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                    F Offline
                    F Offline
                    Fang Ming
                    wrote on last edited by
                    #9

                    " it will take a significant amount of time if the tables grow" yeah ,that's the truth. i just gived a way to resolve Patel's problem. in general , yours must be the first and best choice.

                    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