截图解释了这一切。 http://i46.tinypic.com/f3hobl.png
使用当前配置, InvoiceSentDate 仅接受8位日期(MM-DD-YY)。我希望能够捕捉MM-DD-YYYY日期。我该怎么做?
作为比较,请查看发票2106-2112 vs 2116。
FURTHERMORE,让事情复杂化!有些记录在日期之后有文字。 http://i50.tinypic.com/2r5qa88.png
答案 0 :(得分:4)
您可以在纯T-SQL中执行此操作。这是工作SqlFiddle。
在这里,我找到patindex
的日期,然后找到第一个非数字。这为substring
提供了单独提取日期所需的参数。如您所见,我添加了一些测试数据,涵盖了各种可能性,包括斜线和破折号日期分隔符。
-- 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
答案 1 :(得分:1)
CndiedCode链接中的示例非常接近您的需求。
与Regex匹配略有不同
N'#39; ^ \ d {3} - \ d {2} - \ d {4} $&#39;
去
N'#39; \ d {2} / \ d {2} / \ d {2,4}&#39;
下面的代码看起来不同,因为必须逃避\
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}"));
}