• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

Help with SQL View

I have a SQL View and i want to add an additional column to compute the value based on 2 other columsn within the same view. ie

If i have a column by the name, NOTIFICATION_STEP_ID  and if it hold a value 1 or 2 or 3 then
i want to expose a new column that show the combination of 2 columns .

Curent View

SELECT ID, NOTIFICATION_STEP_ID,NOTIFICATION_STEP,CREATE_DATE FROM TABLE_NAME

Expected View

SELECT ID, NOTIFICATION_STEP_ID,CREATE_DATE, NEW_COLUMN FROM TABLE_NAME

the NEW_COLUMN should hold the value of CREATE_DATE + ',' + NOTIFICATION_STEP

If the NOTIFICATION_STEP_ID is not (2,3) then i need to display only NOTIFICATION_STEP

Can i use a IF ELSE CLAUSE IN A VIEW. I tried it and i am getting an error.

How can i achieve my objective. Please illustrate the view .

Thanks
0
TECH_NET
Asked:
TECH_NET
1 Solution
 
SharathData EngineerCommented:
try like this
ALTER VIEW Your_View
SELECT ID, NOTIFICATION_STEP_ID,NOTIFICATION_STEP,CREATE_DATE,
       CASE WHEN NOTIFICATION_STEP_ID IN (2,3) THEN CONVERT(nvarchar,NOTIFICATION_STEP)
            ELSE CONVERT(nvarchar,CREATE_DATE) + ',' + CONVERT(nvarchar,NOTIFICATION_STEP) END AS NEW_COLUMN
  FROM TABLE_NAME

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now