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. Search + Sql server 2005

Search + Sql server 2005

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
7 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.
  • A Offline
    A Offline
    abcurl
    wrote on last edited by
    #1

    hi, I have to search 90% characters in a string. Suppose the string is "abcdef" and it;s character length is 6 and it's 90 percent character length will be 5 Now i have to search it's any 5 character in the following string string1 stringabcdef astrbincgdef Please suggest something ... Is there any way to do so with loop Thanks

    L R 2 Replies Last reply
    0
    • A abcurl

      hi, I have to search 90% characters in a string. Suppose the string is "abcdef" and it;s character length is 6 and it's 90 percent character length will be 5 Now i have to search it's any 5 character in the following string string1 stringabcdef astrbincgdef Please suggest something ... Is there any way to do so with loop Thanks

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

      For that you need to write either Stored Procedure or Function. Just calculate length of the search string, and also need to calculate how many characters for 90% it. Then you need to search character by character.. Sounds Non-trivial.

      Jinal Desai - LIVE Experience is mother of sage....

      1 Reply Last reply
      0
      • A abcurl

        hi, I have to search 90% characters in a string. Suppose the string is "abcdef" and it;s character length is 6 and it's 90 percent character length will be 5 Now i have to search it's any 5 character in the following string string1 stringabcdef astrbincgdef Please suggest something ... Is there any way to do so with loop Thanks

        R Offline
        R Offline
        RyanEK
        wrote on last edited by
        #3

        How about the following...

        -- return 90% of a string
        declare @s varchar(6)
        set @s = '123456'
        select substring(@s, 1, cast(floor(0.9 * len(@s)) as int))

        -- usage
        select *
        from [table]
        where charindex(substring(@s, 1, cast(floor(0.9 * len(@s)) as int)), [field]) > 0

        Ryan

        A 1 Reply Last reply
        0
        • R RyanEK

          How about the following...

          -- return 90% of a string
          declare @s varchar(6)
          set @s = '123456'
          select substring(@s, 1, cast(floor(0.9 * len(@s)) as int))

          -- usage
          select *
          from [table]
          where charindex(substring(@s, 1, cast(floor(0.9 * len(@s)) as int)), [field]) > 0

          Ryan

          A Offline
          A Offline
          abcurl
          wrote on last edited by
          #4

          hi,

          RyanEK wrote:

          -- return 90% of a string declare @s varchar(6) set @s = '123456' select substring(@s, 1, cast(floor(0.9 * len(@s)) as int)) -- usage select * from [table] where charindex(substring(@s, 1, cast(floor(0.9 * len(@s)) as int)), [field]) > 0

          Thanks for the kind attention. It will work fine if I have to start from position 1 only. But the string to search that I mentioned in the post. was abcdef and it's 90% can be abcde or bcdef similarly i can have larger strings as well and it's propability will be have more string to search. Is it possible to resolve it without any iteration ? Thanks

          R 1 Reply Last reply
          0
          • A abcurl

            hi,

            RyanEK wrote:

            -- return 90% of a string declare @s varchar(6) set @s = '123456' select substring(@s, 1, cast(floor(0.9 * len(@s)) as int)) -- usage select * from [table] where charindex(substring(@s, 1, cast(floor(0.9 * len(@s)) as int)), [field]) > 0

            Thanks for the kind attention. It will work fine if I have to start from position 1 only. But the string to search that I mentioned in the post. was abcdef and it's 90% can be abcde or bcdef similarly i can have larger strings as well and it's propability will be have more string to search. Is it possible to resolve it without any iteration ? Thanks

            R Offline
            R Offline
            RyanEK
            wrote on last edited by
            #5

            Ahh I see, you want to match a percentage of a string regardless of its position. I suggest you create a temp table holding all possible values and join to that. eg. 25% of 'ABCDEFGHIJ' would yield a table with the rows: 'AB' 'BC' 'CD' etc... Ryan

            A 1 Reply Last reply
            0
            • R RyanEK

              Ahh I see, you want to match a percentage of a string regardless of its position. I suggest you create a temp table holding all possible values and join to that. eg. 25% of 'ABCDEFGHIJ' would yield a table with the rows: 'AB' 'BC' 'CD' etc... Ryan

              A Offline
              A Offline
              abcurl
              wrote on last edited by
              #6

              hi Ryan, Thanks for the reply. Actually i wanted to search 90% in a non consecutive way. Example the string to search is abcdef it's original length is 6 it's 90% length is 5. and i want to search any five characters from this 90% in a non consecutive way from the following records. "Search Matched" - Non matched because only 4 characters matched "Sabaschede" - matched and found five characters in it. can i do it without using any loop ? And I have millions of records, also have to consider it's performance

              R 1 Reply Last reply
              0
              • A abcurl

                hi Ryan, Thanks for the reply. Actually i wanted to search 90% in a non consecutive way. Example the string to search is abcdef it's original length is 6 it's 90% length is 5. and i want to search any five characters from this 90% in a non consecutive way from the following records. "Search Matched" - Non matched because only 4 characters matched "Sabaschede" - matched and found five characters in it. can i do it without using any loop ? And I have millions of records, also have to consider it's performance

                R Offline
                R Offline
                RyanEK
                wrote on last edited by
                #7

                Interesting problem. This may be a solution? Say you have a string "abcdef". -- place each character in a temp table declare @temp table (ch char) insert into @temp values ('a') insert into @temp values ('b') insert into @temp values ('c') insert into @temp values ('d') insert into @temp values ('e') insert into @temp values ('f') -- some test data declare @temp2 table (string varchar(10)) insert into @temp2 values ('Sabaschede') insert into @temp2 values ('aaaaaaaaaa') insert into @temp2 values ('a1b2c3d4e5') -- use a cross join to determine how many times each character appears in the string field -- you just need to determine that 90% of 'abcdef' is 5 characters select string from ( select ch, string, case when charindex(ch,string) > 0 then 1 else 0 end chrcount from @temp cross join @temp2 ) as t group by string having sum(chrcount) >= 5 Ryan

                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