sqlregexhana

Is there a SQL function that can help me reverse an order of a string?


I have this string 'Year/Month' and I want to make it 'Month/Year'.

Now I've looked into the REVERSE function, but that would convert something like '2023/10' into '01/3202', which is not what I want.

Then I thought of a regex, but I was not sure how to use a regex function to identify the '/' and move everything in front of the '/' to the end of my string.

Maybe something like REGEX_REPLACE could work? Have never used this.

Any help would be greatly appreciated.


Solution

  • If the data is always formatted as you describe and there is no need for a sanity check then one straightforward approach would be.

    SELECT RIGHT('2023/10',2) + '/'+ LEFT('2023/10',4)