Tuesday, 29 March 2016

How to get the ID of the selected database in SQL server ?

To get the ID of the selected database refer the following syntax 

Syntax:

DB_ID()

Example:

SELECT DB_ID()

Output:

5

Open a new query window ,select the required database and Now execute the above function,then you will get the ID of the selected database.

How to get the database ID in SQL Server ?

In a Single SQL Server Instance ,it is possible to have multiple databases.
Each database has its own unique ID.

To get the ID of the database refer the following syntax 

Syntax:

DB_ID('Database Name')

Example:

SELECT DB_ID('Sample')

Output:

5

Now the ID of the Sample database is 5.


Use of SUBSTRING Function in SQL Server

SUBSTRING function is used to select the Part of the String in a Expression 

Syntax : 

SUBSTRING('STRING EXPRESSION','START','LENGTH')

Example :

SELECT SUBSTRING('JK SQL SEVER BLOG',4,20)

Output :

SQL SERVER BLOG

In above example ,the substring function will select the string from 4th character to 20th character.
Using this function we can select the required part of the string in a expression.

Saturday, 26 March 2016

Use Of Replace Function in SQL Server

Replace function is used to replace the exact occurrence of the String in a Expression 

Syntax : 

REPLACE '[STRING EXPRESSION]','[STRING TO FOUND]',[REPLACEMENT STRING]

Example :

SELECT REPLACE('JK SQL SEVER BLOG','SEVER','SERVER') 

In above example 'SEVER' will be replaced with replacement string 'SERVER' in the string expression.

Note : Replace function will replace all the exact occurrence of the string expression
rather than replacing particular string occurrence.

Thursday, 24 March 2016

What is ISNULL function in SQL Server ?

Syntax : 
ISNULL (ColumnName,Repalcement Value)


Example :
Select IsNull(FirstName,LastName) from EmployeeDetail

In EmployeeDetail table the FirstName and LastName are Nullable columns.
If the FirstName column is null ,we can get the value of LastName column using Isnull function.

In above example ,the select query will display the LastName of all employees unless the firstname column value is null.

It is possible to use the  isnull function in both "select statement" as well as "where" condition.