Thursday, May 8, 2014

How to find whether specific column exists in the given table.. in SQL 2008

-- Query to check the specific column is exists in the table in SQL 2008

if exists (select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='<Table_Name>' and COLUMN_NAME='<Column_Name>')
begin
print 'Column you have specified is exists'
end
else
begin
print 'Column does not exists'
end

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. 


Saturday, April 26, 2014

SQL Query to check number of systems in Database

SQL Query to check the number of databases in the sQL server 2008:

select * from sys.sysdatabases

It will yields following results

| master    1   |
| ------------ |
| tempdb    2  |
| ------------ |
| model    3    |
| ------------ |
| msdb    4    |
| -----------  |

Monday, April 21, 2014

SET NOCOUNT ON



SET NOCOUNT ON


Advantages of   SET NOCOUNT ON – SQL Statement are


  • (It prevents the sending of after process messages to the clients for each of SQL DML or DDL statements executed in a stored procedure.  Actually stored procedures that contain several statements that do not return much actual data, Hence setting above SQL statement to ON can boost the performance as well as Network traffic between clients machines and Server is greatly reduced

  • It will reduce another query to get the how many tables / records are updated once the SQL DML / DDL statements are executed. It will help us to boost the performance of the SQL server also