0

I am reading some data from a database table and the text contains the '/' character.

I now need to insert this string into a different SQL table but I'm having problems escaping it correctly.

If the string I read contains this pattern:

Test/test2

After some google searching, I am using to insert the text (google refs Ref1 and Ref2)

Field1= replace (cast('Test/th' as nvarchar),'/','\\\')

But it still returns the error:

Msg 8152, Level 16, State 2, Line 18 String or binary data would be truncated.

If I use:

Field1= replace (cast('Test/th' as nvarchar),'/','')

Everything is fine

Can anyone help me with my escape clause please?

Community
  • 1
  • 1
Ian W
  • 385
  • 2
  • 10
  • 30
  • 1
    That error would indicate that you are trying to store a string longer than the column will accept. I don't think this has anything to do with special characters. What is the definition of `Field1`? You should also use a length with `NVARCHAR` in your convert, e.g. `CONVERT(NVARCHAR(20), 'Test/th')` – GarethD Jan 09 '17 at 15:23
  • 1
    What is the point of that first replace? You are changing all instances of a forward slash with....a forward slash. – Sean Lange Jan 09 '17 at 15:26
  • Sorry you are correct it was meant to be \ – Ian W Jan 09 '17 at 16:29

1 Answers1

0

I think your column field length is set to 6. Change it to 7.

cloudsafe
  • 2,444
  • 1
  • 8
  • 24