Thursday, May 1, 2014

Difference between cast and convert in MS SQL



Difference between cast and convert


Syntax of cast and convert is as follows,

(    (1)   Select cast(<varchar-value> as int) from Table
(    (2)   Select Convert(int, <varchar-value>) from Table

There are two types of conversion of data format from one format to another, they are implicit and explicit,
Implicit way of conversion includes the server automatically converts the data without requiring the user to input any external data or coding.
Explicit way of conversion is where server accepts user input to covert the data to his desired format.


use temp1
create table dbo.custr (cuid int, custaddress varchar(50))
go
insert into dbo.custr values (1, '560056')


select CAST(custaddress as int)as value from dbo.custr

select CONVERT(int,custaddress) as value from dbo.custr



In above example column custaddress is having datatype of varchar which is of length of 50, we extracted the data in the format of int by using cast and covnert functions.

Datetime and Datetime2 in MS Sql Server 2008 R2



Datetime and Datetime2 in MS Sql Server 2008 R2
---------------------------------------------------------------------- 


Datetime2 is a datatype in MS SQL server 2008 to store the date and time values. Datetime is not ansi complaint


DateTime
DateTime2
Min. Value
1753-01-01 00:00:00
0001-00-00 00:00:00
Max Value
9999-12-31 23:59:59.997
9999-12-31 23:59:59.9999999
Storage Size
8 bits
6 to 8 bits
Declaration Method
Declare @now datetime
Declare @now datetime2(7)

(1)    To find the current system date by using datetime datatype:

Declare @now datetime
@now = getdate()

(2)    To find the current system date by using datetime2 datatype:

Declare @now datetime2(7)
@now = sysdatetime()
(3)    To find the length of the data type


declare @now datetime2(2)
set @now=sysdatetime()
select DATALENGTH(@now)


Use of Set ANSI_NULL ON / OFF in SQL Server



Use of Set ANSI_NULL ON / OFF in SQL Server


Hint: It is used while creating user defined functions, stored procedures and etc

Let we create table with name of customers,
CODE
Create table dbo.customer (CustId varchar(5), CustFirstName varchar(25), CustLastName varchar(25))
Go
Insert into dbo.customer (CustId, CustFirstName, CustLastName) values ('CU001', 'SURESH', 'BABU'), ('CU002', 'RAVI', 'KUMAR'), ('CU003', 'AVINASH', NULL), ('CU004', 'MAHESH', NULL)

Now run below SQL Query to extract the values in the table of dbo.customer
set ansi_nulls on
select * from dbo.customer where CustLastName = null

It will yields “0” results like
CustId
CustFirstName
CustLastName

And in the Message column it shows
(0 row(s) affected)

Again you run with another code but little modification
set ansi_nulls off
select * from dbo.customer where CustLastName = null

it will yields you following results,
CustId
CustFirstName
CustLastName
CU003
AVINASH
NULL
CU004
MAHESH
NULL

And in the message column,

(2 row(s) affected)
Hint: If it is ansi_nulls on
We need to use following code to extract records with null values
set ansi_nulls on
select * from dbo.customer where CustLastName is null

or

set ansi_nulls on
select * from dbo.customer where CustLastName is not null


Summary: When this setting value is ON (i.e. SET ANSI_NULLS ON) then comparison with NULL value using = and <> comparison operator will return false.