简体   繁体   中英

regular expressions in SQL

I need to verify an ESN number using javascript. i was quite successful to succeed it by using a simple regular expression and an if statement:

 $('#ESN_val').on('keyup', function () {
                var ESN = document.getElementById('ESN_val').value;
                if (/^[0-9A-F]{8}$/.test(ESN) && ESN.length == 8) {              
                alert('valid ESN')
                }else{
                    'invalid ESN'
                } 
            });

Now I just need to get the same result from sql once the whole data contains within the table as attached . Table tsp_filtered_data name:

I need the query in the below format. There should be a new column and it should include 'valid ESN' or 'Invalid ESN'

SELECT
tsp_filtered_data.Document_id,
tsp_filtered_data.ESN,
(**********Regular Expresseion********) AS ESN_status 
FROM
tsp_filtered_data

Kindly tell me the query that I should include.

You can use the REGEXP operator to check for a match. It will return 0 or 1 as a result.

SELECT
tsp_filtered_data.Document_id,
tsp_filtered_data.ESN,
tsp_filtered_data.ESN REGEXP '^[0-9A-F]{8}$' AS ESN_status 
FROM
tsp_filtered_data

A CASE WHEN and an RLIKE/REGEX should do.

SELECT
t.Document_id,
t.ESN,
case when t.ESN RLIKE '^[0-9A-F]{8}$' then 'valid ESN' else 'invalid ESN'  end AS ESN_status 
FROM
tsp_filtered_data t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM