与 SQL 服务器中的子字符串和解析混淆
Confusion with Substring and Parse in SQL Server
我正在尝试解析 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
使用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
我正在尝试解析 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
使用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