Results 1 to 2 of 2
  1. #1
    3 Star Lounger
    Join Date
    Jul 2008
    Location
    Suffolk, United Kingdom
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Create a view with an If Function (SQL Enterprise Manager)

    Hi everyone,

    I am trying to creat a view of a table but include an if function on column 'WARD' the primary table is called tbl_Discharge and the table with exclusions is called tbl_Profile_Detail

    If the data from tbl_Discharge.Ward is in tbl_Profile_Detail.Data then return "OTHER" else return tbl_Discharge.Ward

    Heres what I have got but I don't know how or if you can right If functions into an sql view

    SELECT dbo.tbl_Discharge.CRN, dbo.tbl_Discharge.Discharge_Date, { fn HOUR(dbo.tbl_Discharge.Discharge_Time) } AS Hour, dbo.tbl_Discharge.Consultant,
    dbo.tbl_Discharge.DOW
    FROM dbo.tbl_Discharge LEFT OUTER JOIN
    dbo.tbl_Profile_Detail ON dbo.tbl_Discharge.Ward = dbo.tbl_Profile_Detail.Data
    Regards
    Gerbil (AKA Kevin)

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Re: Create a view with an If Function (SQL Enterprise Manager)

    You could try a CASE expression:

    SELECT dbo.tbl_Discharge.CRN, dbo.tbl_Discharge.Discharge_Date, { fn HOUR(dbo.tbl_Discharge.Discharge_Time) } AS Hour,
    dbo.tbl_Discharge.Consultant, dbo.tbl_Discharge.DOW, TheWard = CASE dbo.tbl_Profile_Detail.Data
    WHEN dbo.tbl_Profile_Detail.Data Is Null THEN dbo.tbl_Discharge.Ward
    ELSE 'Other'
    END
    FROM dbo.tbl_Discharge LEFT OUTER JOIN
    dbo.tbl_Profile_Detail ON dbo.tbl_Discharge.Ward = dbo.tbl_Profile_Detail.Data

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •