question

newbee avatar image
0 Votes"
newbee asked BertZhoumsft-7490 edited

Replacing one value with other value in sql select query

Hello All,

I have a table which have 60 columns

Now my requirement is I need to create a query which will select all 60 columns from the table and I have to replace TrendType column value as Quarterly if it is Monthly .

Could any one please help how to do

sql-server-generalsql-server-transact-sql
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

While asking a question you need to provide a minimal reproducible example:
(1) DDL and sample data population, i.e. CREATE table(s) plus INSERT, T-SQL statements.
(2) What you need to do, i.e. logic, and your attempt implementation of it in T-SQL.
(3) Desired output based on the sample data in the #1 above.
(4) Your SQL Server version (SELECT @@version;)

0 Votes 0 ·
BertZhoumsft-7490 avatar image
0 Votes"
BertZhoumsft-7490 answered BertZhoumsft-7490 edited

Hi,@newbee
Welcome to Microsoft T-SQL Q&A Forum!

Please Try this:

 create table #test
 (
  column1 int ,
  TrendType varchar(20)
    
 )
 insert into #test values(1,'yearly' ),
 (2,'Monthly' ),(3,'Monthly') ,(4,'Quarterly') ,(5,'Monthly' )
    
 select column1,
 case when TrendType = 'Monthly' then 'Quarterly'  else TrendType end TrendType 
 from #test


Best regards,
Bert Zhou


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




image.png (3.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered

The question is grossly unclear, but it sounds like you want this:

SELECT col1, col2, ... 
       IIF(TrendType = 'Monthly', 'Quarterly', TrendType) AS TrendType, 
        col58, col59, col60
FROM   tbl
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.