Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views5 pages

Dbms Experiment 06

This document outlines SQL queries using various conversion, string, and date functions. It provides examples for functions such as TO_CHAR, TO_NUMBER, TO_DATE, string manipulation functions like CONCATENATION, and date functions including SYSDATE and ADD_MONTHS. Each function is demonstrated with a SQL query and its corresponding result.

Uploaded by

aimlbtech7
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)
27 views5 pages

Dbms Experiment 06

This document outlines SQL queries using various conversion, string, and date functions. It provides examples for functions such as TO_CHAR, TO_NUMBER, TO_DATE, string manipulation functions like CONCATENATION, and date functions including SYSDATE and ADD_MONTHS. Each function is demonstrated with a SQL query and its corresponding result.

Uploaded by

aimlbtech7
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/ 5

EXPERIMENT 06:

SQL Queries using Conversion functions (to_char, to_number and to_date), string
functions (Concatenation, lpad, rpad, ltrim, rtrim, lower, upper, initcap, length, substr
and instr), date functions (Sysdate, next_day, add_months, last_day, months_between,
least, greatest, trunc, round, to_char, to_date)

1. Conversion Functions

a) TO_CHAR (Converts a date or number to a string)

SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD') AS formatted_date FROM dual;

Result:

FORMATTED_DATE

-------------------

2025-01-21

b) TO_NUMBER (Converts a string to a number)

SELECT TO_NUMBER('1234.56') AS numeric_value FROM dual;

Result:

NUMERIC_VALUE

-----------------

1234.56

c) TO_DATE (Converts a string to a date)

SELECT TO_DATE('2025-01-21', 'YYYY-MM-DD') AS date_value FROM dual;

Result:

DATE_VALUE

---------------

2025-01-21

2. String Functions

a) Concatenation (||)

SELECT 'Hello' || ' ' || 'World' AS greeting FROM dual;

Result:

GREETING

--------------

Hello World

b) LPAD (Left Padding)


SELECT LPAD('123', 5, '0') AS padded_value FROM dual;

Result:

PADDED_VALUE

---------------

00123

c) RPAD (Right Padding)

SELECT RPAD('123', 5, '0') AS padded_value FROM dual;

Result:

PADDED_VALUE

---------------

12300

d) LTRIM (Trim Leading Spaces)

SELECT LTRIM(' Hello') AS trimmed_value FROM dual;

Result:

TRIMMED_VALUE

----------------

Hello

e) RTRIM (Trim Trailing Spaces)

SELECT RTRIM('Hello ') AS trimmed_value FROM dual;

Result:

TRIMMED_VALUE

----------------

Hello

f) LOWER (Convert to Lowercase)

SELECT LOWER('HELLO WORLD') AS lower_case FROM dual;

Result:

LOWER_CASE

--------------

hello world

g) UPPER (Convert to Uppercase)

SELECT UPPER('hello world') AS upper_case FROM dual;

Result:

UPPER_CASE
--------------

HELLO WORLD

h) INITCAP (Capitalize First Letter)

SELECT INITCAP('hello world') AS capitalized_string FROM dual;

Result:

CAPITALIZED_STRING

------------------------

Hello World

i) LENGTH (Length of String)

SELECT LENGTH('Hello World') AS string_length FROM dual;

Result:

STRING_LENGTH

-----------------

12

j) SUBSTR (Extract Substring)

SELECT SUBSTR('Hello World', 1, 5) AS substring FROM dual;

Result:

SUBSTRING

-----------

Hello

k) INSTR (Position of Substring)

SELECT INSTR('Hello World', 'World') AS position FROM dual;

Result:

POSITION

------------

3. Date Functions

a) SYSDATE (Current Date and Time)

SELECT SYSDATE FROM dual;

Result:

SYSDATE

---------------

2025-01-21 12:34:56
b) NEXT_DAY (Next specified day)

SELECT NEXT_DAY(SYSDATE, 'SUNDAY') AS next_sunday FROM dual;

Result:

NEXT_SUNDAY

--------------------

2025-01-25

c) ADD_MONTHS (Add months to a date)

SELECT ADD_MONTHS(SYSDATE, 3) AS new_date FROM dual;

Result:

NEW_DATE

-------------------

2025-04-21

d) LAST_DAY (Last day of the month)

SELECT LAST_DAY(SYSDATE) AS last_day_of_month FROM dual;

Result:

LAST_DAY_OF_MONTH

-----------------------

2025-01-31

e) MONTHS_BETWEEN (Months between two dates)

SELECT MONTHS_BETWEEN(SYSDATE, TO_DATE('2024-01-21', 'YYYY-MM-DD')) AS


months_diff FROM dual;

Result:

MONTHS_DIFF

---------------

12

f) LEAST (Least of two or more expressions)

SELECT LEAST(5, 10, 3, 7) AS least_value FROM dual;

Result:

LEAST_VALUE

--------------

g) GREATEST (Greatest of two or more expressions)

SELECT GREATEST(5, 10, 3, 7) AS greatest_value FROM dual;


Result:

GREATEST_VALUE

------------------

10

h) TRUNC (Truncate Date to specified unit)

SELECT TRUNC(SYSDATE, 'MONTH') AS start_of_month FROM dual;

Result:

START_OF_MONTH

-------------------

2025-01-01

i) ROUND (Round Date to specified unit)

SELECT ROUND(SYSDATE, 'MONTH') AS rounded_date FROM dual;

Result:

ROUNDED_DATE

-------------------

2025-01-01

You might also like