0% found this document useful (0 votes)
5 views4 pages

Dhruv RDBMS PRACTICAL 4

The document outlines a practical exercise focused on implementing SQL queries using various date functions in Oracle, including TRUNC(), ADD_MONTHS, and GREATEST. It provides syntax, examples, and explanations for using the TRUNC() function to manipulate date values. The conclusion emphasizes the successful execution of SQL queries utilizing these date functions.

Uploaded by

dhruv1200f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Dhruv RDBMS PRACTICAL 4

The document outlines a practical exercise focused on implementing SQL queries using various date functions in Oracle, including TRUNC(), ADD_MONTHS, and GREATEST. It provides syntax, examples, and explanations for using the TRUNC() function to manipulate date values. The conclusion emphasizes the successful execution of SQL queries utilizing these date functions.

Uploaded by

dhruv1200f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

RDBMS PRACTICAL:-04 ENROLLMENT NUMBER:-236010307124

PRACTICAL NO: 04
AIM: Implement SQL queries using Date functions like add months,
months-between, round, nextday, truncate, greatest, newtime etc
[ 02 – Hours ]

TRUNCATE

The following shows the syntax of the Oracle TRUNC() function:

Code language: SQL (Structured Query Language) (sql)

Arguments

The TRUNC() function accepts two arguments:

1) date
The date argument is a DATE value or an expression that evaluates
to a DATE value that will be truncated.

A.Y. DADABHAI TECHNICAL INSTITUTE ,KOSAMBA, SURAT Page 1


RDBMS PRACTICAL:-04 ENROLLMENT NUMBER:-236010307124

2) format
The format argument determines the unit to which the date will be truncated.

The format argument is optional. Its default value is DD that instructs


the TRUNC() function to truncate the date to midnight.

EXAMPLES

Let’s look at some examples of using the Oracle TRUNC() function.

A) TRUNCATE A DATE VALUE USING DEFAULT FORMAT

Consider the following date time value:

17-OCT-2023 11:01:40

SQL (Structured Query Language)

The following statement truncates the date value to midnight:

SELECT TO_CHAR(

TRUNC(TO_DATE( '17-OCT-2023 11:01:40 ', 'DD-Mon-YYYY HH24:MI:SS'

)), 'DD-Mon-YYYY HH24:MI:SS'

result

FROM

STUD7124;

Code language: SQL (Structured Query Language) (sql)

OUTPUT:

First, the TO_DATE() function converted a date string to a DATE value.

Second, the TRUNC() function truncated the date. Because we did not pass
the format argument, the TRUNC() function uses the default value that
truncates the date to midnight.

A.Y. DADABHAI TECHNICAL INSTITUTE ,KOSAMBA, SURAT Page 2


RDBMS PRACTICAL:-04 ENROLLMENT NUMBER:-236010307124

Third, the TO_CHAR() function formatted the result of the TRUNC() function.

B) GET THE FIRST DAY OF THE MONTH OF A DATE

The following statement returns the first day of the current month.

SELECT

TRUNC( SYSDATE, 'MM' ) result

FROM

STUD7124;

Code language: SQL (Structured Query Language) (sql) Output:

If you want to return the first day of the month of a specific date, you just
need to use that date instead of the SYSDATE

C) GET THE FIRST DAY OF THE QUARTER OF A DATE

SELECT TRUNC( SYSDATE, 'Q' ) result

FROM STUD7124;

Code language: SQL (Structured Query Language) (sql)

Output:

In this example, we replaced the month ( MM) by quarter ( Q).

SQL GREATEST DATE FUCATION

SELECT MAX (dob) AS "MAX DATE" FROM STUD237124;

A.Y. DADABHAI TECHNICAL INSTITUTE ,KOSAMBA, SURAT Page 3


RDBMS PRACTICAL:-04 ENROLLMENT NUMBER:-236010307124

CONCLUSION:- HENCE WE HAVE PERFERMED SQL QUERIS IMPLEMENT SQL QUERIES


USING DATE FUNCTIONS LIKE ADD MONTHS, MONTHS-BETWEEN, ROUND, NEXTDAY,
TRUNCATE, GREATEST, NEWTIME ETC.,

A.Y. DADABHAI TECHNICAL INSTITUTE ,KOSAMBA, SURAT Page 4

You might also like