SQL Server – Searching for X at the Beginning of a Line in Multi-Line String

sql server

On SQL Server (2008), I have a column which is varchar(max).

I want to query for all rows where the blurb contains a lettered list:

Some sentence is here

a. foo
b. bar
c. baz

Some more text later

Can do this easily in regular expressions, but I do not have access to add regex capabilities to my instance.

So, How can I search using LIKE to find something like this? Presumably I would need to find where the first character of the line (in a multi-line blurb) is a character and 2nd is a dot.

Best Answer

You might have to play with CHAR(13) and CHAR(10) depending on the source of the text, but this might be simply:

WHERE CHAR(10) + col LIKE '%' + CHAR(10) + '[A-Za-z].%'
------^^^^^^^^ in case the text *starts* with a lettered list

Note this will also find:

Some sentence is here

a. foo

Some more text later

If you need to only return rows where there is a list with two or more lines, you may not want to do this in the database, because it would require much more complicated parsing. In fact in that case - and if I couldn't perform the parsing outside the database (or in the database with CLR RegEx), I would probably use an ordered split function.