Monday, May 30, 2016
2016-SSIS-1-Import data from excel into SQL Server using SSIS
Import data from excel into SQL Server using SSIS
SQl- Difference Between Char, Nchar, Varchar and Nvarchar Data Types in SQL Server
Difference Between Char, Nchar, Varchar and Nvarchar Data Types in SQL Server
This small article is intended for the audience stuck in their interview when asked for the differences among CHAR, VARCHAR, NCHAR and NVARCHAR data types. Actually it is simple but sometimes people get confused.
To store data as characters, numeric values and special characters in a database, there are 4 data types that can be used. So what is the difference among all 4 of these data types?
To store data as characters, numeric values and special characters in a database, there are 4 data types that can be used. So what is the difference among all 4 of these data types?
- CHAR vs VARCHAR
- NCHAR vs NVARCHAR
Considering an example, we will look into each one of them.
- DECLARE @string CHAR(20)
- SET @string = 'Robin'
- SELECT @string AS 'String', DATALENGTH(@string) AS 'Datalength' , LEN(@string) AS 'Len'
Note: The LEN() method provides the length of a character excluding trailing blanks stored in the string expression whereas the DATALENGTH() method provides the number of byte spaces occupied by the characters in a string expression.
As you know we represent the character values within single quotes, for example 'Robin'. But do you know we can represent these same characters within double quotes similar to programming languages representing a string, for example “Robin”? This can be done by setting the value:
As you know we represent the character values within single quotes, for example 'Robin'. But do you know we can represent these same characters within double quotes similar to programming languages representing a string, for example “Robin”? This can be done by setting the value:
- SET QUOTED_IDENTIFIER OFF
By default, it is set to ON
CHAR vs VARCHAR
CHAR vs VARCHAR
Talking about the CHAR data type:
- It is a fixed length data type
- Used to store non-Unicode characters
- Occupiers 1 byte of space for each character
If the value provided to a variable of CHAR data type is shorter than the length of a column of declared the size of the variable, then the value would be right-padded with blanks to match the size of column length.
- DECLARE @string CHAR(20)
- SET @string = 'Robin'
- SELECT @string AS 'String', DATALENGTH(@string) AS 'Datalength' , LEN(@string) AS 'Len'
As you can see above, the bytes occupied by the variable are 20 even though the length of the characters is 5. That means that irrespective of the character stored in the column, it will occupy all bytes to store the value.
About the VARCHAR data type:
- It is a variable length data type
- Used to store non-Unicode characters
- Occupies 1 byte of space for each character
- DECLARE @string VARCHAR(20)
- SET @string = 'Robin'
- SELECT @string AS 'String', DATALENGTH(@string) AS 'Datalength' , LEN(@string) AS 'Len'
As you can see above, it is showing DATALENGTH as 5 which means it will use only the number of bytes equal to the number of characters. This will allow me to avoid wasting database space.
Note: If SET ANSI_PADDING is OFF when CREATE TABLE or ALTER TABLE is executed, a CHAR column defined as NULL is considered as VARCHAR.
When to use what?
If you are sure about the fixed length of the data that would be captured for any specific column then go for CHAR data type and if the data may vary then go for VARCHAR.
NCHAR vs NVARCHAR
Similar to CHAR data type, the NCHAR data type:
Note: If SET ANSI_PADDING is OFF when CREATE TABLE or ALTER TABLE is executed, a CHAR column defined as NULL is considered as VARCHAR.
When to use what?
If you are sure about the fixed length of the data that would be captured for any specific column then go for CHAR data type and if the data may vary then go for VARCHAR.
NCHAR vs NVARCHAR
Similar to CHAR data type, the NCHAR data type:
- Is a fixed length data type
- Used to store Unicode characters (for example the languages Arabic, German and so on)
- Occupies 2 bytes of space for each character
- DECLARE @string NCHAR(20)
- SET @string = 'Robin'
- SELECT @string AS 'String', DATALENGTH(@string) AS 'Datalength' , LEN(@string) AS 'Len'
As you can see above, the data length column shows 40 bytes even though the size declared is 20. It's because NCHAR holds 2 bytes of space for each character.
About the NVARCHAR data type:
- It is a variable-length data type
- Used to store Unicode characters
- Occupies 2 bytes of space for each character
- DECLARE @string NVARCHAR(20)
- SET @string = 'Robin'
- SELECT @string AS 'String', DATALENGTH(@string) AS 'Datalength' , LEN(@string) AS 'Len'
As in the output above, you will observe DATALENGTH column is showing only 10 as a value. That is because it occupies 2 bytes of space for each character and the data length is only 5 characters, therefore it will occupy 10 bytes of space in the database.
When to use what?
If your column will store a fixed-length Unicode characters like French, Arabic and so on characters then go for NCHAR. If the data stored in a column is Unicode and can vary in length, then go for NVARCHAR.
When to use what?
If your column will store a fixed-length Unicode characters like French, Arabic and so on characters then go for NCHAR. If the data stored in a column is Unicode and can vary in length, then go for NVARCHAR.
Querying to NCHAR or NVARCHAR is a bit slower then CHAR or VARCHAR. So don't go for NCHAR or NVARCHAR to store non-Unicode characters even though this data type supports that.
ConclusionThis small article is just to make you aware of the differences among the CHAR, VARCHAR, NCHAR and NVARCHAR data types since they are all used to store characters, numbers or special characters. I hope you like this small article and will that it will be helpful to you at some point of time. Leave your comments whether its good or bad. Sharing is valuable no matter what :)
Saturday, May 21, 2016
SQL - case statement
Understanding Case Expression in SQL Server with Example
- -- Simple CASE expression:
- SELECT FirstName ,StateCode,Gender, Total=MAX(PayRate)
- FROM dbo.Customer
- GROUP BY StateCode,Gender,FirstName
- HAVING (MAX(CASE Gender WHEN 'M'
- THEN PayRate
- ELSE NULL END) > 180.00
- OR MAX(CASE Gender WHEN 'F'
- THEN PayRate
- ELSE NULL END) > 170.00)
- -- Searched CASE expression:
- SELECT FirstName ,StateCode,Gender, Total=MAX(PayRate)
- FROM dbo.Customer
- GROUP BY StateCode,Gender,FirstName
- HAVING (MAX(CASE WHEN Gender = 'M'
- THEN PayRate
- ELSE NULL END) > 180.00
- OR MAX(CASE WHEN Gender = 'F'
- THEN PayRate
- ELSE NULL END) > 170.00)
COALESCE and ISNULL
Source:
COALESCE and ISNULL
According to SQL Server Books Online, COALESCE "returns the first nonnull expression among its arguments," and ISNULL "replaces NULL with the specified replacement value." As a simple example, the following code demonstrates using the two functions:
Saturday, May 14, 2016
SSIS - Send Mail - Script Task email setting - attach Zip file - Xml content
Send Mail - SSIS
|
Send Mail Task:->
~
General
Mail
Expression
~
Mail
SmtpConnection
From
To
Cc
BCcc
Subject
MessageSourceType Direct Input
MessageSource <--Message that you want to send ->
Priority Normal
Attachments <-Attachment of the file you want->
Note:
i.Send Mail Task -> sends text emails
But if you need to send "HTML" format Send Mail Task can not send.
If you want attach Zip file also it can not send.
Security:
As you are using "SmtpConnection" , it takes your own credential.
When you move to production the "Service Account" that you use need to have permission to send email.
-----------------------------------------------------------------------------------------------------------------------------------------------
This session is using script task.
Script Task:
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.windows.Forms;
using System.IO;
using System.Net;
using System.Net.Mail;
public void Main( )
{
string Sender = "entersenderemailaddress";
string Recipient = "enterrecipientemailaddress";
string SMTP="smtp.live.com";//"smtp.gmail.com";
string Body="Message that you want to send";
Attachment Attach= new Attachment("C:\\yourfilepath\\test2.zip");
MailMessage msg= new MailMessage(Sender,Recipient,"Attached Org XML Data", Body);
SmtpClient smtpClient=new SmtpClient(SMTP,25); // 25 -- SMTP Port
smtpclient.EnableSsl= true;
smtpclient.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials = new NetworkCredential(Sender ,"DV6226TX"); // "DV6226TX" - current password of the email account
smtpclient.UseDefaultCredentials = false;
smtpclient.Credentials=credentials;
msg.Attachments.Add(Attach);
smtpclient.Send(msg);
Dts.TaskResult = (int) ScriptREsults.Success;
}
Saturday, May 7, 2016
Learning XML Tutorials
Learn XML in 30 days
XML Tutorials
coursera-Stanford University-Introduction to Databases
XML Data:
NoSQL Systems
Subscribe to:
Posts (Atom)