简体   繁体   中英

SQL Output variable value

I have the following SQL statement which works. It finds when the room is available. The only issue I now have is I need to convert p.DayShort to a date value. DayShort will return, S,M,Tu,W,Th,F,Sa. I have a variable called week_beginning , which is the date for a Sunday, so let's assume a value of 11/8/2015, I want each row of data to return back a date from 11/8/2015, so if DayShort is W, I want it to return 11/11/2015 (basically + 3 to the week_beginning variable). Anyone show me how to do this please?

SELECT 
    p.DayShort, p.PeriodNumber, i.StartTime, i.EndTime 
FROM 
    PeriodList AS p
LEFT JOIN 
    PeriodInformation AS i ON p.pk_PeriodListID = i.fk_PeriodListID
LEFT JOIN 
    Lesson AS l ON l.fk_PeriodListID = i.fk_PeriodListID
LEFT JOIN 
    LessonRoom a ON l.pk_LessonID = a.fk_LessonID
LEFT JOIN 
    RoomList r ON a.fk_RoomID = r.pk_RoomID
WHERE 
    r.RoomCode = "SportsHall" 
    AND p.PeriodNumber NOT IN ('\"GAM/J\",\"ACT,J\",\"7E\",\"8E\",\"6E\",\"5E\"') 
    AND i.Rotation = "Sum"

How about this?

SELECT DATEADD('d', p.Offset, week_beginning) as thedate, p.DayShort, p.PeriodNumber, i.StartTime, i.EndTime 
FROM (
   SELECT PeriodList.*, 
      CASE 
        WHEN DayShort = 'M' THEN 1
        WHEN DayShort = 'Tu' THEN 2
        WHEN DayShort = 'W' THEN 3
        WHEN DayShort = 'Th' THEN 4
        WHEN DayShort = 'F' THEN 5
        WHEN DayShort = 'Sa' THEN 6
        ELSE 0
      END as Offset
   FROM PeriodList
) AS p
-- no change for the rest of the query

Note DATEADD here is how it is exists in SQL Server -- other flavors of SQL might be different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM