select Event_id,
Timeline_year++Event_title as Timeline,
Event_title,
event_img ,
Timeline_year,
Thumbnail_name,
Overview ,
(select (case event_type when 'impact' then (select event_img) when 'milestone' then (select thumbnail_name) end) as 'asd') from events
where (Event_title like '%Test%' or Timeline_year like '%Test%' or Event_story like '%Test%' or Background like '%Test%' or Background like '%Test%' or Overview like '%Test%' or Image_shortdescription like '%Test%') and Published='Y'and Flag_delete='N' and Event_type='Impact' order by Event_title
select (case event_type when 'impact' then (select event_img) when 'milestone' then (select thumbnail_name) end) as 'asd' from events
Timeline_year++Event_title as Timeline,
Event_title,
event_img ,
Timeline_year,
Thumbnail_name,
Overview ,
(select (case event_type when 'impact' then (select event_img) when 'milestone' then (select thumbnail_name) end) as 'asd') from events
where (Event_title like '%Test%' or Timeline_year like '%Test%' or Event_story like '%Test%' or Background like '%Test%' or Background like '%Test%' or Overview like '%Test%' or Image_shortdescription like '%Test%') and Published='Y'and Flag_delete='N' and Event_type='Impact' order by Event_title
select (case event_type when 'impact' then (select event_img) when 'milestone' then (select thumbnail_name) end) as 'asd' from events
A. Using a SELECT statement with a simple CASE expression
SELECT ProductNumber, Category = CASE ProductLine WHEN 'R' THEN 'Road' WHEN 'M' THEN 'Mountain' WHEN 'T' THEN 'Touring' WHEN 'S' THEN 'Other sale items' ELSE 'Not for sale' END, Name FROM Production.Product ORDER BY ProductNumber;
B. Using a SELECT statement with a searched CASE expression
SELECT ProductNumber, Name, 'Price Range' = CASE WHEN ListPrice = 0 THEN 'Mfg item - not for resale' WHEN ListPrice < 50 THEN 'Under $50' WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250' WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000' ELSE 'Over $1000' END FROM Production.Product ORDER BY ProductNumber ;
C. Using CASE to replace the IIf function that is used in Microsoft Access
SELECT FirstName, LastName, TelephoneNumber, IIf(IsNull(TelephoneInstructions),"Any time", TelephoneInstructions) AS [When to Contact]
FROM db1.ContactInfo; SELECT FirstName, LastName, TelephoneNumber, 'When to Contact' = CASE WHEN TelephoneSpecialInstructions IS NULL THEN 'Any time' ELSE TelephoneSpecialInstructions END FROM Person.vAdditionalContactInfo;
D. Using CASE in an ORDER BY clause
SELECT BusinessEntityID, SalariedFlag FROM HumanResources.Employee ORDER BY CASE SalariedFlag WHEN 1 THEN BusinessEntityID END DESC
SELECT BusinessEntityID, LastName, TerritoryName, CountryRegionName FROM Sales.vSalesPerson WHERE TerritoryName IS NOT NULL ORDER BY CASE CountryRegionName WHEN 'United States' THEN TerritoryName ELSE CountryRegionName END; ,CASE WHEN SalariedFlag = 0 THEN BusinessEntityID END;
E. Using CASE in an UPDATE statement
UPDATE HumanResources.Employee SET VacationHours = ( CASE WHEN ((VacationHours - 10.00) < 0) THEN VacationHours + 40 ELSE (VacationHours + 20.00) END ) OUTPUT Deleted.BusinessEntityID, Deleted.VacationHours AS BeforeValue, Inserted.VacationHours AS AfterValue WHERE SalariedFlag = 0;
F. Using CASE in a SET statement
CREATE FUNCTION dbo.GetContactInformation(@BusinessEntityID int) RETURNS @retContactInformation TABLE ( BusinessEntityID int NOT NULL, FirstName nvarchar(50) NULL, LastName nvarchar(50) NULL, ContactType nvarchar(50) NULL, PRIMARY KEY CLUSTERED (BusinessEntityID ASC) ) AS -- Returns the first name, last name and contact type for the specified contact. BEGIN DECLARE @FirstName nvarchar(50), @LastName nvarchar(50), @ContactType nvarchar(50); -- Get common contact information SELECT @BusinessEntityID = BusinessEntityID, @FirstName = FirstName, @LastName = LastName FROM Person.Person WHERE BusinessEntityID = @BusinessEntityID; SET @ContactType = CASE -- Check for employee WHEN EXISTS(SELECT * FROM HumanResources.Employee AS e WHERE e.BusinessEntityID = @BusinessEntityID) THEN 'Employee' -- Check for vendor WHEN EXISTS(SELECT * FROM Person.BusinessEntityContact AS bec WHERE bec.BusinessEntityID = @BusinessEntityID) THEN 'Vendor' -- Check for store WHEN EXISTS(SELECT * FROM Purchasing.Vendor AS v WHERE v.BusinessEntityID = @BusinessEntityID) THEN 'Store Contact' -- Check for individual consumer WHEN EXISTS(SELECT * FROM Sales.Customer AS c WHERE c.PersonID = @BusinessEntityID) THEN 'Consumer' END; -- Return the information to the caller IF @BusinessEntityID IS NOT NULL BEGIN INSERT @retContactInformation SELECT @BusinessEntityID, @FirstName, @LastName, @ContactType; END; RETURN; END; GO SELECT BusinessEntityID, FirstName, LastName, ContactType FROM dbo.GetContactInformation(2200); GO SELECT BusinessEntityID, FirstName, LastName, ContactType
FROM dbo.GetContactInformation(5);
G. Using CASE in a HAVING clause
SELECT JobTitle, MAX(ph1.Rate)AS MaximumRate FROM HumanResources.Employee AS e JOIN HumanResources.EmployeePayHistory AS ph1 ON e.BusinessEntityID = ph1.BusinessEntityID GROUP BY JobTitle HAVING (MAX(CASE WHEN Gender = 'M' THEN ph1.Rate ELSE NULL END) > 40.00 OR MAX(CASE WHEN Gender = 'F' THEN ph1.Rate ELSE NULL END) > 42.00) ORDER BY MaximumRate DESC; http://msdn.microsoft.com/en-us/library/ms181765.aspx