/*
DatePart, DateAdd and DateDiff functions in SQL Server
DATEADD (datepart, number, date) – Returns the DateTime value, after adding
specified NumberToAdd, to the datepart specified of the given date.
datepart it may be – day, weekday, month, year
Number: value which you want to add in date
*/
select dateadd(day,2,getdate()); — we are adding 2 days in today date
select dateadd(year,2,getdate()); — like above we are adding 2 years
— month and weekday DIY (do it yourself)
/*
DATEDIFF(interval, startdate, enddate) –
Returns a value corponding to datepart between startdate and enddate.
interval parameter: may be a day or weekday or month or year
parameter mean: the data which we pass to function or procedure
*/
select datediff(day,’2024-01-01′,getdate());
/*
create function func_name (@paramter datatype) returns datatype
as
begin
…………..
…………….
return value
end;
*/
create function fn_sum(@n1 int, @n2 int) returns int
as
Begin
declare @r int
set @r=@n1+@n2
return @r
End;
select dbo.fn_sum(100,200);
–select dbo.fn_getage(‘1985-09-02’);
/*
create a logic to display a date in Year, Months and Days
5 Years, 10 Months , 55 Days
*/
select * from tblemp;
select eno,fname,dbo.fn_getage(hiredate) from tblemp;
select eno,fname,’$’+ cast(sal as varchar(50)) from tblemp;