鑑於以下情況 - 如何在SQL Server中將尾隨空格替換爲0?如何在SQL Server中用0替換尾隨空格?
CREATE TABLE TestTable
(
TestField CHAR(8)
)
INSERT INTO TestTable
SELECT ' 1 1 1 '
INSERT INTO TestTable
SELECT ' 1 1 1 1'
SELECT
TestField,
LEN(TestField) AS LenTestField,
REPLACE(TestField, ' ', '0') AS TestFieldReplaced,
LEN(REPLACE(TestField, ' ', '0')) AS LenTestFieldReplaced,
REPLACE(TestField, ' ', '0') + REPLICATE('0', DATALENGTH(TestField)-LEN(TestField)),
REPLACE(CONVERT(VARCHAR,TestField), ' ', '0')
FROM TestTable
我更新這包括從接受的答案爲例,使用複製如下所示。 – Brandon
顯然轉換爲varchar也適用。 – Brandon