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 Query For String Searching

SQL Query For String Searching

Scheduled Pinned Locked Moved Database
databasealgorithmshelpquestion
12 Posts 5 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.
  • L Offline
    L Offline
    lhsunshine
    wrote on last edited by
    #1

    My case is Input is "abdknmgbm". BUT The record in database that i want to query out is "abkdnmgmb" The input and database record is quite similar. String length is same, only some arrangement of character are not same. In the above case, dk and kd ; bm and mb. How i write query for this string searching? Help and Thanks.

    C L P J 4 Replies Last reply
    0
    • L lhsunshine

      My case is Input is "abdknmgbm". BUT The record in database that i want to query out is "abkdnmgmb" The input and database record is quite similar. String length is same, only some arrangement of character are not same. In the above case, dk and kd ; bm and mb. How i write query for this string searching? Help and Thanks.

      C Offline
      C Offline
      Corporal Agarn
      wrote on last edited by
      #2

      If you are using SQL server try SOUNDEX. Do not know if it will work though.

      L 1 Reply Last reply
      0
      • L lhsunshine

        My case is Input is "abdknmgbm". BUT The record in database that i want to query out is "abkdnmgmb" The input and database record is quite similar. String length is same, only some arrangement of character are not same. In the above case, dk and kd ; bm and mb. How i write query for this string searching? Help and Thanks.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You could try the LIKE operator[^].

        One of these days I'm going to think of a really clever signature.

        1 Reply Last reply
        0
        • L lhsunshine

          My case is Input is "abdknmgbm". BUT The record in database that i want to query out is "abkdnmgmb" The input and database record is quite similar. String length is same, only some arrangement of character are not same. In the above case, dk and kd ; bm and mb. How i write query for this string searching? Help and Thanks.

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          :snork: A while back I wrote a Levenshtein Distance function (CLR) for SQL Server. :-D Such could be used and tested for "less than some threshold". The Levenshtein Distance between your samples is 4. http://en.wikipedia.org/wiki/Levenshtein_distance[^]

          L 1 Reply Last reply
          0
          • P PIEBALDconsult

            :snork: A while back I wrote a Levenshtein Distance function (CLR) for SQL Server. :-D Such could be used and tested for "less than some threshold". The Levenshtein Distance between your samples is 4. http://en.wikipedia.org/wiki/Levenshtein_distance[^]

            L Offline
            L Offline
            lhsunshine
            wrote on last edited by
            #5

            Actually the swap char will not always are char 3 and 4 and last 2 chars. For example it may also be like this : Input: " abcdkazbmopdkubmgt " The wanted result is : "abckdazmbopkdumbgt" Input and wanted result length are same. BUT the input string length is not always the same. Very hard to solve this. :( Tq.

            P 1 Reply Last reply
            0
            • L lhsunshine

              Actually the swap char will not always are char 3 and 4 and last 2 chars. For example it may also be like this : Input: " abcdkazbmopdkubmgt " The wanted result is : "abckdazmbopkdumbgt" Input and wanted result length are same. BUT the input string length is not always the same. Very hard to solve this. :( Tq.

              P Online
              P Online
              PIEBALDconsult
              wrote on last edited by
              #6

              So? Those have a Levenshtein Distance of 10 (8 if you Trim) -- maybe that's within your threshdold, I dunno.

              L 1 Reply Last reply
              0
              • L lhsunshine

                My case is Input is "abdknmgbm". BUT The record in database that i want to query out is "abkdnmgmb" The input and database record is quite similar. String length is same, only some arrangement of character are not same. In the above case, dk and kd ; bm and mb. How i write query for this string searching? Help and Thanks.

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                lhsunshine wrote:

                How i write query for this string searching

                Very inefficiently. If you have a lot of data then you should probably look for another way to implement the search.

                P 1 Reply Last reply
                0
                • P PIEBALDconsult

                  So? Those have a Levenshtein Distance of 10 (8 if you Trim) -- maybe that's within your threshdold, I dunno.

                  L Offline
                  L Offline
                  lhsunshine
                  wrote on last edited by
                  #8

                  I have read about Levenshtein Distance. New knowledge for me. Thx. But how can i apply into my coding? I am using Asp.net with C#. I have no idea about this.

                  P 1 Reply Last reply
                  0
                  • L lhsunshine

                    I have read about Levenshtein Distance. New knowledge for me. Thx. But how can i apply into my coding? I am using Asp.net with C#. I have no idea about this.

                    P Online
                    P Online
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    If you think it will do what you need you can write a function in C# or VB and add it to SQL Server (I assume you're using SQL Server). As to how to add it to SQL Server, I'd rather write a tip than post it here. Given that the function has the signature int Levenshtein ( string , string ) you would then be able to say: SELECT * FROM sometable WHERE dbo.Levenshtein ( somefield , @somevalue ) < @somethreshold The threshold should probably be based on the lengths of the strings, for instance half the length of the shorter string. To make things more efficient, you could write a function with the signature bool IsSimilar ( string , string , int ) that will return false as soon as the Levenshtein Distance between the strings exceeds the threshold (the third parameter). SELECT * FROM sometable WHERE dbo.IsSimilar ( somefield , @somevalue , @somethreshold ) = 1

                    1 Reply Last reply
                    0
                    • C Corporal Agarn

                      If you are using SQL server try SOUNDEX. Do not know if it will work though.

                      L Offline
                      L Offline
                      lhsunshine
                      wrote on last edited by
                      #10

                      Cannot work. Because they are not in english word.

                      1 Reply Last reply
                      0
                      • J jschell

                        lhsunshine wrote:

                        How i write query for this string searching

                        Very inefficiently. If you have a lot of data then you should probably look for another way to implement the search.

                        P Online
                        P Online
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        Inefficient is better than nothing or wrong. :-D

                        L 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Inefficient is better than nothing or wrong. :-D

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          "It works on my machine" :cool:

                          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