266 Part I EXAM PREPARATION Although that s certainly

266 Part I EXAM PREPARATION Although that s certainly usable in this case, it s not usable if you want to embed the logic into an INSERT or UPDATE statement; plus the CASE involves a lot less typing. Here s another way to write the same code with a different syntax of CASE statement: DECLARE @Result char(10) SET @Result = CASE WHEN datepart(weekday, getdate()) = 1 .THEN Sunday WHEN datepart(weekday, getdate()) = 2 THEN Monday WHEN datepart(weekday, getdate()) = 3 THEN Tuesday WHEN datepart(weekday, getdate()) = 4 THEN Wednesday WHEN datepart(weekday, getdate()) = 5 THEN Thursday WHEN datepart(weekday, getdate()) = 6 THEN Friday WHEN datepart(weekday, getdate()) = 7 THEN ‘Saturday ELSE Unknown END This code will do the exact same thing, but notice that the expression isn t split up. That allows you to do something like this: DECLARE @Result char(10) SET @Result = CASE WHEN datepart(weekday, getdate()) in .(1,7) THEN Weekend ELSE Weekday END PRINT @Result You couldn t use the IN clause with the simple case covered previously, but the searched form of the CASE statement is allowed to use IN along with the rest of the comparison operators. The CASE expression is most often used in SELECT statements to modify data. For example, imagine that you want to find out which objects in your database were created on a weekend: SELECT name, CASE WHEN datepart(weekday,crdate) in (1,7) THEN . Weekend ELSE Weekday END FROM sysobjects CASE statements can also be used in the WHERE clause, an ORDER BY clause, or anywhere else an expression is allowed, like this: SELECT name FROM sysobjects WHERE CASE id % 2 WHEN 1 THEN 1 ELSE 0 END = 1

If you looking for unlimited one inclusive web hosting plan please check cheap web hosting website.

Comments are closed.