Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to replace values in SQL query?

I would like to know if it is possible in an SQL query to replace some values by something else, or if I need to do that in post-processing.

Let me explain. I have this table:

|username| accepted |
|--------|----------|
|  toto  |   NULL   |
|--------|----------|
|  foo   |    0     |
|--------|----------|
|  Rick  |    1     |
|--------|----------|
|  bar   |    1     |
|--------|----------|

I want to know the numbers of row per value of accepted (nullable bit). I'm running this query:

SELECT [accepted], count(*) FROM my_table GROUP BY [accepted]

Which should return:

NULL   1  
false  1  
true   2

Is there some way to replace the accepted values by more meaningful labels? Ideally I would like to have something like:

not_available 1
not_accepted  1
accepted      2

Is that feasible with SQL server 2008 R2?

Thx.

like image 271
Antoine Avatar asked Aug 02 '11 08:08

Antoine


People also ask

Can we replace multiple values in SQL?

Using the REPLACE() function will allow you to change a single character or multiple values within a string, whether working to SELECT or UPDATE data.


4 Answers

If you have a few values:

SELECT CASE [accepted] 
WHEN 0 THEN 'not_accepted' 
WHEN 1 THEN 'accepted '
ELSE 'not_available' END AS accepted
, count(*) 
FROM my_table 
GROUP BY CASE [accepted] 
WHEN 0 THEN 'not_accepted' 
WHEN 1 THEN 'accepted '
ELSE 'not_available' END
like image 54
niktrs Avatar answered Oct 03 '22 07:10

niktrs


Use a CASE

SELECT
    CASE [accepted]
       WHEN 1 THEN 'accepted'
       WHEN 0 THEN 'not_accepted'
       ELSE 'not_available' --NULL
    END AS [accepted], count(*)
FROM my_table
GROUP BY --try [accepted] by itself first
    CASE [accepted]
       WHEN 1 THEN 'accepted'
       WHEN 0 THEN 'not_accepted'
       ELSE 'not_available' --NULL
    END

You may have to use the whole CASE in the GROUP BY. Unless you do this

SELECT
    CASE [accepted]
       WHEN 1 THEN 'accepted'
       WHEN 0 THEN 'not_accepted'
       ELSE 'not_available' --NULL
    END AS [accepted], CountOfAccepted
FROM
   (SELECT [accepted], count(*) AS CountOfAccepted
    FROM my_table GROUP BY [accepted]) foo
like image 36
gbn Avatar answered Oct 03 '22 06:10

gbn


SELECT
    CASE [accepted]
        WHEN 1 THEN 'accepted', 
        WHEN 0 THEN 'not accepted',
        ELSE 'not available'
    END AS [accepted],
    count(*) 
FROM my_table GROUP BY [accepted]

?

Not sure if this is exactly right and don't have SQL Server available to test now, but should be something like that.

EDIT: aww, already posted

like image 37
potNPan Avatar answered Oct 03 '22 07:10

potNPan


yeah it is there ... you can modify your query as below -

SELECT decode(accepted,NULL,'not_available' ,1,'accepted',0,'not_accepted'),count(*) 
FROM my_table GROUP BY 
 decode(accepted,NULL,'not_available' ,1,'accepted',0,'not_accepted')

it should give output as you want..

may be decode function is not there then you can use case over there..

SELECT 
case accepted 
    WHEN 1 THEN 'accepted'
    WHEN 0 THEN 'not_accepted'
    ELSE 'not_available' 
end,count(*) 
    FROM my_table GROUP BY     
 case accepted 
    WHEN 1 THEN 'accepted'
    WHEN 0 THEN 'not_accepted' 
    ELSE 'not_available' end
like image 44
pratik garg Avatar answered Oct 03 '22 06:10

pratik garg