Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-09
4
Medium Priority
?
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 66

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 66

Accepted Solution

by:
Jim Horn earned 1500 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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

664 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