SQL Management Studio messes up query syntax

77 views Asked by At

I am trying to remove a space and a no-break space from a varchar field i'm going to cast as an int, however, when using the replace method SQL Management Studio messes up the syntax after accepting it so I can't save it. This my query:

SELECT SZOMSCHRIJVING 
FROM dbo.BETONKWALITEITEN AS b 
WHERE (iBetonMengsel = CAST(REPLACE(p.SZBETONRECEPT, '  ', '') as INT))

After accepting this syntax, SQL Management Studio turns it into the following code:

SELECT SZOMSCHRIJVING 
FROM dbo.BETONKWALITEITEN AS b 
WHERE (iBetonMengsel = CAST(REPLACE('p.SZBETONRECEPT.   . ',,) AS INT))

How do I replace the space and no-break space from this column?

Image to attempt to show the problem. enter image description here

1

There are 1 answers

1
cloudsafe On

Try replacing the char values 32 for space and 160 for no-break space:

SELECT SZOMSCHRIJVING 
FROM dbo.BETONKWALITEITEN AS b 
WHERE (iBetonMengsel = CAST(REPLACE(REPLACE(p.SZBETONRECEPT, char(160), ''), char(32), '') as INT))