Posts

Showing posts from November, 2013

How to use the YEAR, MONTH and DAY functions?

How to use the YEAR, MONTH and DAY functions? Execute the following Microsoft SQL Server T-SQL scripts in SSMS Query Editor to demonstrate the application of Year, Month and Day datetime functions.   -- T-SQL Year, Month & Day functions DECLARE @Date datetime = '2018-03-15'   -- SQL Server Year function SELECT YYYY = YEAR ( @Date ) SELECT YYYY = DATEPART ( yy , @Date ) -- 2018   -- SQL Server Month function SELECT MM = MONTH ( @Date ) SELECT MM = DATEPART ( mm , @Date ) -- 3   -- SQL Server Day function SELECT DD = DAY ( @Date ) SELECT DD = DATEPART ( dd , @Date ) -- 15  

How to use CASE function to form conditional expressions?

How to use CASE function to form conditional expressions? Execute the following Microsoft SQL Server T-SQL database scripts in Management Studio Query Editor to demonstrate the use of the CASE expression to build complex conditional expressions. -- SQL CASE function in WHERE clause - QUICK SYNTAX DECLARE @ProductID INT = NULL   -- input parameter SELECT * FROM AdventureWorks2008 . Production . Product WHERE ProductID = CASE                     WHEN @ProductID is not null THEN @ProductID                     ELSE ProductID END GO -- (504 row(s) affected) DECLARE @ProductID INT = 800  -- input parameter SELECT * FROM AdventureWorks2008 . Production . Product WHERE ProductID = CASE