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. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 18 Times in 18 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
  •