Learn working of Date function in SQL

1
9430
SQL –Date Function

In programming, situation usually comes to manage data with dates. Sql inbuilt function provides help to manipulate the data to avoid date errors in different scenario.

Following are the commonly used inbuilt Date function in sql server.

1. GetDate()
2. Convert()
3. IsDate()
4. DatePart()
5. DateADD()
6. DateDiff().
7. DateName()
8. Day()
9. Month()
10. Year()

In this section we will describe use of above functions in detail

1. GetDate() :
GetDate() will return current date and time

2. Convert() :
In many case situations arise in formation of a date like “DD/MM/YYYY”, “MM/DD/YYYY”.
Following table show standard date time format are often used in different scenario.

By Default Sql Server use YYYY-MM-DD Format for date type data. The above sql statements are displaying output with current date and time in given date format.

The date in these statements should be date type data.

3. ISDate() :

ISDATE () return 1 in case of valid date and time value otherwise it return 0. ISDate() could be use for date validation. In case of 1 we can write further business code otherwise we can print a message for date failure.

4. DATEPART()
DATEPART () function Return date part in a number from a specified date.
Part could be day/week/month/year/hour/minute/second from a date and time value.

5. DateAdd()

It Returns a date with the specified number interval (In Number) added to a given DATEPART (day/week/month/year/hour/minute/second) of that date.

In above sql statement I have increased day, month and year with 1 interval.

6. DateDiff():
Date Diff return the date time difference between two specified dates

7. DateName() :
Date name represent the character string of specified date of given date part

8. Day() :
Day function return day from the specified date.

9. Month() :
Month function return month from specified date

10. Year () :
Year function return year from specified date.


Hence we have learned to working with date in sql.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here