INTERVIEW QUESTIONS AND ANSWERS FOR SOFTWARE DEVELOPER

Difference between Sql Server Char and Varchar Data Type

What is difference between nvarchar and char?



CHAR Data Type is a Fixed Length Data Type. For example if you declare a variable/column of CHAR (10) data type, then it will always take 10 bytes irrespective of whether you are storing 1 character or 10 character in this variable or column. And in this example as we have declared this variable/column as CHAR (10), so we can store max 10 characters in this column.

VARCHAR is a variable length Data Type. For example if you declare a variable/column of VARCHAR (10) data type, it will take the no. of bytes equal to the number of characters stored in this column. So, in this variable/column if you are storing only one character then it will take only one byte and if we are storing 10 characters then it will take 10 bytes

DECLARE @Char Char(20) = 'MEHUL',
  @VarChar VarChar(20) = '  MEHUL'
 SELECT DATALENGTH(@Char) CharSpace,
  DATALENGTH(@VarChar) VarCharSpace
Result:






DECLARE @FirstName Char(20) = 'MEHUL',
  @LastName Char(20) = 'MAHERIYA'

SELECT  @FirstName + ' ' + @LastName AS Name,
 len(@FirstName + ' ' + @LastName) AS Length


 Go


 DECLARE @FirstName VarChar(20) = 'MEHUL',
 @LastName VarChar(20) = 'MAHERIYA'


SELECT  @FirstName + ' ' + @LastName AS Name,
 len(@FirstName + ' ' + @LastName) AS Length

So, it is clear from the above examples that during concatenation of CHAR data type variables, it includes space in-place of unused space in the result of concatenation.

Data Type
Length
Storage Size
Max Characters
Unicode
char
Fixed
Always bytes
8,000
No; each character requires 1 byte
varchar
Variable
Actual length of entry in bytes
8,000
No; each character requires 1 byte
nchar
Fixed
Twice n bytes
4,000
Yes; each character requires 2 bytes
nvarchar
Variable
Twice actual length of entry in bytes
4,000
Yes; each character requires 2 bytes




Difference between Sql Server VARCHAR and NVARCHAR Data Type


Difference between Sql Server NCHAR and NVARCHAR Data Type

NCHAR data type:
1     Is a fixed length data type
2    Used to store Unicode characters (for example the languages Arabic,  German and so on)
       Occupies 2 bytes of space for each character

DECLARE @name NCHAR(10) 
SET @name = 'MEHUL' 
SELECT @name AS 'Name', DATALENGTH(@name) AS 'Datalength' , LEN(@name) AS 'Len'



NVARCHAR data type:
  • It is a variable-length data type
  • Used to store Unicode characters
  • Occupies 2 bytes of space for each character

DECLARE @name NVARCHAR(50) 
SET @name = 'MEHUL' 
SELECT @name AS 'Name', DATALENGTH(@name) AS 'Datalength' , LEN(@name) AS 'Len'




Explain .net Architecture?
Explain MVC architecture?
Explain SQL Transaction in .net?
What is abstract class?
What is interface?
What is inheritance?
What is Entity Framework?



Comments

Popular posts from this blog

Validate Mobile Number with 10 Digits in ASP.Net