将选择结果转换为SQL Server中的列名

时间:2013-05-26 03:47:09

标签: sql sql-server tsql

我有一张表@MyTemp,如下所示:

|Name |Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday |
-----------------------------------------------------------------
|Test |18:30 |15:40  |15:40    |17:30   |18:30 |08:30   |09:30  |

我知道我可以使用

在SQL Server中度过一天
DECLARE @Today NVARCHAR(50) = (SELECT datename(dw,getdate()))

如何在我的选择

中获得一天的结果
SELECT  @Today From @MyTemp  ???

1 个答案:

答案 0 :(得分:2)

怎么样

SELECT 
  CASE datename(dw,getdate())
    WHEN 'Monday'    THEN Monday
    WHEN 'Tuesday'   THEN Tuesday
    WHEN 'Wednesday' THEN Wednesday
    WHEN 'Thursday'  THEN Thursday
    WHEN 'Friday'    THEN Friday
    WHEN 'Saturday'  THEN Saturday
    WHEN 'Sunday'    THEN Sunday
  END today
  FROM @MyTemp
 WHERE Name = 'Test'

示例输出:

| TODAY |
---------
| 09:30 |

<强> SQLFiddle