Suraz Suraz - 19 days ago 59
SQL Question

Conversion failed when converting the varchar value '<b>' to data type float

Here, What I am trying to do is to display the data in

BOLD
and did for
name
column at
backend
and there isn't any issue but when applying same to the
salary
it's raising an error even using
conversion functions
. So, I am looking for your suggestions OR solutions :

The case is same but data and query is different and I must go through the dynamic query as below because it's huge with various conditions.

CREATE TABLE #test(name VARCHAR(50), salary FLOAT)
INSERT INTO #test VALUES
('a', 1000),
('b', 5000)

DECLARE @sql VARCHAR(8000)

SET @sql = 'SELECT
''<b>'' + name + ''</b>'',
''<b>'' + salary + ''</b>''
FROM #test'

EXEC(@sql)


Thank You.

Answer Source
CREATE TABLE #test(name VARCHAR(50), salary INT)
INSERT INTO #test VALUES
('a', 1000),
('b', 5000)

DECLARE @sql VARCHAR(8000)

SET @sql = 'SELECT 
        ''<b>'' + name + ''<b>'',
        ''<b>'' + CAST(salary AS VARCHAR(20)) + ''<b>''
    FROM #test'

EXEC(@sql)