-1

i really loves mysql virtual generated columns but am having a small issue with it am trying to make a virtual column from a varchar column where i extract the number as the following.

DB::statement('ALTER TABLE reservations ADD number_vc  BIGINT AS (REVERSE(REVERSE(number) << 0)) ');

my issue is in this part REVERSE(REVERSE(number) << 0))

as if you tried now to run this part of sql in any sql editor as the following

SELECT REVERSE(REVERSE("A100") << 0)

it will generate the following

enter image description here

**What Am Trying To Achieve **

if the string was A100 after reverse i need 100 also the schema of the varchar not always one character it may be like this A100 , AA100 , AB100

**Edit 2 :Am Afraid i did go production a week a go and here is the result **

enter image description here

SOS
  • 6,330
  • 2
  • 10
  • 29

1 Answers1

0

With mySQL8+ you can do this :

SELECT REGEXP_REPLACE('A100', '\D', '');

It's basically replacing non digits characters with nothing using a regexp.

aymcg31
  • 321
  • 1
  • 7