sql获取字符串和特殊字符之间的字符

fnvucqvd  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(450)

我正在尝试转换工作周的格式。当前格式如下所示:

workweek
------------
2020ww20.5
2020ww18.3
2019ww05.2

我要将格式转换为:

workweek
------------
202020
202018
202005

以下是我尝试过的:

SUBSTRING(workweek,
    CHARINDEX('ww',workweek)+2,
    CHARINDEX('.',workweek)- CHARINDEX('ww',workweek)-1)

workweek outcome: 202020.

我也试过了

SUBSTRING(workweek,
    PATINDEX('%ww%', workweek)+1,
    PATINDEX('%.%', workweek) - PATINDEX('%ww%', workweek))

workweek outcome: 2020w20

先谢谢你

hm2xizp9

hm2xizp91#

如果字符串总是遵循这种固定格式,那么 substring() 就足够了:

concat(
    substring(workweek, 1, 4),
    substring(workweek, 7, 2)
)

db小提琴演示:

select workweek, concat(substring(workweek, 1, 4), substring(workweek, 7, 2)) result
from (values ('2020ww20.5'), ('2020ww18.3'), ('2019ww05.2')) as t(workweek);
workweek   | result
:--------- | :-----
2020ww20.5 | 202020
2020ww18.3 | 202018
2019ww05.2 | 201905
ibps3vxo

ibps3vxo2#

你可以的 replace “ww”然后得到一切 left 到第6个字符

left(replace(workweek,'ww',''),6)

相关问题