Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130349
  • 博文数量: 69
  • 博客积分: 595
  • 博客等级: 中士
  • 技术积分: 670
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-16 17:37
文章分类

全部博文(69)

文章存档

2017年(2)

2016年(9)

2015年(13)

2014年(30)

2012年(4)

2011年(2)

2010年(2)

2009年(5)

2008年(2)

我的朋友

分类: SQLServer

2014-12-15 17:26:02

/*There is a column with data like 'plase20080412OK', 'jdjldk19351123',
 please write a query to get the Date value.*/
Create table #GetDateStr (String1 varchar(50))
insert into #GetDateStr values ('please20080412ok'), ('Jane19880416Hi'), ('euout19950612dk');


/*The subquery is used to trim the left characters before date,
second query trims characters after date*/
select substring(String2, 1, PATINDEX('%[A-Z,a-z]%',String2)-1) from
(
  select substring(String1, PATINDEX('%[0-9]%',String1),len(String1)) as String2 
  from #GetDateStr
) tbl


-- or --
select substring(String1, 
                 CASE When Charindex('20',String1) != 0 Then Charindex('20',String1)
     When Charindex('19',String1) != 0 Then Charindex('19',String1)
end,8) 


from #GetDateStr


阅读(649) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~