How to change columns based on another field value

  • I would like to pull data from two seperate columns based on the vaule for MakeFlag. So if MakeFlag = 0 I would

    like the description to show but anything else I would like catalog description to show up.

    DECLARE @MyVari varchar(20)

    SELECT [ProductID]

    ,[prod].[Name]

    ,[ProductNumber]

    ,[MakeFlag]

    ,[FinishedGoodsFlag]

    ,[MyRow] = @MyVari

    FROM [AdventureWorks2014].[Production].[Product] prod

    INNER JOIN [Production].[ProductModel] Prodmod

    ON prod.Name = Prodmod.name

    WHERE Case When

    [Production].[Product].[MakeFlag] = 0 Then @MyVari = [prod].[ProductModelID]

    Else @MyVari = [Prodmod].[CatalogDescription]

    END

  • DECLARE @MyVari varchar(20)

    SELECT [ProductID]

    ,[prod].[Name]

    ,[ProductNumber]

    ,[MakeFlag]

    ,[FinishedGoodsFlag]

    ,[MyRow] = @MyVari

    FROM [AdventureWorks2014].[Production].[Product] prod

    INNER JOIN [Production].[ProductModel] Prodmod

    ON prod.Name = Prodmod.name

    WHERE @MyVari = Case When [Production].[Product].[MakeFlag] = 0

    Then [prod].[ProductModelID]

    Else [Prodmod].[CatalogDescription]

    END

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply