user2657943
user2657943

Reputation: 2758

What is the max value of a CHAR?

I was wondering what the max char value is in sql? I noticed in C# this \uFFFF, but when I use that value to compare a string SQL renders it as an empty string I think.

The table is in SQL_Latin1_General_CP1_CI_AS if that matters.

Upvotes: 0

Views: 18845

Answers (3)

Tom Blodget
Tom Blodget

Reputation: 20812

Numerically, the answer is 255. CHAR has a potential range of 0 to 255. It is an 8-bit code unit for the character encoding configured for the field (which it might inherit from the table or database).

Whether 255 is a valid code unit and is a complete codepoint, and which character it represents, and its sort order (is that what you meant by max?), depends on the collation. (A collation specifies a character encoding and sort order.)


Oh, if you are going to compare SQL datatypes to others, NVARCHAR and C#'s char and .NET's Char all use UTF-16 as the character encoding.

Upvotes: 0

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67311

There is a deep misconception of what is ascii...

ASCII is a 7bit code (0 to 127) where the characters are fix

the 8th bit offers this range a second time (128 to 255). In this area the characters are depending on codepages and collations.

Thinking of CHAR as a BYTE (8 bit in memory) is misleading...

Try this, both return a captial A

SELECT CHAR(65) COLLATE Latin1_General_CI_AS
SELECT CHAR(65) COLLATE Arabic_CI_AS

The code 255 renders with Latin1_General_CI_AS as ÿ, with the arabic collation there seems to be no printable character, hence the question mark.

SELECT CHAR(255) COLLATE Latin1_General_CI_AS
SELECT CHAR(255) COLLATE Arabic_CI_AS

So in short: SQL renders it as an empty string is not true. This is depending on your settings

Upvotes: 6

Rahul
Rahul

Reputation: 77906

Did you checked Documentation as it clearly says

char [ ( n ) ]

Fixed-length, non-Unicode string data. n defines the string length and must be a value from 1 through 8,000. The storage size is n bytes. The ISO synonym for char is character.

Upvotes: 0

Related Questions