SQL view with If...Then...Else for Column

Posted on 2009-02-17
Last Modified: 2012-05-06

Lets say I have the following table:
OriginalDate   NewDate  GoodData Value
1/1/2008        2/2/2008    1                109
1/2/2008        3/3/2008    0                401

I want to create a view with 2 columns: Date, Value
If GoodData = 1, I want Date to show OriginalDate
if GoodData =0, I want Date to show NewDate

so SELECT * FROM View1 would return
Date         Value1
1/1/2008   109
3/3/2009   401
Question by:sfun28
    LVL 39

    Accepted Solution

    create view View1 as
    select case gooddata when 1 then originaldate when 2 then NewDate else null end, value
    from YourTable
    LVL 17

    Assisted Solution

    CREATE VIEW View1(Date, Value1) AS
    SELECT CASE GoodData WHEN 1 then OriginalDate WHEN 0 THEN NewDate ELSE null END, VALUE
    FROM YourTable;
    The ELSE null part is not really required for your exact requirement.
    LVL 1

    Author Closing Comment


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now