Friday, 28 August 2015

Different ways to replace NULL in SQL SERVER

Different ways to replace NULL in SQL SERVER

Consider the Employees table below.


Write a LEFT OUTER SELF JOIN query, which produced the following output.


In the output, MANAGER column, for Todd's rows is NULL. I want to replace the NULL value, with 'No Manager'

Replacing NULL value using ISNULL() function: We are passing 2 parameters to IsNULL() function. If M.Name returns NULL, then 'No Manager' string is used as the replacement value.

SELECT E.Name as Employee, ISNULL(M.Name,'No Manager') as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID

Replacing NULL value using CASE Statement:
SELECT E.Name as Employee, CASE WHEN M.Name IS NULL THEN 'No Manager'
   ELSE M.Name END as Manager
FROM  tblEmployee E
LEFT JOIN tblEmployee M
ON   E.ManagerID = M.EmployeeID

Replacing NULL value using COALESCE() function: COALESCE() function, returns the first NON NULL value :
SELECT E.Name as Employee, COALESCE(M.Name, 'No Manager') as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID

Thursday, 30 July 2015

SQL Server PRINT Keyword

SQL Server PRINT Keyword

To display something in plain text as a result of a statement, type PRINT followed by what to display. Therefore, PRINT uses the following formula:

PRINT 'WhatToPrint'

The item to display can also be an operation or the result of an operation. If you want to display a character, a word, or a sentence, include it between single-quotes. If you want to include a single-quote in your statement, double it;


Thursday, 23 July 2015

How to convert varchar data type column to int in SQL Server while querying?

How to convert varchar data type column to int in SQL Server while querying?

There are two ways to convert varchar data type column to int while writing query statements.
  1. CAST
  2. CONVERT
Examples :

CONVERT(int,discountfee)
CAST(discountfee as int)

Here discountfee is column name and int declared as data type for column varchar data type discountfee.

Friday, 17 July 2015

SQL Server NULLIF function

SQL Server NULLIF function

The NULLIF function takes two arguments. If the two arguments are equal, then NULL is returned. Otherwise, the first argument is returned. The syntax for NULLIF is as follows:

NULLIF ("expression 1", "expressions 2")

It is the same as the following CASE statement:

SELECT CASE ("column_name")
  WHEN "expression 1 = expression 2 " THEN "NULL"
  [ELSE "expression 1"]
  END
FROM "table_name";

For example, let's say we have a table that tracks actual sales and sales goal as below:
Table Sales_Data

Store_Name
Actual
Goal
Store A
50
50
Store B
40
50
Store C
25
30

We want to show NULL if actual sales is equal to sales goal, and show actual sales if the two are different. To do this, we issue the following SQL statement:

SELECT Store_Name, NULLIF (Actual, Goal) FROM Sales_Data;

Result:
Store_Name      NULLIF (Actual, Goal)
Store A                                 NULL
Store B                                     40

Store C                                     25

SQL Server USE Keyword

SQL Server USE Keyword

The USE keyword is used to select a database in SQL Server. The syntax is as follows:
USE "database_name";

For example, if you want to connect to a database called "World", you can type in the following:

USE World;

Wednesday, 1 July 2015

SQL Server: TOP WITH TIES

SQL Server: TOP WITH TIES

TOP clause is commonly used to get top required rows from a result set. Beauty of this clause is that it can be used with WITH TIES clause, to retrieve all similar rows to base result set.

Syntax :
[
      TOP (expression) [PERCENT]
      [ WITH TIES ]
]
Arguments:

expression: is the numeric expression that specifies the number of rows to be returned. expression is implicitly converted to a float value if PERCENT is specified; otherwise, it is converted to bigint.

If the query includes an ORDER BY clause, the first expression rows, or expression percent of rows, ordered by the ORDER BY clause are returned. If the query has no ORDER BY clause, the order of the rows is arbitrary.

PERCENT: indicates that the query returns only the first expression percent of rows from the result set.

WITH TIES: specifies that additional rows be returned from the base result set with the same value in the ORDER BY columns appearing as the last of the TOP n (PERCENT) rows. TOP...WITH TIES can be specified only in SELECT statements, and only if an ORDER BY clause is specified.

Example:

DECLARE @TEST TABLE(
ID int, Amount int)
INSERT INTO @TEST VALUES
(1,10),(2,30),(3,60),
(4,50),(5,50),(6,70),
(7,20),(8,70),(9,70),
(10,44),(11,80),(12,90)


Below is the result-set using TOP without and with clause WITH TIES.


Saturday, 6 June 2015

Shadow Copy In SQL Server Without Using Create Command

Shadow Copy In SQL Server Without Using Create Command

Various methods of copying a table with a new name using a select statement in SQL Server. It takes a lots of time to create new empty table and defining all field names again. Various methods are explained below.
Creating Table in SQL Server
create table StudentDetails
(
roll_num int,
F_Name varchar(15),
L_Name varchar(15),
City varchar(15)
)

Insert values in above table and table will look like below image


Method I
Select all columns into new table but this way of copying will not copy constraints and indexes. This technique will copy all the columns of the source table (StudentDetails) to the destination table (StudentDetailsCopy).


Method II
Select only the columns we want into the new table. Suppose we want to copy roll_num, F_Name, City columns from StudentDetails table into the StudentDetailsCopy1 table. Constraints and indexes will not be copied.


Method III
To copy the structure of the source table into another new table with new name. Constraints and indexes will not be copied.


Here, 1=2 will prevent the data from being copied from StudentDetail to the StudentDetailCopy2 table.