In SQL Server, is there a way to search (and format) a result for a certain string (like preg_match
in PHP):
SELECT (IF (col1 contains 'http'){'<a>' + col 1 + '</a>'}
else {'<b>' + col1 + '</b>'}),
col2
FROM Table
... etc.
Is this possible? I want to format the results based on their content.
Try this:
SELECT CASE WHEN col1 LIKE '%http%' THEN '<a>' + col1 + '</a>' ELSE '<b>' + col1 + '</b>' END AS 'Desired Column Name', col2
FROM table
See the MSDN Documentation here: http://msdn.microsoft.com/en-us/library/ms181765.aspx
You could try something like this (from the AdventureWorks database)
SELECT Title,
FirstName,
LastName,
CASE
WHEN LastName LIKE 'G%' THEN '<a href="mailto:' + EmailAddress + '">' + LastName + '</a>'
ELSE '<b>' + LastName + '</b>'
END
FROM SalesLT.Customer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With