简体   繁体   中英

Filter table using Regex like functionality

I have an table on SQL Server which has two columns like below.

OldValue NewValue
ReqNo: 123456789 ReqNo: 89898989
RandomGibberish: , ReqNo: RandomGibberish: , ReqNo: 12121212

I want to be able to filter the table if both the columns OldValue and NewValue start with "ReqNo: " followed by an 8 digit number and are an exact match to this pattern.

This would mean that the first row would be in my output but not the second row.

I know how to do this in python but still new to SQL Server and can't seem to find the right syntax.

Please help.

You could use SQL Server's enhanced LIKE operator here:

SELECT *
FROM yourTable
WHERE OldValue LIKE 'ReqNo: [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' AND
      NewValue LIKE 'ReqNo: [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]';

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