Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using replacement in SQL 2005 Query

Posted on 2009-04-10
6
Medium Priority
?
344 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:udaya kumar laligondla
ID: 24116749
use
select 'C' as Col1,col2,...,colN form tableName where Col1=6
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 5

Assisted Solution

by:mfhorizon
mfhorizon earned 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

926 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