SQL Server char(1) and char(2) column -


i need table bookkeeping types in data model. don't have many types fair deal of 'em. ideally short descripive names work well.

so did this:

create table entitytype (     entitytypeid char(2) primary key,     entitytypename varchar(128) not null ) 

and put data table:

insert entitytype values ('a', 'type1') insert entitytype values ('b', 'type2') 

but can query baffles me:

declare @pentitytype char(1) set @pentitytype = 'a' select ''''+entitytypeid+'''', entitytypename  entitytype  entitytypeid = @pentitytype 

the result yields 'a ', , there's whitespace in literal.

my understanding there's implicit conversion that's converting char(1) -> char(2)

i'm not complaining, what's rationale behind this?

the reason behaviour trailing spaces ignored in string comparisons in sql server. happens irrespective of whether fixed or variable length data types being used can seen below.

declare @entitytype table (     entitytypeid varchar(2) primary key,     entitytypename varchar(128) not null )  insert @entitytype values ('a ', 'type1') insert @entitytype values ('b', 'type2')  declare @pentitytype varchar(1) set @pentitytype = 'a' select ''''+entitytypeid+'''', entitytypename  @entitytype  entitytypeid = @pentitytype 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -