Lecture 6 - Built-In Functions
Lecture 6 - Built-In Functions
Instructor:
CONVERSION FUNCTIONS
▪ The Cast() function is used to convert a data type variable or data from one
data type to another data type.
▪ The Cast() function provides a data type to a dynamic parameter (?) or a NULL
value.
▪ The GETDATE() function returns the current date and time from the SQL
Server.
▪ The DATEPART() function is used to return a single part of a date/time, such as
year, month, day, hour, minute, etc.
Syntax:
GETDATE()
DATEPART(datepart, date)
▪ Ex : SELECT GETDATE()
SELECT DATEPART(YYYY, GETDATE())
Result :
▪ Ex:
DECLARE @date1 DATETIME
DECLARE @date2 DATETIME
SET @date1= '2019-04-07 20:12:22.013'
SET @date2= '2020-02-27 22:14:10.013'
SELECT DATEDIFF(month, @date1, @date2) AS 'Month'
Result: 10
STRING FUNCTIONS
Syntax:
SUBSTRING(str, position, length)
▪ Ex : SELECT CHARINDEX('bicycle',
'Reflectors are vital safety components of your bicycle.') AS Positions
SELECT PATINDEX ( '%ein%', 'Das ist ein Test') AS Positions
Result:
✓ String Functions
RTRIM, LTRIM, SUBSTRING, LEN, CHARINDEX, PATINDEX
✓ Demo