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. Replace function

Replace function

Scheduled Pinned Locked Moved Database
databasequestionsql-serversysadminhelp
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.
  • J Offline
    J Offline
    Jay Royall
    wrote on last edited by
    #1

    Hi, I am using SQL Server 2005 and have a question regarding the Replace function. I have a 'select' query looking something like this: SELECT tblContacts.* FROM tblContacts WHERE tblContacts.[Surname] + tblContacts.[Firstname] LIKE '%Smith (Hon. Member)John%' ('Smith (Hon. Member)John' will come from the user's input) Now, the end users do not want to have to type in 'Smith (Hon. Member)John', they just want to type SmithHonMemberJohn, that is, the full name and membership type without any spaces, brackets or full stops (periods). I have looked at the replace function to replace characters but is it possible to replace more than 1 type of character? That is, the replcae function will replace all instances of a certain character, for instance, commas. But is it possible to specify more than 1 chacter type, for instance commas, brackets and spaces? Hope I have explained this OK. Thanks in advance for any help.

    R 1 Reply Last reply
    0
    • J Jay Royall

      Hi, I am using SQL Server 2005 and have a question regarding the Replace function. I have a 'select' query looking something like this: SELECT tblContacts.* FROM tblContacts WHERE tblContacts.[Surname] + tblContacts.[Firstname] LIKE '%Smith (Hon. Member)John%' ('Smith (Hon. Member)John' will come from the user's input) Now, the end users do not want to have to type in 'Smith (Hon. Member)John', they just want to type SmithHonMemberJohn, that is, the full name and membership type without any spaces, brackets or full stops (periods). I have looked at the replace function to replace characters but is it possible to replace more than 1 type of character? That is, the replcae function will replace all instances of a certain character, for instance, commas. But is it possible to specify more than 1 chacter type, for instance commas, brackets and spaces? Hope I have explained this OK. Thanks in advance for any help.

      R Offline
      R Offline
      RGTuffin
      wrote on last edited by
      #2

      Not ideal, but you can nest the replace statments for a quick fix: replace(replace(replace(replace('Smith (Hon. Member)John','(',''),')',''),'.',''),' ','') produces: SmithHonMemberJohn otherwise you could always write a function to remove any non alphanumeric characters. I don't know of any pattern matching within the replace statement though.

      J 1 Reply Last reply
      0
      • R RGTuffin

        Not ideal, but you can nest the replace statments for a quick fix: replace(replace(replace(replace('Smith (Hon. Member)John','(',''),')',''),'.',''),' ','') produces: SmithHonMemberJohn otherwise you could always write a function to remove any non alphanumeric characters. I don't know of any pattern matching within the replace statement though.

        J Offline
        J Offline
        Jay Royall
        wrote on last edited by
        #3

        I like the idea of a SQL Server function to remove any non alpha characters but, my query is being dynamically built within the Windows application rather than being a stored procedure. Am I able to call a SQL Server function in this way? If not then the nested replace will have to suffice!

        B 1 Reply Last reply
        0
        • J Jay Royall

          I like the idea of a SQL Server function to remove any non alpha characters but, my query is being dynamically built within the Windows application rather than being a stored procedure. Am I able to call a SQL Server function in this way? If not then the nested replace will have to suffice!

          B Offline
          B Offline
          Ben Fair
          wrote on last edited by
          #4

          Yes, you can call it that way. If you make a SQL Server scalar function for this purpose, let's say it's named 'RemoveNonAlphaChars'. Your SELECT would use it like so:

          SELECT tblContacts.*
          FROM tblContacts
          WHERE dbo.RemoveNonAlphaChars(tblContacts.[Surname] + tblContacts.[Firstname]) LIKE '%SmithHonMemberJohn%'

          Keep It Simple Stupid! (KISS)

          J 1 Reply Last reply
          0
          • B Ben Fair

            Yes, you can call it that way. If you make a SQL Server scalar function for this purpose, let's say it's named 'RemoveNonAlphaChars'. Your SELECT would use it like so:

            SELECT tblContacts.*
            FROM tblContacts
            WHERE dbo.RemoveNonAlphaChars(tblContacts.[Surname] + tblContacts.[Firstname]) LIKE '%SmithHonMemberJohn%'

            Keep It Simple Stupid! (KISS)

            J Offline
            J Offline
            Jay Royall
            wrote on last edited by
            #5

            Great :) Thank you both for your help.

            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