We will see this in action when we run this function. RETURN the above example, we have created a SQL Function to calculate the age of the employee from the DOB so the function takes a parameter and returns NVARCHAR(50). SET AS NVARCHAR(4))+' Age_Years AS NVARCHAR(2))+' Age_Months AS NVARCHAR(2))+' Age_Days Old' We have the above table Employee which consist of the date of birth and from this, we will calculate the age in terms of a year, month and days in 2 steps Step 1 – Creating a function Select ID,emp_name,emp_dateOfBirth from Employee Examples to Implement SQL DATEADD()īelow are the examples mentioned: 1. This function works in the SQL server starting from the 2008 version, Azure SQL Data Warehouse, Azure SQL Database, Parallel Data Warehouse. It can be a string literal or user-defined variables. It should be an expression that resolves in date, datetime, datetimeoffset, datetime2, smalldatetime, or time value. Msg 8115, Level 16, State 2, Line 8Arithmetic overflow error converting expression to data type int.” The above queries statements both return an error message “Msg 8115, Level 16, State 2, Line 7Arithmetic overflow error converting expression to data type int. The number argument should be an int and it should not exceed its limit positively or negatively. The above statements add a month to the date.
We can also specify the quarter of the year. This argument can be anything that represents a time interval like a month, week, day, year. This is also called datepart and it is provided as a string to this function. Hadoop, Data Science, Statistics & othersĪs we can see in this function there are three arguments and all are mandatory for this function to work and return the integer result