Function in SQL
Function in SQL
❖ The following functions can be used to get the current system date and time:
select getdate()
select SYSDATETIME()
❖ Some Usefull Datetime Functions:
1- ISDATE() - Checks if the given value, is a valid date, time, or datetime. Returns 1 for success, 0
for failure.
Select ISDATE('IT') -- returns 0
Select ISDATE(Getdate()) -- returns 1
Select ISDATE('2012-08-31 21:02:04.167') -- returns 1
2- Day() - Returns the 'Day number of the Month' of the given date
Select DAY(GETDATE()) -- Returns the day number of the month, based on current system
datetime.
Select DAY(2010-10-31) -- Returns 31
3- Month( ) - Returns the 'Month number of the year' of the given date.
Select Month(GETDATE( )) -- Returns the Month number of the year, based on the
current system date and time
Select Month('2010-10-31') -- Returns 10
4- Year() - Returns the 'Year number' of the given date.
Select Year(GETDATE( )) -- Returns the year number, based on the current system date
Select Year('2010-10-31') -- Returns 2010
5- DateName(DatePart,Date) - Returns a string, that represents a part of the given
date. 'DatePart' parameter specifies, the part of the date, we want.
1
Select DATENAME(Day, '2012-09-30 12:43:46.837') -- Returns 30
Select DATENAME(YEAR, '2012-09-30 12:43:46.837') -- Returns 2012
Select DATENAME(MONTH, '2012-09-30 12:43:46.837') -- Returns
September
Select DATENAME(HOUR, '2012-09-30 12:43:46.837') -- Returns 12
Select DATENAME(wk, '2012-09-30 12:43:46.837') -- Returns 40
➢ Valid Datepart parameter values are:
datepart Abbreviations
year yy, yyyy
month mm, m
day dd, d
week wk, ww
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns
Examples:
Examples:
Select DATEDIFF(MONTH,'2005-11-30','2006-01-31') -- returns 2
Select DATEDIFF(DAY,'2005-11-30','2006-01-31') -- returns 62
Select DATEDIFF(hh,'2005-11-30','2006-01-31') -- returns 1488
2
8-CAST: It is used to convert one data type to another in SQL server.
3
id name Daily _Salary
1 Rami 6
2 Sam 7
3 Ahmed 5
4 Ali 8
QUSTION : Create a function which calculates and returns the factorial of a given number then use it to
compute the factorial of each Daily_salary in the above table .
✓ Scalar user defined functions can be used in the Where clause, as shown below:
4
QUESTION: Find the id and name of all employees whose factorial of their daily salary is less than
5000
from Employee
Where dbo.fact(Daily_salary)< 5000 ;
➢ important Notes:
EX:
Create a function that returns EMPLOYEES details by GENDER from the table bellow .
5
from tblEmployees
where Gender = @Gender)
Home Work
1- Write the previous function fact using the stored procedure concept then execute it.
2- Write a user defined function that simulate the power system function in SQL Server for example:
power2(3,2) -- returns 9
3- Consider the following student table:
id S_name DateOfBirth
1 Maged 1994-01-24 02:30:20.260
2 Khalid 1985-01-20 19:10:20.280
3 Reham 2000-10-20 15:50:09.290
4 Sami 1992-04-25 09:30:20.390
- Create a function which calculates and returns the age of any student by using datetime functions
explained above when the date of birth is given then apply this function to find the name and age of all
the students.