Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add single quotes to results in a column from a SQL query

Tags:

sql

sql-server

So I currently have the following SQL:

SELECT opportunities.ref FROM opportunities GROUP BY opportunities.ref

which returns the following results:

OP10
OP252
OP52905
OP42

I am trying to achieve the following:

'OP10'
'OP252'
'OP52905'
'OP42'

I have tried the following:

SELECT opportunities.ref FROM opportunities
CASE WHEN opportunities.ref IS NOT NULL THEN "'"+opportunitiesref+"'"
GROUP BY opportunities.ref

but this does not work.

I do not want to update the column, or use any @declare functions as this. I am currently using SQL server 2008. Im not sure how to go about this. Help?

like image 332
Sammy Sung Avatar asked Dec 05 '22 15:12

Sammy Sung


2 Answers

If you are using SQL SERVER then modify your query according to below :

SELECT opportunities.ref FROM opportunities
CASE WHEN opportunities.ref IS NOT NULL THEN ''''+opportunitiesref+''''
GROUP BY opportunities.ref
like image 192
Gaurav Rajput Avatar answered Mar 23 '23 09:03

Gaurav Rajput


If you are using SQL SERVER then refer below queries. it will work.

1)

SELECT opportunities.ref,concat('''',opportunities.ref,'''')
FROM opportunities
GROUP BY opportunities.ref

2)

SELECT opportunities.ref,
CASE WHEN opportunities.ref IS NOT NULL THEN ''''+opportunities.ref+'''' END
FROM opportunities
GROUP BY opportunities.ref
like image 43
Kiran Ukande Avatar answered Mar 23 '23 10:03

Kiran Ukande