Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

string and Date functions in sql

Uploaded by

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

string and Date functions in sql

Uploaded by

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

Scenario Series 12 -- 𝐑𝐞𝐚𝐥-𝐖𝐨𝐫𝐥𝐝 𝐀𝐩𝐩𝐥𝐢𝐜𝐚𝐭𝐢𝐨𝐧𝐬 𝐨𝐟 𝐒𝐭𝐫𝐢𝐧𝐠

𝐚𝐧𝐝 𝐃𝐚𝐭𝐞 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬

-- Create the Employee table


CREATE TABLE Employee (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Position VARCHAR(50),
Department VARCHAR(50),
HireDate DATE,
Salary DECIMAL(10, 2),
Email VARCHAR(100)
);

-- Insert data into the Employee table


INSERT INTO Employee (EmployeeID, FirstName, LastName, Position, Department, HireDate,
Salary, Email)
VALUES
(1, 'John', 'Doe', 'Manager', 'Sales', '2018-01-15', 75000, 'john.doe@example.com'),
(2, 'Jane', 'Smith', 'Developer', 'IT', '2019-03-22', 85000,
'jane.smith@example.com'),
(3, 'Alice', 'Brown', 'Analyst', 'Finance', '2020-07-19', 65000,
'alice.brown@example.com'),
(4, 'Bob', 'Davis', 'Consultant', 'HR', '2021-05-10', 70000, 'bob.davis@example.com'),
(5, 'Eve', 'White', 'Assistant', 'Admin', '2017-12-05', 45000,
'eve.white@example.com');

1. 👥 𝐅𝐮𝐥𝐥 𝐍𝐚𝐦𝐞 𝐃𝐢𝐬𝐩𝐥𝐚𝐲: How can you display the full name of
each employee by combining their first and last names?

Follow me on LinkedIn – Shivakiran kotur


2. 𝐒𝐮𝐛𝐬𝐭𝐫𝐢𝐧𝐠 𝐄𝐱𝐭𝐫𝐚𝐜𝐭𝐢𝐨𝐧:
How would you extract the first three characters of an
employee's first name?

How could you extract the last three characters of a job title?

How can you pull a department name starting from the second
character?

Follow me on LinkedIn – Shivakiran kotur


3. 𝐍𝐚𝐦𝐞 𝐋𝐞𝐧𝐠𝐭𝐡 𝐂𝐡𝐞𝐜𝐤: How would you find the length of each
employee's first name to ensure it meets specific criteria?

4. 𝐂𝐚𝐬𝐞 𝐂𝐨𝐧𝐯𝐞𝐫𝐬𝐢𝐨𝐧:
How can you convert first names to uppercase for consistency?
How would you convert last names to lowercase, perhaps for
generating email addresses?

5. 𝐓𝐞𝐱𝐭 𝐑𝐞𝐩𝐥𝐚𝐜𝐞𝐦𝐞𝐧𝐭:
How would you update all occurrences of the word "Manager"
to "Lead" in job titles?
How could you replace "IT" with "Technology" in department
names?
How would you replace spaces with hyphens in full names for
URL-friendly formatting?

Follow me on LinkedIn – Shivakiran kotur


6. 𝐄𝐦𝐚𝐢𝐥 𝐌𝐚𝐬𝐤𝐢𝐧𝐠: What’s the best way to mask an email address
so that only the first three characters and the domain are visible,
keeping the rest private?

STUFF(Email, 4, CHARINDEX('@', Email) - 4, REPLICATE('*',


CHARINDEX('@', Email) - 4)):
• Email: The original email address.
• 4: The starting position where masking begins (the
4th character).
• CHARINDEX('@', Email) - 4: The length of the string
to be replaced, which is the number of characters
between the 3rd position and the @ symbol.
• REPLICATE('*', CHARINDEX('@', Email) - 4): The
replacement string, which is a series of asterisks
(*) to mask the characters.

Follow me on LinkedIn – Shivakiran kotur


𝐃𝐚𝐭𝐞 𝐌𝐚𝐧𝐚𝐠𝐞𝐦𝐞𝐧𝐭:
How would you extract just the date from a hire date field?

How can you break down the hire date into year, month, and
day components?

What’s the best way to calculate the number of days an


employee has been with the company?

Follow me on LinkedIn – Shivakiran kotur


How would you add a year to the hire date for tracking
anniversaries?

Find Employees Hired in a Specific Month (e.g., January)

Determine the Weekday of the Hire Date

Calculate the Number of Weeks Since the Hire Date

Follow me on LinkedIn – Shivakiran kotur


Find the Last Working Day of the Current Month

Follow me on LinkedIn – Shivakiran kotur

You might also like