Skip to main content

Posts

Showing posts from December, 2014

Difference between null and coalesce

ISNULL ISNULL function is used to replace the NULL value with specified value. It contains only two arguments. Same data type not compulsory. Example -1: SELECT ISNULL (NULL, 'Raki' ) AS [ISNULL] Output:  Raki Example -2: DECLARE @name VARCHAR ( 10 ) DECLARE @marks INT = 500 SELECT ISNULL ( @name , @marks ) AS [ISNULL] Output:  500 Example-3: SELECT ISNULL (NULL,NULL, 'Raki' ) AS [ISNULL]       Output:    Msg 174, Level 15, State 1,    The isnull function requires 2 argument(s). COALESCE Coalesce function is returns first non null value among arguments.    It contains multiple arguments. Same data type compulsory for arguments or precedence data type order should follow. Example-1: SELECT COALESCE (NULL,NULL, 'Raki' ) as [COALESCE] Output:  Raki Example-2: DECLARE @name VARCHAR ( 5 )= 'Raki' DECLARE @mark...

Charindex function in sql server

Charindex is used to find the position of the expression in another expression. Syntax: select charindex ( Exptofind , Exptosearch , [Start_Position] ) Examples: select charindex ( 'a' , 'Rakesh' ) [Position] -- Here 'a' position is 2 select charindex ( 'H' , 'Hello World' ) [Position] -- Here 'H' position is 1 select charindex ( ' ' , 'C# corner' ) [Position] -- Here [Space] position is 3 select charindex ( 'W' , 'C# corner' ) [Position] --Here 'W' position is 0 beacuse there no char 'W' in search Exp select charindex ( 'a' , NULL) -- if any of the exp is null then result also null select charindex (NULL, 'Rakesh' ) -- if any of the exp is null then result also null select charindex ( 'o' , 'Hello World' , 6 ) --Here 6 is the start postion of char to find the Exp Example-2: create table Employee (  Em...

Difference between delete and truncate

Delete Truncate In Delete Query we can use Where Clause In Truncate Query we can not use Where Clause Syntax DELETE FROM [Table_Name] or DELETE FROM [Table_Name] WHERE [Condition] Syntax: TRUNCATE TABLE [Table_Name] Comparing the Delete is Slower than truncate Comparing the Truncate is faster than the delete query In Delete Query we can delete single only or multiple rows by using where clause. In Truncate Query All the rows data truncated from table. We can use triggers when delete query is executed . We can not use triggers when truncate query is executed. Delete query is logs the data. Truncate can not log the data. Delete query delete the data from table using row by row scenario. Truncate query truncate the data from table using page by page scenario...