Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine Substring & replace in sql server?

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,’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way i can remove the comma from the data. I tried using replace but wasnt able to combine it with substring.

Replace([Column],',',' ') as  test

>Solution :

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading