WISDOMBYDATA
  • BLOG
    • Blog Guide
    • Blog History
  • EXCEL
    • Functions & Formulas
    • VBA & Macros
    • VLOOKUP
    • Pivot Tables
    • Conditional Formatting
    • Tricks & Shortcuts
  • BI
    • SAP BOBJ/BW
    • Tableau
  • SQL
  • ABOUT
    • About WBD
    • About Me

Using a Scalar Function to Calculate Employee Seniority

3/14/2014

0 Comments

 
In one of my previous blog posts, I explained how to calculate employee seniority using the features of MS Excel (e.g. VLOOKUP and Yearfrac). In this blog post, I want to demonstrate how to do the same thing using SQL Server Management Studio. I will use a Scalar Function in order to perform this task. A Scalar Function is a function that operates on and returns a single value. 
STEP 1]

The first step is to make sure that the AdventureWorks database is being used by executing the code below in Management studio:

USE AdventureWorks2012 
GO
 
​

STEP 2]
As could be observed in the code below, I have created a function named “dbo.Seniority”. This function takes one parameter (i.e. @ST) and returns a VARCHAR(100). Note that the parameter name(s) must be enclosed in parenthesis and must start with @. 

Create function dbo.Seniority (@ST datetime)   
Returns VARCHAR (100)
AS
BEGIN
DECLARE @EMP int
SELECT @EMP =DATEDIFF(yy, @ST, Getdate())
RETURN (CONVERT (varchar(100), @EMP) + ' - ' + CONVERT(varchar (100), @EMP +2))
END
GO

This function passes in a date (only the year). The function then classifies the seniority in two year groupings. To have this done, I first created a parameter named @EMP. This parameter uses the DATEDIFF command to find the difference bewteen the employees' Hire Date that and the current date).
STEP 3] Execution
In order to use the code which we created in Step 2, we need to use a select statement which will extract data from the HumanResources table. Note that this code calls the "dbo.Seniority" function and then pass in the "HireDate" for  each employee and this parameter will eventually return the string which was defined in Step 2.

SELECT LoginID, BirthDate,SalariedFlag,MaritalStatus,hiredate,dbo.Seniority([HireDate]) as 'Seniority Range (Years)'
FROM [HumanResources].[Employee]
Order by hiredate asc
GO
Picture
STEP 4] Counting the number of employees which fall under each seniority range
SELECT dbo.Seniority (HireDate) as 'Seniority Range (Years)', Count (*) as 'Number of Employees'
FROM [HumanResources].[Employee]
GROUP BY dbo.Seniority(HireDate)
Order by 'Number of Employees' desc
GO
Picture
0 Comments
<<Previous

    Categories

    All
    BI
    EXCEL
    MISC
    SQL

    Archives

    June 2020
    May 2020
    April 2020
    March 2020
    February 2020
    December 2019
    November 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    September 2017
    August 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    May 2015
    April 2015
    March 2015
    February 2015
    January 2015
    December 2014
    November 2014
    October 2014
    September 2014
    August 2014
    April 2014
    March 2014
    February 2014
    January 2014
    December 2013
    November 2013

Powered by Create your own unique website with customizable templates.
  • BLOG
    • Blog Guide
    • Blog History
  • EXCEL
    • Functions & Formulas
    • VBA & Macros
    • VLOOKUP
    • Pivot Tables
    • Conditional Formatting
    • Tricks & Shortcuts
  • BI
    • SAP BOBJ/BW
    • Tableau
  • SQL
  • ABOUT
    • About WBD
    • About Me