Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL:按最近的逗号分隔日期排序_Sql_Sql Server_Sql Server 2008 R2 - Fatal编程技术网

SQL:按最近的逗号分隔日期排序

SQL:按最近的逗号分隔日期排序,sql,sql-server,sql-server-2008-r2,Sql,Sql Server,Sql Server 2008 R2,我有一个以逗号分隔的日期表,如下所示 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/201

我有一个以逗号分隔的日期表,如下所示

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,    

以下是将字符串转换为日期表的自定义项:

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
子句进行选择和筛选,或者使用
orderby
子句进行排序,请提供您的数据样本的预期输出。同时请提供您迄今为止尝试过的内容。为什么,为什么,为什么,您要这样存储数据?