0

Right, so I am trying to write a REGEX function. Trying to write it as sort of a stored procedure, I guess (assuming that if that is the right way to go about it)?

Problem : I have a table with millions of rows. I would like to create a REGEX function that scrapes out the 6 alpha numeric CML code (mentioned in the table). Sometimes, it does not even mention the word CML and the code is still there. See Row 5 for reference below

My approach

SELECT * 
FROM [Reporting].[dbo].[Master] 
WHERE [Notes] LIKE '[C][M][L]%'

I am not too sure if that is the right way to go about it or not.

Desired result : I want to be able to write a code using a REGEX function which can look for such patterns in the notes column and put it as a separate column against its respective Part ID

This is the table:

Table

Edit - A typo at my end occurred for L987234. The length of these characters is also 6, hence, it actually is L98723

14
  • If you want Regex functions in SQL Server, you need to use CLR objects. Commented Aug 1, 2022 at 18:26
  • If you can't precisely describe what makes a valid CML code (other than being six characters) then how is a regex going to help? Commented Aug 1, 2022 at 18:32
  • Those 6 characters is the only way to identify it. Sometimes it might mention "CML" sometimes, it may not but based on my tribal knowledge, I know those 6 characters will always be the code I want to extract. Commented Aug 1, 2022 at 18:35
  • @Larnu, can you elaborate it briefly? Thanks Commented Aug 1, 2022 at 18:35
  • There is no in built support for Regex on SQL Server, @AttitudeBlack . To be able to use Regex you have to create CLR functions so that you can use those instead. Commented Aug 1, 2022 at 18:37

1 Answer 1

1

Assuming SQL Server 2016 or greater:

SELECT m.<columns>,
  Code = s.value
FROM Reporting.dbo.[Master] AS m
CROSS APPLY STRING_SPLIT(m.Notes, ' ') AS s
 WHERE LEN(s.value) = 6
 AND s.value LIKE '%' + REPLICATE('[0-9A-Z]', 6) + '%'
 AND s.value LIKE N'%[0-9]%'  -- make sure it has at least one digit
 AND s.value LIKE N'%[A-Z]%'; -- make sure it has at least one letter

If you're on a lower version, then create your own function (like this one), then replace STRING_SPLIT with dbo.SplitString.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank-you. Let me give it a try and I shall let you know.
Works. Thank-you very much. Appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.