2015-04-03 48 views
1

如何使用case語句確定當前日期是星期六,星期日還是星期幾?我已經嘗試了以下所以任何指針我要去哪裏錯將不勝感激。謝謝。如何在sql case語句中使用datename

SELECT * FROM mytable WHERE day = (SELECT CASE aday 
             WHEN DATENAME(dw, GETDATE()) = 'Saturday' THEN 'Sat' 
             WHEN DATENAME(dw, GETDATE()) = 'Sunday' The 'Sun' 
             ELSE 'Weekday'); 
+0

什麼是'day'和'aday'你的表字段定義? – 2015-04-03 21:21:49

+0

'case aday'應該是'case'。而你錯過了'結束'。也沒有理由將其作爲子查詢編寫,儘管這是有效的。 – 2015-04-03 21:23:36

+0

'SELECT * FROM mytable WHERE day = CASE WHEN DATENAME(dw,GETDATE())='Saturday'THEN'Sat' 當DATENAME(dw,GETDATE())='星期天'然後'Sun' ELSE'Weekday 'end;' – 2015-04-03 21:25:55

回答

1
SELECT * FROM mytable WHERE day = (
    SELECT CASE DAYNAME(aday) 
     WHEN 'Saturday' THEN 'Sat' 
     WHEN 'Sunday' THEN 'Sun' 
     ELSE 'Weekday' END 
) 
;