1

Need help to do a simple task in sql string in my table there is a field comment(user comments).

In my procedure, I am passing a parameter @input. I want to check whether the string contains the string @input, and if it contains @input then add some style to the substring(that same as @input).

eg: if the Comment string like - "i have added the user Test user" and

@input - "tes"

I want the output as

i have added the user <span style="color:red">Tes</span>t user
2
  • 2
    For what database? @ for variables is supported by SQL Server and MySQL. Commented Feb 16, 2011 at 5:49
  • Can you make your question more clearer? Something like create the result of table... Commented Feb 16, 2011 at 5:54

2 Answers 2

1

Using T-SQL (Microsoft SQL)

SELECT REPLACE(comment, @input, '<span style="color:red">' + SUBSTRING(comment, PATINDEX('%' + @input + '%', comment), len(@input)) + '</span>') as cmt
FROM thetable
WHERE comment LIKE '%' + @input + '%'

EDIT: OK Even more ugly and still untested, but should work for you. Now we are replaceing the exact text from the string back into the replace.

UPDATE: I just tested this code and it works as you need it to. It finds Tes in the string using 'tes' and outputs Tes as it is in the original string.

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

2 Comments

Hi , thanks....but problem is it will modify the actual string...eg..if the string "i have deleted the user 'Test user'"..this will produce and output -"i have deleted the user '<span style="color:red">'tes</span>t' user'"...here the name 'Test' changes to test....i think u understand my problem
The is the problem you deal with when you work with case insensitive collation.
1
select comment from table where comment like '%' + @input + '%'

Irrespective of the RDBMS, there is LIKE in other RDBMS(SQLserver, Oracle, Mysql). Specifically my answer is assuming your DB is Sql server since I noticed the use of @

EDIT: To get the <span> tag, I think you may have to use regex to replace the input string with <span> tag in the output result. Better would be to do it in the underlying code instead of getting results from SQL query with format. In SQL server you can use REPLACE function too.

4 Comments

Where's the text updating? Oracle doesn't support @ in a variable either.
thanks for reply,Sql server 2005 - but my problem is ,i got the all comments by using the s"elect comment from table where comment like @input" statements,but i want to modify the substring( like @input) in the string as i explained in frist post
@OMG Ponies - Aaah.. The question is edited after my answer. Sorry, did not see the edit. The <span> tag was not visible in first version of question. Hence my answer.
This will only do an exact match to @input even though you used LIKE. You need to add a wild card(s).

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.