Combine Substring & replace in SQL Server?

kxe2p93d  于 2023-10-15  发布在  SQL Server
关注(0)|答案(1)|浏览(139)

I am extracting the first part of column in SQL Server using substring to get an order number, however some of the data has a comma after it which I would also like to remove.

This is what I'm using to extract the order number:

SUBSTRING([Column], 1, CHARINDEX(' ', [Column])) AS OrderNbr

A couple of the values are being extracted as '01234,' or '02348,'

Is there a way I can remove the comma from the data. I tried using replace but wasn't able to combine it with SUBSTRING :

REPLACE([Column], ',', ' ') AS test
xxhby3vn

xxhby3vn1#

I think you actually can combine substring and replace like this :

SELECT 
    REPLACE(SUBSTRING([Column], 1, CHARINDEX(' ', [Column])), ',', '') AS OrderNbr
FROM 
    YourTableName;

相关问题