을 탈출했다으로 아래 코드는 다른 보인다. 다음은 작동하는 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
즉의 작업이 될 것이다 SSIS, 어떻게하는지 압니다. 지금 당장은 날짜를 깨끗하게 잡아낼 SQL 문을 작성하려고합니다. – Kyle
그것은 TSQL이 직접 할 일을 추진할 것입니다. CLR 및 Regex가 필요할 수 있습니다. – Paparazzi
나는 또한 CLR과 Regex를 제안하려고했다. http://msdn.microsoft.com/en-us/magazine/cc163473.aspx#S2 – CandiedCode