Prova detta...
DECLARE @table table(XYZ VARCHAR(8) , id int)
INSERT INTO @table
SELECT '4000', 1
UNION ALL
SELECT '3.123', 2
UNION ALL
SELECT '7.0', 3
UNION ALL
SELECT '80000', 4
UNION ALL
SELECT NULL, 5
UNION ALL
SELECT 'WTF',6
SELECT CASE
WHEN ISNUMERIC(XYZ) = 0 THEN NULL
WHEN CHARINDEX('.',XYZ,0) < LEN(XYZ)-2 AND CHARINDEX('.',XYZ,0) > 0 THEN XYZ
WHEN ISNUMERIC(XYZ) >0 then convert(decimal(18,3),xyz) / 1000.000
ELSE NULL
END
FROM @table
Utdata
4.00000000000
3.12300000000
0.00700000000
80.00000000000
NULL
NULL
Redigera - för att hålla dig till 3 decimaler i utdata gör du detta
SELECT convert(decimal(8,3),CASE
WHEN ISNUMERIC(XYZ) = 0 THEN NULL
WHEN CHARINDEX('.',XYZ,0) < LEN(XYZ)-2 AND CHARINDEX('.',XYZ,0) > 0 THEN XYZ
WHEN ISNUMERIC(XYZ) >0 then convert(decimal(18,3),xyz) / 1000.000
ELSE NULL
END)
FROM @table
Observera att (8,3) definierar detta, total precision 8 siffror, 3 efter punkten.
Du kanske vill konvertera tillbaka till varchar(8) också