Search code examples
mysqlsqlstringpostgresqlpgadmin-4

Removing the first 5 characters of each entry of a column STORED PROCEDURE


I have the table reservation and in that table there is a column called campground. Every entry of the campground column is very similar to the other ones e.g.:

ALBE/Alberta Beach Family RV Park

Can someone tell me how to remove the first 5 characters of every entry in this column using a stored procedure which removes 5 characters from every entry in this column?

The code I've tried so far is the following:

UPDATE reservation
SET RIGHT(campground, LEN(campground) - 5)

Can someone help me creating a stored procedure with the functionality mentioned above?

Thanks!!


Solution

  • Do you really want to delete the first 5 characters of the string, or do you want to delete everything up to the first forward slash?

    The other answers here address your literal question. This answers the question you might really be asking (PostgreSQL dialect):

    select substr(campground,position('/' in campground)+1)
    from (values
         ('ABC/American Bike Camp')
        ,('ALBE/Alberta Beach Family RV Park')) t(campground)
    ;
    
                substr
    ------------------------------
     American Bike Camp
     Alberta Beach Family RV Park
    (2 rows)
    

    Using that expression in an update statement:

    update reservation
    set campground = substr(campground,position('/' in campground)+1)
    where campground like '%/%'
    ;
    

    (where clause included to ensure it doesn't update rows that have already been updated)