Truncating and copying to another column

  • Hi Guys

    I have a column called ColumnA with the following data in it

    2300>MC13

    What I need to achieve is copy the last five characters into ColumnB...ie

    >MC13

    Can someone assist with the correct statement to do this?

    Thanks in advance

    A

  • Hi,

    You can try this

    ColumnB = Right(colname, LEN(colname) - 4)

    Regards.

  • Do you always want the 5 characters on the right? Or is it always the everything to the right of and including the gt ">" symbol?

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Hi

    Thanks you for the reply

    I just need the > symbol and the 4 characters after it

    All the data is consistent in character length

    A

  • Thank you Kish...your suggestion worked!:-)

  • Here is a means to get that data that allows for some variances - just in case.

    DECLARE @table TABLE (ColumnA VARCHAR(20), ColumnB varchar(20))

    INSERT INTO @table (ColumnA) VALUES ('2300>MC13')

    SELECT SUBSTRING(ColumnA,CHARINDEX('>',ColumnA),LEN(ColumnA)-CHARINDEX('>',ColumnA)+1)

    FROM @table

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Oh and here is the rest of the setup to update ColumnB

    DECLARE @table TABLE (ColumnA VARCHAR(20), ColumnB varchar(20))

    INSERT INTO @table (ColumnA) VALUES ('2300>MC13');

    SELECT SUBSTRING(ColumnA,CHARINDEX('>',ColumnA),LEN(ColumnA)-CHARINDEX('>',ColumnA)+1)

    FROM @table;

    UPDATE t

    SET ColumnB = SUBSTRING(ColumnA,CHARINDEX('>',ColumnA),LEN(ColumnA)-CHARINDEX('>',ColumnA)+1)

    FROM @table t;

    SELECT *

    FROM @table;

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 7 posts - 1 through 6 (of 6 total)

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