如何从可能包含SQL日期的字符串中获取int?

时间:2016-05-10 16:39:30

标签: sql-server regex validation date

我必须从第三方工具中获取一个字符串,该工具包含一个整数,剥离int,并使用它来连接本地表。我使用的Pinal Dave's udf_GetNumeric function适用于I worked on ticket no. 123456 for a long time,但现在用户输入的内容为5/9/2016 ticket 123456 was closed as duplicate,返回5092016123456。我可以将搜索模式修改为仅查看5个或更多个连续数字,因为我们的ID列是> 9999,但是sql不理解([0-9]{5,})。不幸的是,我无法安装CLR来使用完整的正则表达式。

2 个答案:

答案 0 :(得分:0)

  

我可以将搜索模式修改为仅查看5个或更多   自我们的ID列以来的连续数字是> 9999,但sql没有   理解([0-9] {5,})。我无法安装CLR

您可以修改搜索模式以使用%[0-9][0-9][0-9][0-9][0-9]%。这将找到5个连续整数的字符串。例如如下所示。

WITH T(Notes)
     AS (SELECT 'I worked on ticket no 123456.  for a long time ')
SELECT *,
       SUBSTRING(Notes, PatIdx, Length)
FROM   T
       CROSS APPLY (SELECT PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', Notes)) CA(PatIdx)
       CROSS APPLY (SELECT PATINDEX('%[^0-9]%', SUBSTRING(Notes + ' ', PatIdx, 8000)) - 1) CA2(Length) 

答案 1 :(得分:0)

使用JamesZ的建议拆分空间,我改变了我在别处使用的旧功能。

CREATE FUNCTION [dbo].[tvf_CreateVarcharTableFromCsv]
    (
      @VarcharCsv VARCHAR(4000) ,
      @Seperator VARCHAR(5) = NULL
    )
RETURNS @VarcharTable TABLE
    (
      VarcharIndex VARCHAR(50) NOT NULL
    )
AS
    BEGIN
        DECLARE @localVarcharIndexList VARCHAR(4000);  
        DECLARE @localSeperator VARCHAR(5);  
        SET @localVarcharIndexList = @VarcharCsv;
        SET @localSeperator = @Seperator;
        IF @localSeperator IS NULL
            SET @localSeperator = ',';

        /*****************************************************************************************
        Parse CSVs into tables with CTE
        http://www.simple-talk.com/sql/t-sql-programming/sql-server-2005-common-table-expressions/
        *****************************************************************************************/
        IF @localVarcharIndexList IS NOT NULL
            BEGIN
                WITH    VarcharCsv ( i, j, VarcharIndex )
                          AS ( SELECT   i = 1 ,
                                        VarcharIndex = CHARINDEX(@localSeperator,
                                                              @localVarcharIndexList
                                                              + @localSeperator) ,
                                        SUBSTRING(@localVarcharIndexList, 1,
                                                  CHARINDEX(@localSeperator,
                                                            @localVarcharIndexList
                                                            + @localSeperator)
                                                  - 1)
                               UNION ALL
                               SELECT   i = j + 1 ,
                                        j = CHARINDEX(@localSeperator,
                                                      @localVarcharIndexList
                                                      + @localSeperator, j + 1) ,
                                        SUBSTRING(@localVarcharIndexList,
                                                  j + 1,
                                                  CHARINDEX(@localSeperator,
                                                            @localVarcharIndexList
                                                            + @localSeperator,
                                                            j + 1) - ( j + 1 ))
                               FROM     VarcharCsv
                               WHERE    CHARINDEX(@localSeperator,
                                                  @localVarcharIndexList
                                                  + @localSeperator, j + 1) <> 0
                             )
                    INSERT  INTO @VarcharTable
                            SELECT  VarcharIndex
                            FROM    VarcharCsv
                    OPTION  ( MAXRECURSION 500 );
            END;
        RETURN; 
    END;

它可以按照我的要求使用:

SELECT  *
FROM    [tvf_CreateVarcharTableFromCsv]('here is a string 123456 blah blah blah 5/09/2016"',
                                        ' ')
WHERE   LEN(VarcharIndex) > 5
        AND ISNUMERIC(VarcharIndex) = 1