3

我正在使用 SQL Server 2008,我有时间列 ( varchar),时间间隔为 10 分钟。我想以半小时为单位显示时间..

表 ( timeslot) 包含以下数据:

time
-----
11:10 AM
11:20 AM
11:30 AM
11:40 AM
11:50 AM
12:00 PM
12:10 PM
12:20 PM
12:30 PM
12:40 PM
12:50 PM
01:00 PM
01:10 PM
01:20 PM

我想显示这个输出:

time
-----
11:00 AM
11:30 AM
12:00 PM
12:30 PM
01:00 PM
01:30 PM

有人可以帮我吗?提前致谢...

4

2 回答 2

0
WITH CTE 
AS (SELECT DISTINCT columnname FROM table_name
WHERE DATEPART(MINUTE, CAST(column as TIME)) IN (0, 30) )
SELECT columnname
FROM CTE
ORDER BY CAST(columnname as time)
于 2012-10-12T07:51:22.800 回答
0

尝试这样的事情:

SELECT 
    (list of columns)
FROM 
    dbo.YourTableWithTheTime
WHERE 
    DATEPART(MINUTE, CAST(YourTimeColumnHere as TIME)) IN (0, 30)

当然 - 如果您首先为您的列使用了适当的数据类型,您可以节省自己的构造,事情会更容易......TIMECAST(.. as TIME)

更新:如果您需要按时订购 - 使用此:

SELECT 
    DISTINCT [time]
FROM 
    dbo.YourTableWithTheTime
WHERE 
    DATEPART(MINUTE, CAST([time] as TIME)) IN (0, 30)
ORDER BY
    CAST([time] as TIME)

PS:命名列也是一个非常糟糕的主意time- 因为这是 SQL Server 2008 中的保留关键字(用于数据类型).....

于 2012-10-12T05:29:10.420 回答