Solved

Using replacement in SQL 2005 Query

Posted on 2009-04-10
6
336 Views
Last Modified: 2012-05-06
I have data like:
6,Test1,Test2,Test3
3,Test1,Test2,Test3
1,Test1,Test2,Test3
2,Test1,Test2,Test3

In a SQL 2005 Database (4 columns in my example).  I need to do a SQL * select query so that for records with a "6" in Col 1, it will replace the 6 with the letter "C" in the output.  Also, the query would only select records with a 6 in Col 1 (where Col1=6).  Is there an easy way to do replacements in SQL queries?

Thanks,

Joe
0
Comment
Question by:netwrked
6 Comments
 
LVL 12

Expert Comment

by:udayakumarlm
ID: 24116749
use
select 'C' as Col1,col2,...,colN form tableName where Col1=6
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24116750
you mean:
select 'C' col1, col2, col3, col4
 from yourtable
 where col1 = 6

Open in new window

0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24116760
select (case column1 when 6 then 'C' when 3 then 'd' when 1 'e' when 2 then 'f' end) column1, column2, column3, column4 from MYTABLE

enjoy!!1
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Assisted Solution

by:mfhorizon
mfhorizon earned 250 total points
ID: 24116780
In my example you can show uniqe alphabet on any given digit in your column 1. If you want this only for column value 6 and actual digit for other rows then use this query

select (case column1 when 6 then 'C' else column1 end) column1, column2, column3, column4 from MYTABLE

CHEERS!!

0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24116797
I appologize I forgoet 'then' in my first recommended query!!

just replace [  when 1 'e'   ] with [  when 1 then 'e'  ]

thanks
0
 

Author Closing Comment

by:netwrked
ID: 31568959
Both worked well, thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
email about the whoisactive result 7 35
Getting max record but maybe not use Group BY 2 27
SYbase 4 27
T-SQL: New to using transactions 9 20
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article I will describe the Copy Database Wizard 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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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