question

michelle avatar image
michelle asked

How to add quotation marks in SQL

Hi,

I have a result set like

ID-------- Post_Code----Suburb

2036-------2112----------Beverly Hills

3478-------2167----------Cremone

5390-------2022----------Paddington

How do I add quotation marks for ID and Post_Code as in

ID-------- Post_Code----Suburb

"2036"-------"2112"----------Beverly Hills

"3478"-------"2167"----------Cremone

"5390"-------"2022"----------Paddington

Can anyone please help?

Thanks

Michelle

t-sqlformatting
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered

Try this....

select
 '"'+cast(ID as varchar)+'"',
 '"'+cast(Post_Code as varchar)+'"',
 Suburb
from
 MyTable
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

TimothyAWiseman avatar image
TimothyAWiseman answered

As always, Kev has the right answer. Just for completeness, you could use the built in quotename function, specifying " as the option quote character of course. This will double up the quotation marks if one is actually located within your string though, whether or not this is a good thing depends on your situation.

http://msdn.microsoft.com/en-us/library/ms176114(SQL.90).aspx

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
The only problem with `QUOTENAME` is that it is limited to strings of 128 characters or less - anything more than 128 will return `null`
1 Like 1 ·
michelle avatar image
michelle answered

I have got an error message 'a character string failed conversion to numeric value'. What should I do? Michelle

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.