Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 37

Chapter 6

Single-Row Functions(2)

Objectives
Display

current date and time from

Access
Perform calculation with Dates
Using Dates and Conversion functions
Understand NULL values
Change the display of dates, number
and text

Current Date and Time


Default

format for Date in Access is


MM/D/YYYY
Functions to display system date and time:
Function Name Description
Date()

Provides current date

Time()

Provides current time

Now()

Provides current date


and time

Display Current Date / Time


SELECT Date() As [Today's Date],Time() As [Current Time] ,
Now() As [Current Date and Time]
From Test;
Output:
Today's Date
6/1/2005

Current Time
9:15:40 PM

Current Date and Time


6/1/2005 9:15:40 PM

Arithmetic with Dates


Add

or subtract a number to or from a date to


obtain a date value
Subtract two dates to find the number of days
between those dates

Using Arithmetic operator with


Dates
SELECT LastName, DateEnrolled , DateEnrolled+30 As [New Date]
from Student
Where LastName = 'Lee';
Output:

LastName
Lee

DateEnrolled
05-Jan-02

New Date
2/4/2002

Using Arithmetic operator with


Dates(2)
SELECT LastName, (Date() -DateEnrolled)/7 As [Week in School]
from Student
Where CourseID = 'DCS';
Output:
LastName

Week in School

Bartell

204.714285714286

Mikulski

122.714285714286

Tham

121.714285714286

Williams

161.857142857143

Dates Functions
Function Name

Description

Day(date)

Extracts the day of the month from


a date.

Month(date)

Extracts the month from a date.

Year(date)

Extracts the year from a date.

Weekday(date)

Extracts the day of the week from a


date.

DateAdd(interval, number, Add number of day/month/year to a


date)
date.
DateDiff(interval, date1,
date2)

Subtract number of day/month/year


to a between two dates.

Using Dates functions Year()


SELECT Year(DateEnrolled) AS [Year Enrolled] , Year(Date())Year(DateEnrolled) AS [No of years in schools]
FROM Student
where CourseID = 'DIT';

Output:
Year Enrolled

No of years in schools

2002

2001

2001

2000

Using Dates functions Month()


SELECT StudID, Month(DateEnrolled) AS [Month Enrolled] ,
Month(DateEnrolled)+6 AS [Sixth Month]
FROM Student
where CourseID = 'DIT';
Output:
StudID

Month Enrolled

Sixth Month

S003

S005

10

S011

10

S013

Using Dates functions Day()


SELECT StudID, DateEnrolled, Day(DateEnrolled) AS [Date]
FROM Student
where CourseID = 'DIT';
Output:
StudID

DateEnrolled

Date

S003

05-Jan-02

S005

01-Apr-01

S011

01-Apr-01

S013

30-Mar-00

30

Using Dates functions WeekDay()


SELECT StudID, DateEnrolled, WeekDay(DateEnrolled) AS [Day
of Week]
FROM Student
where CourseID = 'DIT';
Output:
StudID

DateEnrolled

Day of Week

S003

05-Jan-02

S005

01-Apr-01

S011

01-Apr-01

S013

30-Mar-00

Interval value

The interval value for both DateAdd and DateDiff functions is the
same.
The interval is the argument to instructs the function what kind of
setting you want to add/subtract to the date.
Setting

Description

Day

Month

yyyy

Year

weekday

ww

week

Hour

Minute

second

DateAdd Functions
Allow

to add number of day/month/year to


a date.
Depends on the interval setting.
Syntax:
DateAdd

(interval, number, date)

Using DateAdd Functions


SELECT StudID,DateEnrolled, DateAdd('m',6,DateEnrolled) As
[Add 6 months], DateAdd('w',5,DateEnrolled) as [Add 5 weekdays]
FROM Student
WHERE CourseID = 'DIT';
Output:
StudID

DateEnrolled

Add 6 months

Add 5 weekdays

S003

05-Jan-02

7/5/2002

1/10/2002

S005

01-Apr-01

10/1/2001

4/6/2001

S011

01-Apr-01

10/1/2001

4/6/2001

S013

30-Mar-00

9/30/2000

4/4/2000

DateDiff Functions
Allow

to subtract day/month/year between


two dates.
Depends on the interval setting.
Syntax:
DateDiff(interval,

date1, date2)
If date1 > date 2, result will be in negative
value.

Using DateDiff Functions


SELECT StudID,DateEnrolled, DateDiff('yyyy',DateEnrolled,Date())
As [Year]
FROM Student
WHERE CourseID = 'DIT';
Output:
StudID

DateEnrolled

Year

S003

05-Jan-02

S005

01-Apr-01

S011

01-Apr-01

S013

30-Mar-00

Using DateDiff Functions


SELECT DateDiff ('m',#01-JAN-2006#,#31-MAR-2006#) As
[Month Difference]
FROM TEST;
Output:
Month Difference
2

Conversion Function
Function Name

Description

CDate(expr)

Converts a number or character string to a date

CInt(expr)

Converts a character string to a number

CStr(expr)

Converts a number or date to a character string

Nz(expr, value)

Converts a Null value to another value

Convert a value from one data type to another.


The conversion function will not change its original
value but only to change its representation.

Using CDate Function


SELECT StudID, LastName, DateEnrolled
from Student
Where DateEnrolled > CDate('31-Dec-01');
Output:
StudID

LastName

DateEnrolled

S001

Bartell

15-Feb-02

S003

Lee

05-Jan-02

S006

Mikulski

12-Sep-03

S007

Tham

19-Sep-03

S009

Nicosia

01-Feb-02

S012

Maser

25-Sep-03

S014

Williams

12-Dec-02

S015

Chan

12-Dec-02

Using CInt Function


SELECT CInt(20) + CInt(10) As [Addition]
from Test;
Output:
Addition
30

Define NULL value


A null

value is a value that is unavailable,


unassigned, unknown or inapplicable.
It is not the same as zero or a space. Zero
is a number while space is a character.
Columns of any data type can be null
values, but it must not be the column that
defined as NOT NULL or as a PRIMARY
KEY.

Null values in Arithmetic


Expressions
Any

column in an arithmetic expression


contain null values, the result will also be
null.
SELECT Student.LastName, GPA*8 AS [Total Points]
FROM Student;
Output:

LastName

Total Points

Bartell

25.68

Kebel

21.68

Lee

30.56

null

Lewis
.

NZ function
NZ

function is to force a value where a null


would otherwise appear.
NZ can be used with date, character and
number data types.
Syntax for NZ function is as follow:

NZ (expression, value).
expression is the source of the data, column or
expression that may contain null
value is the target value for converting null.

Both

expression and the value to convert: their


data types must match.

Using NZ function
SELECT Student. LastName, NZ(GPA,0)*8 AS [Total Points]
FROM Student;
Output:
LastName

Total Points

Bartell

25.68

Kebel

21.68

Lee

30.56

Lewis

Law

24.4

..

..

Roche

15.04

Williams

21.92

Chan

24.96

Jann

Formatting in Access
You

can use the Format function to


customize the way numbers, dates, times,
and text are displayed and printed.
You can use one of the predefined formats
or you can create a custom format by
using formatting symbols.
The Format function uses different
settings for different data types.

Access Date Formats


Symbol
yyyy

Description
Full year (0100 to 9999).

yy
mmmm

Last two digits of the year (01 to 99).

mmm
mm

First three letters of the month (Jan to Dec).

dd
ddd

Day of the month in two numeric digits (01 to 31).

dddd

Full name of the weekday (Sunday to Saturday).

Full name of the month (January to December).


Month of the year in two numeric digits (01 to 12).
First three letters of the weekday (Sun to Sat).

Refer to Study guide for a complete Date format setting.

Example of Date Format


mm-dd-yyyy hh:nn:ss am/pm 12-31-2005 07:36:45 PM
mm-dd-yyyy hh:nn am/pm

12-31-2005 07:36 PM

dddd, mmmm dd, yyyy

Saturday, December 31, 2005

dd-mmm-yy

31-Dec-05

mm-dd-yyyy

12-31-2005

hh:nn:ss am/pm

07:36:45 PM

hh:nn am/pm

07:36 PM

hh:nn

19:36

Displaying Formatted Date


SELECT StudID, LastName, Format(DateEnrolled,'dddd, mmm dd yyyy')
FROM Student
WHERE DateEnrolled>CDate('31-Dec-01');
Output:
StudID

LastName

Expr1002

S001

Bartell

Friday, Feb 15 2002

S003

Lee

Saturday, Jan 05 2002

S006

Mikulski

Friday, Sep 12 2003

S007

Tham

Friday, Sep 19 2003

S009

Nicosia

Friday, Feb 01 2002

S012

Maser

Thursday, Sep 25 2003

S014

Williams

Thursday, Dec 12 2002

S015

Chan

Thursday, Dec 12 2002

Displaying Formatted Date


SELECT Format(Date(), ' dd mmmm " year of " yyyy " Time:"
hh:nn') AS [Today's Date]
FROM Test;
Output:
Today's Date
15 December year of 2005 Time: 12.00

Access Number Formats


Symbol

Description

. (period)

Decimal separator.

, (comma)

Thousand separator.

Digit placeholder. Display a digit or 0.

Digit placeholder. Display a digit or nothing.

Display the literal character "$".

Percentage. The value is multiplied by 100


and a percent sign is appended.

Example of Number format


Format(4234, ##,##0.00)
Format (2000, $#,###.00)
Format(8, 0.00%)

4,234.00
$2,000.00
800.00%

Displaying Formatted Number


SELECT Format(2555, '$##,###.00') As [Currency]
FROM Test;
Output:
Currency
$2,555.00

Access Text Formats


Symbol

Description

Text character (either a character or a space) is


required.

&

Text character is not required.

<

Force all characters to lowercase.

>

Force all characters to uppercase.

Displaying Formatted Text


SELECT Format(CourseID,'(@) - @@') AS [Course]
FROM Course;
Output:
Course
(D) - CS
(D) - GAT
(D) - IC
(D) - ICT
(D) - IT
(D) - NC

Displaying Formatted Text


SELECT LastName, Format(LastName,'>') AS [Name]
FROM Student
where CourseID = 'DCS';
Output:
LastName

Name

Bartell

BARTELL

Mikulski

MIKULSKI

Tham

THAM

Williams

WILLIAMS

Summary
Use date and conversion functions to:
Convert

column data types during calculations


and display
Perform calculations on data
Modify individual data items
Alter date formats for display

You might also like