Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If value contains these letters, then

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.

like image 631
d-_-b Avatar asked Aug 03 '12 12:08

d-_-b


2 Answers

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

like image 126
Tom Chantler Avatar answered Oct 01 '22 09:10

Tom Chantler


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
like image 31
daz-fuller Avatar answered Oct 01 '22 09:10

daz-fuller