skip to Main Content

I am using a function REGEXP_REPLACE within SQL to generate a slug value from name column

UPDATE tag
       SET slug = LOWER(REGEXP_REPLACE(
                REGEXP_REPLACE('Team Building 2022', '^[^A-Za-z]+|[^A-Za-z]+$', '', 'g'),
                 '[^A-Za-z]+|(?<=[a-z])(?=[A-Z])', '-', 'g')
         );

But I get: team-building

Result I want is: team-building-2022

Is there a way to add numbers in this function? Thanks

2

Answers


  1. Chosen as BEST ANSWER

    Resolved it with:

    LOWER(REGEXP_REPLACE(
          REGEXP_REPLACE(name, '^[^A-Za-z0-9]+|[^A-Za-z0-9]+$', '', 'g'),
             '[^A-Za-z0-9]+|(?<=[a-z])(?=[A-Z])', '-', 'g')
    

  2. Well you did not actually specify what result you want. but from the actual question and the reply to @WiktorStribiżew it appears like you are wanting team-building-2022. If that is correct then you just need a simple replace() function (table 9.10) rather than a complicated regular expression. So just:

    select lower(replace('Team Building 2022',' ', '-'));
    

    If that is not the desired result then please post the exact result you want.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search