截图解释了一切。http://i46.tinypic.com/f3hobl.png
对于当前的配置,InvoiceSentDate只接受8位数的日期(MM-DD-YY).我希望能够捕获MM-DD-YYYY的日期以及。我该怎么做?
要比较,请看发票2106-2112 vs 2116。
而且,让事情变得更复杂!有些记录在日期之后有文本。http://i50.tinypic.com/2r5qa88.png
发布于 2012-09-21 16:47:08
您可以在纯中完成这一任务。这是工作的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
发布于 2012-09-21 15:36:04
CndiedCode链接中的示例非常接近您需要的内容。
只是稍微不同的Regex匹配
N‘^d{3}-\d{2}-\d{4}$’
转到
N‘d{2}/\d{2}/\d{2,4}’
下面的代码看起来不同,因为必须转义\
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}"));
}
https://stackoverflow.com/questions/12532839
复制相似问题