Sql Server Interview Questions and Answers (1758) - Page 51

What is an alternate way of Converting Integer value to Varchar other that Cast function?

We can also Convert Integer value to Varchar with the help of Convert function.

For Example:-

Declare @Id Int = 123;


Select Convert(Varchar,@Id) as Id;

//OR

Select Convert(Varchar(3),@Id) as Id;


Output will be 123 for both queries.
What is syntax for SQL-Server BETWEEN operator?

SELECT column_name
From table_name
Where column_name
BETWEEN value1 AND value2;

For Example:-

Select employee_name,employee_code

From Employee_Master
Where Employee_Id
Between 200 And 250;

What will be the output of below Sql-server query? SELECT CAST(CAST(205.45 AS varbinary(20)) AS decimal(10,5));

NOTE: This is objective type question, Please click question title for correct answer.
How to get Current time only in Sql-Server?

We can cast Gettime() function to such format,which shows only Current Time

For Example:-
Select CONVERT(VARCHAR(11),GETDATE(),108); 


Here,108 is the Time format as HH:mm:ss
How to get Current Date only in dd-MM-yyyy format in Sql-Server?

We can cast Gettime() function to such format,which shows only Current Date

For Example:-
Select CONVERT(VARCHAR(11),GETDATE(),105);


Here,105 is the Date format as dd-MM-yyyy means 20-12-2013
How to get the list of tables in a database and also the size of which each table occupies?

This is very intresting question where we need to understand on getting the complete list of tables in database is quite easy.

Syntax

USE SAMPLEDATABASE
SELECT * FROM SYS.TABLES

This query will return the complete list of tables in the selected Database.

Now coming to the second part by indentifying the total size of the table that it occupied. Here it is.

Syntax:
EXEC SP_USEDSPACE "Your Table Name goes here"

The above query will return number of rows in the table, Space reserved, actual space occupied and finally the free un-used space in the table.
How to get Date Part only from Date-Time in Sql Server?

We can use Convert in-built sql-server function which extract Date part by passing date,so that we can get only Datepart.

For Example:-

SELECT CONVERT(DATE,GETDATE()) As 'Date_Only';

How to get only Year from Current Date?

We will use Year in-built Sql-Server function to get Year from current date.

For Example:-

Select Year(GetDate()) as 'Current Year';

Which sql statement is used to extract data from data from a Database?

NOTE: This is objective type question, Please click question title for correct answer.
Which sql keyword is used to sort the result-set?

NOTE: This is objective type question, Please click question title for correct answer.
How to get current month only?

With the help of Month in-built sql-server functions,we can get current month. It will return Month in numeric as 1 to 12

For Example:-

select month(getdate()) as 'current month';

Which Sql statement is used for returning unique rows?

NOTE: This is objective type question, Please click question title for correct answer.
What is the output of below sql query? declare @str varchar(50); select ISNULL(@str,'Hello World');

Output would be Hello World.

As IsNull checks 1st arguments,if it's null then returns second argument values.

And in above query @str has no value means @str has not been assigned any value.

so it will return 2nd argument value that is Hello World
What's the difference between DELETE TABLE and TRUNCATE TABLE commands?

1. DELETE TABLE syntax logs the deletes thus making the delete operation slow.TRUNCATE table does not log any information but it logs information about deallocation of data page of the table.

2.DELETE table can be rolled back while TRUNCATE can not be.

3.DELETE table can have criteria while TRUNCATE can not.
How many Clustered Index Table have?

We can have only 1 clustered index in one table.
When we assign primary key to identity column,then by-default clustered index will be created.
When does below error occur? Column names in each table must be unique. Column name 'PROJECT_ISSUE_ID' in table 'ISSUE_REGISTER' is specified more than once.

When there is already a column named PROJECT_ISSUE_ID in table ISSUE_REGISTER and we are again adding it into the same table.
Then we get above error.Because column name must be unique and we can not add same column in a same table.
How to add a column in existing table?

Suppose i have an Employee_Master table and i want to add Column LDAP and Supervisor as

Alter Table Employee_Master 

Add LDAP varchar(10),
Supervisor Int;

How to Add a column,with a default value to an existing table in SQL Server?

Syntax:-
ALTER TABLE {TABLENAME} 

ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}

For Example:-
ALTER TABLE Employee_Master

ADD Employee_Code VARCHAR(20)
CONSTRAINT DF_Employee_Master_Employee_Code DEFAULT '' NOT NULL

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories