SQL截取字符串

我请讲解下,SQL截取字符串?

某字段中数据格式为:H300172013082300**** or HW30017201204803**** or 201312111 3种
我要截取的格式为:201312111 我想到的是用SUBSTRING,但是需要截取2次,因为带H和带HW的,位置是不一致的,那么我想到用case when 字段 like '%H%' 但是用了like后,‘HW’ 也包含了进来,还有'201312111' ,变成了Null
附带SQL:
select WBOOK_NO = case when WBOOK_NO like '%H%' then SUBSTRING(WBOOK_NO,7,9) when WBOOK_NO like 'W%'then SUBSTRING(WBOOK_NO,8,9) end, WBOOK_NOfrom EMS_PDE_HEAD WHERE WBOOK_NO='H3001720130823003698' OR WBOOK_NO='HW300172012048035872'or WBOOK_NO='201001780'
SQL怎么截取得到 201001780
最新回答
じ★ve妳

2024-11-27 06:05:49

select WBOOK_NO = 
case when WBOOK_NO like '%H%' and WBOOK_NO not like '%HW%' then SUBSTRING(WBOOK_NO,7,9)   
when WBOOK_NO like 'H%' and then SUBSTRING(WBOOK_NO,8,9) end, 
WBOOK_NO
from EMS_PDE_HEAD WHERE WBOOK_NO='H3001720130823003698' OR WBOOK_NO='HW300172012048035872'

LIKE 'H%'时再and NOT LIKE 'HW%'就好啦

薄凉

2024-11-27 07:08:33

select WBOOK_NO = case when WBOOK_NO like '%H%' then SUBSTRING(WBOOK_NO,7,9)
when WBOOK_NO like 'W%'then SUBSTRING(WBOOK_NO,8,9) else WBOOK_no end,--注意else语句
WBOOK_NO
from EMS_PDE_HEAD WHERE WBOOK_NO='H3001720130823003698' OR WBOOK_NO='HW300172012048035872'
or WBOOK_NO='201001780'