Question

This is my view

 SELECT e.Text AS StatusText,
        a.Created AS [DATE],
        a.Username,
        b.Name AS CustomerName,
        c.Name AS ServiceName,
        d.Message AS DeviationMessage
   FROM dbo.StatusUpdate AS a
        LEFT OUTER JOIN dbo.Customer AS b ON a.CustomerId = b.CustomerID
        LEFT OUTER JOIN dbo.Service AS c ON a.ServiceId = c.ServiceID
        LEFT OUTER JOIN dbo.Deviation AS d ON a.DeviationId = d.DeviationID
        LEFT OUTER JOIN dbo.StatusText AS e ON a.Status = e.ID

What i would like to do is: When a.Status(int) is 2 or 3, i would like to force a.Username to show as NULL. How should i approach this?

Was it helpful?

Solution

Try this:

 SELECT e.Text AS StatusText,
        a.Created AS [DATE],
        CASE a.status
            WHEN 2 THEN NULL
            WHEN 3 THEN NULL
            ELSE a.Username
        END,  
        b.Name  AS CustomerName,
        c.Name  AS ServiceName,
        d.Message AS DeviationMessage
   FROM dbo.StatusUpdate AS a
        LEFT OUTER JOIN dbo.Customer AS b ON a.CustomerId = b.CustomerID
        LEFT OUTER JOIN dbo.Service AS c ON a.ServiceId = c.ServiceID
        LEFT OUTER JOIN dbo.Deviation AS d ON a.DeviationId = d.DeviationID
        LEFT OUTER JOIN dbo.StatusText AS e ON a.Status = e.ID

OTHER TIPS

You could achieve this pretty easily with a case statment.

So something like

CASE WHEN Status = 3 THEN USERNAME IS NULL

This is untested but the idea is there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top