Replacing one value with other value in sql select query

Learner 226 Reputation points
2022-05-12T15:31:13.173+00:00

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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,707 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

Accepted answer
  1. Bert Zhou-msft 3,421 Reputation points
    2022-05-13T02:09:40.027+00:00

    Hi,@Learner 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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 101K Reputation points MVP
    2022-05-12T20:18:51.357+00:00

    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
    
    1 person found this answer helpful.