Headder AdSence

Ways of column alias name in SQL Server.



As per my knowledge I am using in three ways of giving alias names to the database table column in SQL Server while selecting,

Below are the types which I am using:

1. Giving the alias name in Square Bracket [ ]

2. Giving the alias name with Underscore _

3. Giving the alias name with Double Quotes “ ”
4. Giving Direct Expected Name

1. Square Brackets

          The required column/ header name should be with in the Square Brackets only, with in this bracket we can give spaces also in expected name, so mostly we will use these kinds of brackets when we have space in our expected column name, below is the example.

Example:


SELECT                                                                                                           
  Empno                                         --Original Name of the column   
, Empno AS [Employee Number]--Alias Name of the Column   FROM tbl_Emp

O/P


2. Underscore _

          The required column/ header name should be with Underscore i.e. _, with this underscore only between the two words like Employee and Name, we have to concatenate these two with Underscore I.e. Employee_Name

Example:


SELECT  
  Empno   --Original Name of the column                
, Empno  AS Employee_Number --Alias Name of the Column   FROM tbl_Em            

O/P












3. Double Quotes:

          The required column/ header name should be within the Double Quotes only, with in this double quote we can give spaces also in expected column name, so this also mostly we will use these kinds of Quotes when we have spaces in our expected column name, below is the example.

Example:


SELECT                                                                                                           
  Empno --Original Name of the column                                           
, Empno AS "Employee Number"--Alias Name of the Column   FROM tbl_Emp                                                                                            

O/P













4. Direct Expected Name:

          This like directly whatever you what you can give, there is no such condition like above, but here we can’t write column name with spaces, it will work only for single world column name

Example:


SELECT                                                                                                            
  Empno                                         --Original Name of the column   
, Empno AS "EmployeeNumber"--Alias Name of the Column  
FROM tbl_Emp                                                                                             

O/P











Please comment below

Department (Dept) Sample Data


Please Copy the below Query and run on your SSMS.


/* Droping the Existing Dept Table: */
DROP TABLE IF EXISTS tbl_Dept;

/* Creating New Dept Table: */
CREATE TABLE tbl_Dept(
tbl_Deptno INT NOT NULL PRIMARY KEY
, DName VARCHAR(50) NOT NULL
, Location VARCHAR(50) NOT NULL);

/* Inserting Data into Dept Table: */
INSERT INTO tbl_Dept VALUES 
 (10,'Accounting','New York')
,(20,'Research','Dallas')
,(30,'Sales','Chicago')
,(40,'Operations','Boston');

/*cSeleting Inserted Data */
SELECT * FROM tbl_Dept

Click Here for : Employee Table Sample Data

Please comment below


Employee (Emp) Sample Data


Please Copy the below Query and run on your SSMS.


/*Please relook once you before Drop the Existing Table: */
DROP TABLE IF EXISTS tbl_Emp;

/*Creating New Employee Table*/
CREATE TABLE tbl_Emp(
  Empno INT NOT NULL PRIMARY KEY
, Ename VARCHAR(50) NOT NULL
, Job VARCHAR(50) NOT NULL
, Mgr INT
, Hiredate DATE
, Sal DECIMAL(10,2)
, Comm DECIMAL(10,2)
, Deptno INT NOT NULL);


/*Inserting Data Into above created Table: */
INSERT INTO tbl_Emp (Empno,Ename,Job,Mgr,Hiredate,Sal,Comm,Deptno)
VALUES
  (7369, 'SMITH', 'CLERK', 7902, '6/13/93', 800, 0.00, 20)
, (7499, 'ALLEN', 'SALESMAN', 7698, '8/15/98', 1600, 300, 30)
, (7521, 'WARD', 'SALESMAN', 7698, '3/26/96', 1250, 500, 30)
, (7566, 'JONES', 'MANAGER', 7839, '10/31/95', 2975, null, 20)
, (7698, 'BLAKE', 'MANAGER', 7839, '6/11/92', 2850, null, 30)
, (7782, 'CLARK', 'MANAGER', 7839, '5/14/93', 2450, null, 10)
, (7788, 'SCOTT', 'ANALYST', 7566, '3/5/96', 3000, null, 20)
, (7839, 'KING', 'PRESIDENT', null, '6/9/90', 5000, 0, 10)
, (7844, 'TURNER', 'SALESMAN', 7698, '6/4/95', 1500, 0, 30)
, (7876, 'ADAMS', 'CLERK', 7788, '6/4/99', 1100, null, 20)
, (7900, 'JAMES', 'CLERK', 7698, '6/23/00', 950, null, 30)
, (7934, 'MILLER', 'CLERK', 7782, '1/21/00', 1300, null, 10)
, (7902, 'FORD', 'ANALYST', 7566, '12/5/97', 3000, null, 20)
, (7654, 'MARTIN', 'SALESMAN', 7698, '12/5/98', 1250, 1400, 30);


/*Selecting Data from Table*/
select * from tbl_Emp


Please comment below