Help query to select part of string

  • Hi Guys

    Hope everyone is well, i have a field called NAME with some data as shown below

    Name

    RECA-AA-3-CCC-1

    RECA-AB-31-abc-1

    RECA-C-3-T

    I am trying to write a select statement ( or a script) to remove the Characters 'RECA-' from the names ( shown above) so the output result should be as below

    Name

    AA-3-CCC-1

    AB-31-abc-1

    C-3-T

    can anyone help me with this?

    Thank you

  • Look at the REPLACE function, and replace with ''  (2 quotes, without space between them)

    • This reply was modified 2 years, 10 months ago by  homebrew01.
  • hi

    thank you for the reply

    Does the replace , replace the data in the original table?

     

    As i dont want that

  • This will replace the values (overwrite them):

    use tempdb;
    go

    CREATE TABLE Vals(Val VARCHAR(20));
    GO
    INSERT INTO Vals VALUES
    ('AAAB-AA-43-DDD-2')
    ,('RECA-AA-3-CCC-1')
    ,('RECA-AB-31-abc-1')
    ,('RECA-C-3-T');

    UPDATE Vals
    SET Val = REPLACE(Val,'RECA-','')
    WHERE Val LIKE 'RECA-%';

    If you just want to find them, use something like

    SELECT * FROM Vals WHERE Val LIKE 'RECA-%'
  • If you are running SELECT it is only for display.

    UPDATE will change the data in the table.

    https://docs.microsoft.com/en-us/sql/t-sql/functions/replace-transact-sql?view=sql-server-ver15

     

    • This reply was modified 2 years, 10 months ago by  homebrew01.
    • This reply was modified 2 years, 10 months ago by  homebrew01.
    • This reply was modified 2 years, 10 months ago by  homebrew01.
  • You can also use Substring() to return a part of the string only.

  • Thank you

    i dont want to replace or ovewrite them.

     

    I just want to pull the data without 'RECA-' at the start

  • A few choices:

    CREATE TABLE Vals(Val VARCHAR(20));
    GO
    INSERT INTO Vals VALUES
    ('AAAB-AA-43-DDD-2')
    ,('RECA-AA-3-CCC-1')
    ,('RECA-AB-31-abc-1')
    ,('RECA-C-3-T');

    GO

    SELECT
    REPLACE(Val,'RECA-','') AS Option1
    , CASE WHEN SUBSTRING(val, 1, 5) = 'RECA-'
    THEN SUBSTRING(val, 6, LEN(val))
    ELSE val
    END AS Option2
    FROM dbo.Vals AS v

    you can also use CHARINDEX to find spots in a string to drive substring if there are other patterns you need.

  • SELECT 
    STUFF(NAME, 1, CASE WHEN LEFT(NAME, 5) = 'RECA-' THEN 5 ELSE 0 END, '') AS NAME
    FROM dbo.table_name

    A potential problem with REPLACE is that it will affect the entire string, not just the leading chars.  For example, this:

    RECA-001-abc-qztb-RECA-LL

    would become:

    001-abc-qztb-LL

    While not at all likely to affect this code, if you only want leading chars changed, it's better to specify that explicitly.

     

    • This reply was modified 2 years, 10 months ago by  ScottPletcher.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • WHERE NAME NOT LIKE 'RECA-%'
  • pietlinden wrote:

    WHERE NAME NOT LIKE 'RECA-%'

    That will eliminate them from results. The request was to remove the characters 'RECA-' but keep the remaining characters.

    • This reply was modified 2 years, 10 months ago by  homebrew01.

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply