sql query

Posted on 2011-09-26
Last Modified: 2012-05-12
Using MS Sql Server 2008.

My table has field name  DCode1, DCode2, DCode3, DCode4, DCode5. The values of all these DCode fields is either 'F1' or 'F7'.
I want to write a select statement with the following constraint:
When any DCode value is F1 then Go in F1, if DCode value is 'F7', then go in F7.

Select ???? as F1, ???? as F7.............How do I do that?

Question by:patd1
LVL 15

Expert Comment

ID: 36599615
I'm not really sure what you're talking about when you say go in F1/F7.  Can you provide some example data including what it looks like in the table and what the expected results are?

Author Comment

ID: 36599659
DCode1, Dcode2,Dcode3, Dcode4, Dcode5
F1, F7, a,b,c

I want this to display as follows:
F1, F7


Accepted Solution

bitref earned 500 total points
ID: 36600454
Select (Case When (DCode1 = 'F1' OR DCode2 = 'F1' OR DCode3 = 'F1' OR DCode4 = 'F1' OR DCode5 = 'F1') Then 'Y' Else 'N' END) as F1
, (Case When (DCode1 = 'F7' OR DCode2 = 'F7' OR DCode3 = 'F7' OR DCode4 = 'F7' OR DCode5 = 'F7') Then 'Y' Else 'N' END) as F7
From MyTable

Open in new window


Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Max Consumption Rate (MCR) 3 50
SQL Exceptions 3 43
Correct Thousand and decimal separator in sql server 2008 3 25
Find data in a column which is not in  a date format 29 30
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

19 Experts available now in Live!

Get 1:1 Help Now