/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu




Wednesday, May 21, 2014

Using char varchar in sql server

Char Vs Varchar

Few example showing the difference between char and varchar:

DECLARE @CharName Char(30) = 'Yogesh Chandra Upreti',
@VarCharName VarChar(30) = 'Yogesh Chandra Upreti'
SELECT DATALENGTH(@CharName) CharSpaceUsed,DATALENGTH(@VarCharName) VarCharSpaceUsed


DECLARE @FirstName Char(20) = 'YogeshChandra',
@LastName Char(20) = 'Upreti
IF @FirstName + ' ' + @LastName = 'YogeshChandra Upreti
 PRINT 'I was Expecting'
ELSE 
 PRINT 'Surprise to me ...' 
SELECT  @FirstName + ' ' + @LastName AS Name, len(@FirstName + ' ' + @LastName) AS Length


DECLARE @FirstName VarChar(20) = 'YogeshChandra',
@LastName VarChar(20) = 'Upreti
IF @FirstName + ' ' + @LastName = 'YogeshChandra Upreti
 PRINT 'I was Expecting'
ELSE 
 PRINT 'Surprise to me ...' 
SELECT  @FirstName + ' ' + @LastName AS Name, len(@FirstName + ' ' + @LastName) AS Length






No comments:

Post a Comment

Recent Posts

My Blog List