Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Software ==> | SQL (203 exercises) |
Topic ==> | Scalar functions (9 exercises) |
Level ==> | Relatively easy |
Subject ==> | SQL training |
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
First (if you haven't already done so) run the script shown above to generate the Doctor Who training database.
This exercise uses the DateDiff function, which calculates the difference between two dates in days, months, years, etc, and takes 3 arguments:
Argument | What to put |
---|---|
Interval | The time period to use (use day for this exercise) |
Start date | The start date |
End date | The end date |
For example, the following would give 365 as the answer:
-- this would give 365 as the difference
SELECT DateDiff(day,'20150101','20160101')
Write a function called fnReign which uses the DateDiff function to return the number of days between any two given dates. Here's a quick reminder of scalar function syntax:
CREATE FUNCTION NameOfFunction(
@Parameter1 datatype,
...
@Parametern datatype,
)
RETURNS datatype
AS
BEGIN
RETURN something
END
Call your squeaky clean new function within a query to show the relative duration of each doctor's "reign":
If the end date is null for a doctor your function should substitute the current date. This may mean that Peter Capaldi appears higher up the list when you run your query than is shown here (since he is the only doctor for whom the last episode date is null).
Optionally, save your query as Baker wins.sql, then close it down.
You can find other training resources for the subject of this exercise here:
From: | kalaivanan |
When: | 04 Jul 17 at 07:20 |
One possible answer:
CREATE FUNCTION fnRegion(@FirstDate DATE,@LastDate DATE)
RETURNS INT
AS
BEGIN
DECLARE @Date_Diff INT
SELECT @Date_Diff=DateDIff(day,@FirstDate,ISNULL(@LastDate,Getdate()))
RETURN @Date_Diff
END
***************************************************************************************************************************
select DoctorName,dbo.fnRegion(FirstEpisodeDate,LastEpisodeDate)[Reign in days] from tblDoctor
****************************************************************************************************************************
From: | Andy B |
When: | 04 Jul 17 at 08:18 |
We don't normally post answers (apart from anything else, it removes the incentive to solve the exercise), but I'll make an exception for this!
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.