Sample Header Ad - 728x90

Microsoft SQL Server - Get first n alphanumeric characters with variable length, meeting ABCD-1234 or ABCD-1234567 format

2 votes
1 answer
402 views
My MS SQL 2019 server version is 15.0.2080.9 (X64) I have a table like | id | message | | -------- | -------------- | | 2003 | ABCD-1234: some text | | 2897 | ABCD-5678 | | 2456 | ABCD-675456: some text | | 3204 | ABCD-4512345 :some text | | 4567 | ABCD-2345 | My requirement is to create another column in SELECT query, for each row to get like | id | message | key | -------- | -------------- | -------------- | | 2003 | ABCD-1234: some text | ABCD-1234 | 2897 | ABCD-5678 | ABCD-5678 | 2456 | ABCD-675456 some text | ABCD-675456 | 3204 | ABCD-4512345 :some text |ABCD-4512345 | 4567 | ABCD-2345 |ABCD-2345 Meaning I need from position 0 till ABCD-1234 format is satisfied discarding empty or any other text after that. I tried LEFT, RIGHT and Substring but could not find a way to get the exact length to meet the regex criteria from position 0. Can you please help me out? Thanks in advance
Asked by Ramakrishnan Srinivasan (53 rep)
Nov 9, 2022, 03:16 AM
Last activity: Nov 9, 2022, 11:31 AM