Solved

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

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get Duration of last Status Update 4 35
Sql Query with datetime 3 24
kill process lock Sql server 9 53
SQL Server (2008) Declare Variable (Date) and Set value 6 32
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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