从字符串中提取日期并将其复制到其他日期列SQL

时间:2018-01-03 20:00:43

标签: mysql sql date

我有一个几乎没有字段的SQL表。

id,日期,描述等

描述是人们添加一些内容的字符串..

示例1:Pista Automobilistica Con Automobili 500 04/07/1957 , Poi Le 500

示例2:Context date: 09/02/1979.. text text..

我需要从文本中提取日期(d/m/y)并以sql格式将其复制到de Date列(y/m/d

2 个答案:

答案 0 :(得分:1)

- 此查询返回列描述的日期(只有一个日期字符串)

选择str_to_date(substr(descrip,instr(descrip,' /') - 2,10),'%d /%m /%Y')as tab from tabla ;

答案 1 :(得分:0)

--This is for MSSQL Server, I hope you got the idea

declare @str1 varchar(max),@str2 varchar(max),@str3 varchar(max)

 set @str1='Pista Automobilistica Con Automobili 500 04/07/1957 , Poi Le 500'
 set @str2='Context date: 09/02/1979.. text text..'
 set @str3='xzczxczxczxcxzc 12/12/2018 xcvxcvxc'

 print convert(date,substring(@str1,CHARINDEX('/',@str1,1)-2,10),101)
 print convert(date,substring(@str2,CHARINDEX('/',@str2,1)-2,10),101)
 print convert(date,substring(@str3,CHARINDEX('/',@str3,1)-2,10),101)