100% found this document useful (1 vote)
132 views5 pages

SQL Assignment 3

This document provides instructions for an MS SQL Server assignment involving stored procedures and string/date functions. It includes 12 questions/tasks related to creating stored procedures to display data from tables based on certain conditions, using string functions like LEN, LTRIM, RTRIM, CHARINDEX, SUBSTRING, STUFF, and date functions like DATENAME, MONTH, YEAR. Sample answers/queries are provided for each question.

Uploaded by

Nikhil Rai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (1 vote)
132 views5 pages

SQL Assignment 3

This document provides instructions for an MS SQL Server assignment involving stored procedures and string/date functions. It includes 12 questions/tasks related to creating stored procedures to display data from tables based on certain conditions, using string functions like LEN, LTRIM, RTRIM, CHARINDEX, SUBSTRING, STUFF, and date functions like DATENAME, MONTH, YEAR. Sample answers/queries are provided for each question.

Uploaded by

Nikhil Rai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

MS SQL Server

Assignment # 3

Topic: Stored Procedures, string functions, date functions Time: 2 Hr

SQL Assignment # 3 – Questions

1. Create a stored procedure to Display the number of Employee by each Job from Employees
table.
Ans - create procedure acroschema_17.emp
as begin
select JOB,count(JOB) as EMPNO FROM acroschema_17.EMPLOYEES group by JOB
end

exec acroschema_17.EMPLOYEES

2. Create a stored procedure to Display client name whose project’s ‘Coding’ task and status ‘In
Progress’.
Ans - create procedure acroschema_17.A1
as begin
select t1.CNAME from acroschema_17.CLIENTS t1 inner join
acroschema_17.PROJECTS t2 ON t2.CLIENT_ID = t1.CLIENT_ID
inner join acroschema_17.EMPPROJECTTASKS t3 on t3.PROJECT_ID=t2.PROJECT_ID
and t3.TASK='Coding' and t3.STATUS = 'In Progress'
end
exec acroschema_17.A1

3. Create a stored procedure to Display names of employees doing ‘System Analysis’ along with project
name
Ans -
create procedure acroschema_17.A2
as begin
select t1.ENAME,t3.DESCR from acroschema_17.EMPLOYEES t1 inner join
acroschema_17.EMPPROJECTTASKS t2 ON t2.EMPNO = t1.EMPNO
inner join acroschema_17.PROJECTS t3 on t3.PROJECT_ID=t2.PROJECT_ID
and t2.TASK='System Analysis' group by t1.ENAME,t3.DESCR
end
exec acroschema_17.A2

4. Display the Ename and count of Ename characters from the Employee table whose Ename
characters count greater than 4.
Ans - select ENAME,LEN(ENAME) AS char from acroschema_17.EMPLOYEES where
LEN(ENAME) > 4 group by ENAME

5. Display the Cname with remove blank on the left hand side & remove blank on the right hand
side from the Clients table.
Ans -
select LTRIM(RTRIM(CNAME)) as CLIENTS from acroschema_17.CLIENTS

6. Write a query to Display the below result set from Clients table.
Ans - select SUBSTRING(EMAIL,CHARINDEX('@',EMAIL)+1,LEN(EMAIL)-
CHARINDEX('@',EMAIL))
AS EMAILDOMAIN FROM acroschema_17.CLIENTS
EmailDomain
trackon.com
acmeutil.com
lawful.com
moneysaver.com

7. Write a query to Display the below result set from Clients table.
Ans -
select EMAIL,SUBSTRING(EMAIL,1,CHARINDEX('@',EMAIL)-1) AS
NAME ,SUBSTRING(EMAIL,CHARINDEX('@',EMAIL)+1,LEN(EMAIL)-CHARINDEX('@',EMAIL))
AS EMAILDOMAIN FROM acroschema_17.CLIENTS group by EMAIL

Email Name EmailDomain


consult@trackon.com consult trackon.com
contact@acmeutil.com contact acmeutil.com
justice@lawful.com justice lawful.com
save@moneysaver.com save moneysaver.com

8. Write a query to swapping of two strings without using third variable.


9. Write a query to Display the below result set from Clients table.
Ans - select CLIENT_ID, REPLICATE(CNAME,3) AS repeatName from acroschema_17.CLIENTS

CLIENT_I
D RepeatName
1001 ACME UtilitiesACME UtilitiesACME Utilities
1002 Trackon ConsultantsTrackon ConsultantsTrackon Consultants
1003 MoneySaver DistributorsMoneySaver DistributorsMoneySaver Distributors
1004 Lawful CorpLawful CorpLawful Corp

10. Write a query to Display the below result set from Clients table.
Ans -
select EMAIL,STUFF(EMAIL,2,CHARINDEX('@',EMAIL),'*****')
FROM acroschema_17.CLIENTS
group by EMAIL

Email StuffedEmail
consult@trackon.com c*****rackon.com
contact@acmeutil.com c*****cmeutil.com
justice@lawful.com j*****awful.com
save@moneysaver.com s*****oneysaver.com

11. Write a query to Display the below result set from PROJECTS table.
Ans -
select PROJECT_ID ,DESCR,START_DATE,DATENAME(WEEKDAY,START_DATE) AS START_DATE_DAY,
MONTH(START_DATE) AS START_DATE_MONTH_NUMBER,
DATENAME(MONTH,START_DATE) AS START_DATE_MONTH_NAME,
YEAR(START_DATE) AS START_DATE_YEAR FROM acroschema_17.PROJECTS

PROJECT_I
D DESCR START_DATE START_DATE_Day START_DATE_MonthNumber START_DATE_MonthName START_DATE_Year
401 Inventory 01-04-2011 Friday 4 April 2011
402 Accounting 01-08-2011 Monday 8 August 2011
403 Payroll 01-10-2011 Saturday 10 October 2011
Contact
404 Mgmt 01-11-2011 Tuesday 11 November 2011

12. Write a query to calculate number of A in string 'VIKASAAA'?


Ans -

MS SQL Server
Assignment # 3

Topic: Submitted By:Nikhil

You might also like