Solved

How to do repalcement of values in sql query in output using select

Posted on 2013-12-09
4
191 Views
Last Modified: 2013-12-11
I have a field id and mode in table.
It can contain values 1 2 3 4 and null.
I want when the mode is 1 the output data should show Monovalent,
for 2 it should show bivalent for 3 it should show referral and 4 it should show slide

In other words it want to do replace in query with these words
0
Comment
Question by:searchsanjaysharma
  • 2
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39705852
SELECT CASE mode
  WHEN 1 THEN 'Monovalent'
  WHEN 2 THEN 'bivalent'
  WHEN 3 THEN 'referral'
  WHEN 4 THEN 'slide' END as name_goes_here
FROM your_table

Also if you want a good read on CASE blocks, the article SQL Server CASE Solutions is a very good demo of the multiple ways to use CASE.
0
 

Author Comment

by:searchsanjaysharma
ID: 39707814
How to display other fields also, Like i have other fields as UID,State,District and mode with above scenario.
0
 

Author Comment

by:searchsanjaysharma
ID: 39707818
SELECT uin,village, CASE
   WHEN mode 0 THEN ''
   WHEN mode 1 then 'Monovalent'
   WHEN mode 2 then 'Bivalent'
   WHEN mode 3 then 'Referral'
   WHEN mode 4 then 'Slide'
   WHEN mode null then 'Slide'
    END as mode
FROM mstm1

Not working
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39707859
>   WHEN mode null then 'Slide'
Use ISNULL or COALESCE to perform NULL handling, not just {columnname} NULL

SELECT uin,village, CASE ISNULL(mode, -1)
   WHEN  0 THEN ''
   WHEN 1 then 'Monovalent'
   WHEN 2 then 'Bivalent'
   WHEN 3 then 'Referral'
   ELSE 'Slide' END as mode
FROM mstm1
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql 2014,  lock limit 5 37
convert null in sql server 12 45
What is this datetime? 1 18
partitioning database after decade growth 8 25
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question