Date Functions

Date functions are available in SQL depending on the options with each specific implementation. Date functions, similar to character string functions, are used to manipulate the representation of date and time data. Available date functions are often used to format the output of dates and time in an appealing format, compare date values with one another, compute intervals between dates, and so on.

The Current Date

You may have already raised the question: How do I get the current date from the database? The need to retrieve the current date from the database may originate from several situations, but the current date is normally returned either to compare to a stored date or to return the value of the current date as some sort of timestamp.

The current date is ultimately stored on the host computer for the database, and is called the system date. The database, which interfaces with the appropriate operating system, has the capability to retrieve the system date for its own purpose or to resolve database requests, such as queries.

Take a look at a couple of methods of attaining the system date based on commands from two different implementations.

Sybase uses a function called GETDATE() to return the system date. This function is used in a query as follows. The output is what would return if today's current date was New Year's Eve for 1999.

							SELECT GETDATE()
						

Dec 31, 1999

Note

Most options discussed in this book for Sybase's and Microsoft's implementations are applicable to both implementations, because both use SQL Server for their database server. Both implementations also use an extension to standard SQL known as Transact-SQL.


Oracle uses what is calls a pseudocolumn, SYSDATE, to retrieve the current date. SYSDATE acts as any other column in a table and can be selected from any table in the database, although it is not actually part of the table's definition.

To return the system date in Oracle, the following statement returns the output if today was New Year's Eve before 2000:

							SELECT SYSDATE FROM TABLE_NAME
						

31-DEC-99

Time Zones

The use of time zones may be a factor when dealing with date and time information. For instance, a time of 6:00 p.m. in central United States does not equate to the same time in Australia, although the actual point in time is the same. Some of us who live within the daylight savings time zone are used to adjusting our clocks twice a year. If time zones are considerations when maintaining data in your case, you may find it necessary to consider time zones and perform time conversions, if available with your SQL implementation.

The following are some common time zones and their abbreviations:

AbbreviationDefinition
AST, ADTAtlantic standard, daylight time
BST, BDTBering standard, daylight time
CST, CDTCentral standard, daylight time
EST, EDTEastern standard, daylight time
GMTGreenwich mean time
HST, HDTAlaska/Hawaii standard, daylight time
MST, MDTMountain standard, daylight time
NSTNewfoundland standard, daylight time
PST, PDTPacific standard, daylight time
YST, YDTYukon standard, daylight time

Note

Some implementations have functions that allow you to deal with different time zones. However, not all implementations may support the use of time zones. Be sure to verify the use of time zones in your particular implementation, as well as the need in the case of your database.


Adding Time to Dates

Days, months, and other parts of time can be added to dates for the purpose of comparing dates to one another, or to provide more specific conditions in the WHERE clause of a query.

Intervals can be used to add periods of time to a DATETIME value. As defined by the standard, intervals are used to manipulate the value of a DATETIME value, as in the following examples:

							DATE '1999-12-31' + INTERVAL '1' DAY
						

'2000-01-01'

							DATE '1999-12-31' + INTERVAL '1' MONTH
						

'2000-01-31'

The following is an example using the SQL Server function DATEADD:

							SELECT DATEADD(MONTH, 1, DATE_HIRE)
							FROM EMPLOYEE_PAY_TBL
						

DATE_HIRE ADD_MONTH
---------- ----------
23-MAY-89 23-JUN-89
17-JUN-90 17-JUL-90
14-AUG-94 14-SEP-94
28-JUN-97 28-JUL-97
22-JUL-96 22-AUG-96
14-JAN-91 14-FEB-91

6 rows affected.

The following example uses the Oracle function ADD_MONTHS:

							SELECT DATE_HIRE, ADD_MONTHS(DATE_HIRE,1)
							FROM EMPLOYEE_PAY_TBL;
						

DATE_HIRE ADD_MONTH
----- ------
23-MAY-89 23-JUN-89
17-JUN-90 17-JUL-90
14-AUG-94 14-SEP-94
28-JUN-97 28-JUL-97
22-JUL-96 22-AUG-96
14-JAN-91 14-FEB-91

6 rows selected.

To add one day to a date in Oracle, use the following:

							SELECT DATE_HIRE, DATE_HIRE + 1
							FROM EMPLOYEE_PAY_TBL
							WHERE EMP_ID = '311549902';
						

DATE_HIRE DATE_HIRE
---------- ----------
23-MAY-89 24-MAY-89

1 row selected.

Notice that these examples in SQL Server and Oracle, though they differ syntactically from the ANSI examples, derive their results based on the same concept as described by the SQL standard.

Comparing Dates and Time Periods

OVERLAPS is a powerful standard SQL conditional operator for DATETIME values. The OVERLAPS operator is used to compare two timeframes and return the Boolean value TRUE or FALSE, depending on whether the two timeframes overlap. The following comparison returns the value TRUE:

(TIME '01:00:00' , TIME '05:59:00')
OVERLAPS
(TIME '05:00:00' , TIME '07:00:00')

The following comparison returns the value FALSE:

(TIME '01:00:00' , TIME '05:59:00')
OVERLAPS
(TIME '06:00:00 , TIME '07:00:00')

Miscellaneous Date Functions

The following list shows some powerful date functions that exist in the implementations for SQL Server and Oracle.

SQL SERVER 
DATEPARTReturns the integer value of a DATEPARTfor a date
DATENAMEReturns the text value of a DATEPARTfor a date
GETDATE()Returns the system date
DATEDIFFReturns the difference between two dates for specified date parts, such as days, minutes, and seconds

ORACLE 
NEXT_DAYReturns the next day of the week as specified (for example, FRIDAY) since a given date
MONTHS_BETWEENReturns the number of months between two given dates

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset