我正在尝试解析14个字符varchar
(不总是14个字符,有时是12个或10个或0个)列,以便在第二个结果的前面小数点后以两部分显示结果。
EmployeeCode ResultCol1 ResultCol2
-----------------------------------------------
42135430620000 421354306200 .00
42135431750000 421354317500 .01
42135431740001 421354317400 .00
42135430923333 421354309233 .33
42135432370000 421354323700 .00
431354324200 4313543242 .00
42135432320000 421354323200 .00
42135432390000 421354323900 .00
42135430940000 421354309400 .00
421354324300 4213543243 .00
42135431980000 421354319800 .00
421354324400 4213543244 .00
42135432830000 421354328300 .00
421354323800 4213543238 .00
421354328500 4213543285 .00
421354328100 4213543281 .00
421354328400 4213543284 .00
421354328200 4213543282 .00
421354328600 4213543286 .00
421354330000 4213543300 .00
答案 0 :(得分:0)
使用LEFT
和RIGHT
字符串函数。
SELECT EmployeeCode,
LEFT(EmployeeCode, CASE
WHEN Len(EmployeeCode) = 0 THEN 1
ELSE Len(EmployeeCode) - 2
END) AS ResultCol1,
'.' + RIGHT(EmployeeCode, 2) AS ResultCol2
FROM yourtable
<强>样本强>
DECLARE @EmployeeCode VARCHAR(50)='42135430620000'
SELECT @EmployeeCode as EmployeeCode,
LEFT(@EmployeeCode, CASE
WHEN Len(@EmployeeCode) = 0 THEN 1
ELSE Len(@EmployeeCode) - 2
END) AS ResultCol1,
'.' + RIGHT(@EmployeeCode, 2) AS ResultCol2
结果
EmployeeCode ResultCol1 ResultCol2
-------------- ------------ ----------
42135430620000 421354306200 .00