2012-09-21 2 views
2

스크린 샷은 모든 것을 설명합니다. 현재 구성에 http://i46.tinypic.com/f3hobl.png영숫자 필드에서 6-8 자의 날짜를 추출합니다.

, InvoiceSentDate은 8 자리 날짜 (MM-DD-YY)을 수용한다. MM-DD-YYYY 날짜도 캡처 할 수 있기를 원합니다. 어떻게해야합니까? 비교를 위해

는 일을 복잡하게, 2116

또한

대 송장 2106-2112 봐! 일부 레코드에는 날짜 이후에 텍스트가 있습니다. http://i50.tinypic.com/2r5qa88.png

+0

즉의 작업이 될 것이다 SSIS, 어떻게하는지 압니다. 지금 당장은 날짜를 깨끗하게 잡아낼 SQL 문을 작성하려고합니다. – Kyle

+1

그것은 TSQL이 직접 할 일을 추진할 것입니다. CLR 및 Regex가 필요할 수 있습니다. – Paparazzi

+1

나는 또한 CLR과 Regex를 제안하려고했다. http://msdn.microsoft.com/en-us/magazine/cc163473.aspx#S2 – CandiedCode

답변

4

을 탈출했다으로 아래 코드는 다른 보인다. 다음은 작동하는 SqlFiddle입니다.

여기에서 patindex으로 날짜를 찾고 그 이후에 첫 번째 숫자가 아닌 숫자를 찾습니다. 그러면 substring에 필요한 매개 변수만으로 날짜를 가져올 수 있습니다. 보시다시피, 나는 슬래시 (slash)와 대시 (dash) 날짜 분리자를 포함하여 다양한 가능성을 다루는 테스트 데이터를 추가했다.

-- Test data 
declare @Demo table (
    RawData varchar(100) null 
) 
insert into @Demo select 'JS sent via Unifier on 08/29/2012' 
insert into @Demo select 'i sent via email on 09/07/12' 
insert into @Demo select 'i sent via Unifier on 01/04/12; resubmitting p...' 
insert into @Demo select 'JS sent via Unifier on 08-29-2012; resubmitting p...' 
insert into @Demo select '08-29-2012; resubmitting p...' 
insert into @Demo select '08-29-12' 
insert into @Demo select 'no date here' 
insert into @Demo select null 

-- Actual query 
select *, 
    -- If there's a date, display it 
    case when StartChar > 0 then substring(RawData, StartChar, DateLen) else null end as DateString 
from (
    select *, 
     -- Find the first date 
     patindex('%[0-1][0-9][/-][0-3][0-9][/-][0-9][0-9]%', RawData) as StartChar, 
     -- Find the first non-digit after that date 
     patindex(
      '%[^0-9]%', 
      right(
       RawData + '_', -- This underscore adds at least one non-digit to find 
       len(RawData) - patindex('%[0-1][0-9][/-][0-3][0-9][/-][0-9][0-9]%', RawData) - 6 
      ) 
     ) + 7 as DateLen 
    from @Demo 
) as a 

방금이 가능한 날짜 형식을 찾고 있다면 당신은 단지 그들을 위해 확인하여 쿼리가 다소 간단 할 수

업데이트 :

select *, 
    -- If there's a date, display it 
    case 
     when StartChar1 > 0 then substring(RawData, StartChar1, 10) 
     when StartChar2 > 0 then substring(RawData, StartChar2, 8) 
     else null 
    end as DateString 
from (
    select *, 
     -- Find the first MM-DD-YYYY 
     patindex('%[0-1][0-9][/-][0-3][0-9][/-][0-9][0-9][0-9][0-9]%', RawData) as StartChar1, 
     -- Find the first MM-DD-YY 
     patindex('%[0-1][0-9][/-][0-3][0-9][/-][0-9][0-9]%', RawData) as StartChar2 
    from @Demo 
) as a 
+0

대단히 감사합니다! 귀하의 대답은 제 데이터 세트와 잘 작동합니다. – Kyle

1

CndiedCode의 링크 예제는 사용자가 필요로하는 것과 매우 가깝습니다.

그냥 약간 다른 정규식 일치

N '^ \ D {3} - \ d를 {2} - \ d를 {4} $'

N '\ d를 간다 {2}/\ D {2}/\ D {2,4} '

당신은 순수 T-SQL에서이 작업을 수행 할 수있는 \

if (Regex.IsMatch("sent on 01/01/10; ex", "\\d{2}/\\d{2}/\\d{2,4}")) 
    { 
     System.Diagnostics.Debug.WriteLine(Regex.Match("sent on 01/01/10; ex", "\\d{2}/\\d{2}/\\d{2,4}")); 
    } 
    if (Regex.IsMatch("sent on 01/01/2012; ex", "\\d{2}/\\d{2}/\\d{2,4}")) 
    { 
     System.Diagnostics.Debug.WriteLine(Regex.Match("sent on 01/01/2012; ex", "\\d{2}/\\d{2}/\\d{2,4}")); 
    } 
+0

Tim의 대답은 순수한 TSQL +1 인 것으로 보입니다. 일이 더 복잡 해지면 정규 표현식으로 들어가야합니다. – Paparazzi

+0

SSIS를 처음 사용하고 정규식이 어떻게 이루어지는 지보고 싶어합니다. 강력하고 유용한 물건처럼 보입니다. 혹시 이걸 나에게 보여줄 수 있니? – Kyle

+0

나는 전문가가 아니므로 내가 할 수있는 것은 내가 참고할 곳을 가리킨다 http://www.regular-expressions.info/ 살아있는 데이터 파싱을 다룰 계획이라면 필요한 도구이다. 너의 벨트에 가지고있다. – Paparazzi