SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Different Behaviour of VARCHAR and NVARCHAR


Different Behaviour of VARCHAR and NVARCHAR

Author
Message
pvsrpk
pvsrpk
SSC-Enthusiastic
SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)

Group: General Forum Members
Points: 162 Visits: 269
Hi,

I need a small clarification. Please see the below code.

DECLARE @var1 NVARCHAR(100)
DECLARE @var2 VARCHAR(100)
SELECT @var1 ='th'
SELECT @var2='th'
print 'nv'+@var1 ----- Outputs: nvth
print 'nv'+replace(@var1,char(222),'a') ----- Outputs: nva
print 'v'+@var2----- Outputs: vth
print 'v'+replace(@var2,char(222),'b')----- Outputs: vth

Here the variables @var1 is declared as NVARCHAR and @var2 is declared as VARCHAR.

The second print statement is printing nva replacing the th with a.

But the fourth print statement is printing vth.
It is not replacing the th with b.

Please can anyone explain why this behaviour is different.
Scott Coleman
Scott Coleman
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10603 Visits: 1561
Try this:
print 'nv'+replace(@var1,nchar(222),'a') ----- Outputs: nva
print 'v'+replace(@var2,nchar(222),'b')----- Outputs: vb



You are attempting a double-byte character replacement, so it only makes sense with nchar arguments. If either of the arguments is nchar, the other will be converted and the replacement will work. If both arguments are char, it won't work.



pvsrpk
pvsrpk
SSC-Enthusiastic
SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)

Group: General Forum Members
Points: 162 Visits: 269
nchar(222) or char(222) is Þ.

How this is replacing th. Moreover th is two characters and above one is just only one character.
Jack Corbett
  Jack Corbett
SSC Guru
SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)

Group: General Forum Members
Points: 70854 Visits: 14948
I'm actually not seeing that behavior. In both cases the 'th' is being replaced. I'm not sure why it is working at all. According to the ASCII table I am looking at 222 is a Right Half Block character.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Scott Coleman
Scott Coleman
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10603 Visits: 1561
CHAR(222) will be interpreted differently depending on the code page. If you are using code page 1252 (US English), it is Þ. It would only be an ASCII right half block in an old MSDOS code page (839 I think).

NCHAR(222), whatever it is, is Unicode and should be the same character everywhere. That's the whole point of Unicode. I don't have much experience with Unicode, I haven't seen anything like the replacement originally posted before, and I have no idea where double-byte character equivalents are documented. After trying the sample code that was posted however, and seeing that a Unicode "th" was indeed recognized as CHAR(222), it didn't take much imagination to conclude that this only works when one or both of the arguments are Unicode.



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search