select case when charindex(natureDate,'-')>0 then natureDate else right((substring(natureDate,1,10)),4) end as natureDate from nafc_Accident
但是总是执行else里面的语句。不知道为什么数据是这样的
2011-06-21
2011-06-10
2011-04-18
2011-03-22
2010-12-30
2007-07-15
2006-07-15
08 21 2010 12:00AM
03 15 2011 12:00AM
03 13 2011 12:00AM
03 1 2011 12:00AM
01 17 2011 12:00AM目的就是想取出年份,或者有没有其他办法
select case when charindex('-',natureDate)>0 then natureDate else
right((substring(natureDate,1,10)),4) end as natureDate from nafc_Accident
--或是select year(natureDate) as natureDate from nafc_Accident
写反了CHARINDEX
返回字符串中指定表达式的起始位置。
语法
CHARINDEX ( expression1 , expression2 [ , start_location ] ) 如果在 expression2 内没有找到 expression1,则 CHARINDEX 返回 0。
这样才对
charindex('-',natureDate)>0
replace(你要去空格的字段,' ','')