我有一个表格,日期以逗号分隔,如下所示
9/30/2013,9/17/2013,10/16/2013,
5/27/2013, 9/30/2013,
09/24/2013,09/27/2013,
09/13/2013,11/18/2013,
09/11/2013,09/13/2013,
12/27/2013,12/20/2013,
12/27/2013,12/20/2013,12/13/2013,12/6/2013,11/29/2013,
10/3/2013,10/10/2013,10/17/2013,10/24/2013,10/31/2013,
10/31/2013,10/24/2013,10/17/2013,10/10/2013,10/3/2013,
现在我的任务是对最近的第一个记录进行排序,过去的日期可以忽略。需要将所有日期与当前日期进行比较,请建议我如何实现此目标
提前致谢。
已编辑
我除外:
9/30/2013,9/17/2013,10/16/2013, as 10/16/2013 is nearest of all
10/3/2013,10/10/2013,10/17/2013,10/24/2013,10/31/2013, as 10/17/2013 next nearest
10/31/2013,10/24/2013,10/17/2013,10/10/2013,10/3/2013,
12/27/2013,12/20/2013,12/13/2013,12/6/2013,11/29/2013,
12/27/2013,12/20/2013,
答案 0 :(得分:0)
这是一个将字符串转换为日期表的udf:
CREATE FUNCTION [dbo].[udf_ConvertCSVStringToDateTable]
(
@CSVString varchar(max)
)
RETURNS
@DateTable TABLE
(
[Date] DATETIME
)
AS
BEGIN
DECLARE @ListSize BIGINT
DECLARE @FoundIndex BIGINT
DECLARE @Date INT
DECLARE @DateString VARCHAR(10)
SET @ListSize = LEN(@CSVString)
WHILE(@ListSize > 0)
BEGIN
SET @FoundIndex = CHARINDEX(',', @CSVString)
IF(@FoundIndex = 0)
BEGIN
SET @DateString = @CSVString
SET @CSVString = ''
END
ELSE
BEGIN
SET @DateString = SUBSTRING(@CSVString, 1, @FoundIndex - 1)
SET @CSVString = SUBSTRING(@CSVString, @FoundIndex + 1, @ListSize)
END
IF(ISDATE(@DateString) = 1)
BEGIN
INSERT @DateTable([Date]) VALUES (CONVERT(DATETIME, @DateString))
END
SET @ListSize = LEN(@CSVString)
END
RETURN
END
从那以后你应该可以使用WHERE
子句进行选择和过滤,或者使用ORDER BY
子句进行排序