Tuesday 19 March 2013

Date function in SQL Server


Written By:- Isha Malhotra (Software Developer)
Email:-malhotra.isha3388@gmail.com.
Note: -if you find this article helpful then kindly leave your comment

Use of DATE Function in SQL Server
When we need to play with date in sql server, in that case we can use Date Function. Following are some function which I used in my project:-
     1.       Getdate()
This function returns the current date and time in format of ‘ yyyy-mm-dd hh:mm:ss’ where
Yyyy-year
mm-months
dd-date
hh-hour
mm-minutes
ss-Second
For example:-
select GETDATE();
     2.       Day(date)
This functin will return the day part from date in numeric format.
For example:-
select DAY('2013-03-20');
this query will return 20.
  3.  Year(date)
This functin will return the year part from date in numeric format.
For example:-
select year('2013-03-20');
this query will return 2013.

  4.  Month(date)
This functin will return the month part from date in numeric format.
For example:-
select month('2013-03-20');
this query will return 03.
  5.  Datediff(part of date, date1, date 2)
This function will return the difference between two date according to its date part.
For example:-
select DATEDIFF(YEAR,'2012-03-18',getdate())
in this example it will show the difference between these two date according to year. Output of this function will be 1. Similary you can use day, month, week etc in date part.
  6.  Dateadd(part of date, increment number, date)
This functin will add the increment number in your date according to your date part.
For example:-
select DATEADD(year,2,getdate())
this example will add two year in current date. Similary you can use day, month etc in date part.


No comments:

Post a Comment