How about...
USE Sandbox;
GO
/*
All odd alignment issues courtesy of SSC's text editor :)
*/
CREATE TABLE SampleTable (ID int IDENTITY(1,1),
StartDay varchar(9),
EndDay varchar(9));
INSERT INTO SampleTable
VALUES ('Monday','Friday'),
('Tuesday','Wednesday'),
('Friday','Monday');
GO
CREATE FUNCTION DayNumber_fn (@DayName varchar(9))
RETURNS TABLE
AS RETURN
SELECT CASE @DayName WHEN 'Sunday' THEN 0
WHEN 'Monday' THEN 1
WHEN 'Tuesday' THEN 2
WHEN 'Wednesday' THEN 3
WHEN 'Thursday' THEN 4
WHEN 'Friday' THEN 5
WHEN 'Saturday' THEN 6 END AS DayNum;
GO
SELECT ID,
(7 + (ED.DayNum - SD.DayNum) ) % 7 AS [Days]
FROM SampleTable ST
CROSS APPLY dbo.DayNumber_fn(ST.StartDay) SD
CROSS APPLY dbo.DayNumber_fn(ST.EndDay) ED;
GO
DROP TABLE SampleTable;
DROP FUNCTION DayNumber_fn;
I've used a Inline table Function here, however, you could move the CASE into the main query and use a CTE. A The use of the function just makes the final query a little more little more succinct.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk