Solved

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

Posted on 2013-12-09
4
184 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now