CPK_2011
CPK_2011

Reputation: 1150

Append text to each row of the sql select query

I have a query like this

SELECT COUNT(ID) 'Records Affected', TYPE FROM MASTER
GROUP BY TYPE

The output for this is

Records Affected            TYPE
----------------            ---- 
4                            F1
3                            F2
5                            F3

Now I would like to change the query so that the output will be as follows

Records Affected
----------------
The number of records affected for F1 is : 4 
The number of records affected for F2 is : 3
The number of records affected for F3 is : 5

"The number of records affected for " + TYPE + " is : " + COUNT.

How can I add the default text to each row of the result set instead of appending in the front end. I would like to simplify my task of just showing the records in the DataGrid as Summary.

Upvotes: 9

Views: 100479

Answers (5)

MicroMage
MicroMage

Reputation: 11

Use this query:

UPDATE bookmark_linx SET link_url=(SELECT CONCAT(link_url, '?raw=true')) WHERE link_url LIKE '%dropbox%'

Upvotes: 1

Rajesh
Rajesh

Reputation: 7876

SELECT "The number of records affected for " + TYPE + " is : " + COUNT(ID) AS [Records Affected]
FROM Master 
GROUP BY TYPE

Upvotes: 1

Ted Elliott
Ted Elliott

Reputation: 3493

Just put the text in your query:

SELECT 'The number of records affected for ' + TYPE + ' is : ' + CAST(COUNT(ID) as VARCHAR(20)) AS 'Records Affected' FROM MASTER
GROUP BY TYPE

Upvotes: 2

Taryn
Taryn

Reputation: 247650

You can easily concatenate the string using the following. You will use the + to concatenate the string to the type column and the count. Note, the count needs to be converted to a varchar for this to work:

SELECT 
  'The number of records affected for '+ type +
    ' is : '+ cast(COUNT(ID) as varchar(50)) as'Records Affected'
FROM yt
GROUP BY TYPE;

See SQL Fiddle with Demo

Upvotes: 14

unlimit
unlimit

Reputation: 3752

Try this:

SELECT 'The number of records affected for ' + TYPE + ' is : ' + 
STR(X.[Records Affected]) AS [Records Affected]
FROM (SELECT COUNT(ID) 'Records Affected', TYPE FROM MASTER GROUP BY TYPE) X

Upvotes: 0

Related Questions