Skip to main content

Posts

Using PATINDEX Function when Creating the Table

Normally Varchar , Nvarchar , Char these data types in sql server it allows character data and numeric data also ,but in some cases we don't want allow numeric data in some columns at this time we use patindex function with check constraint at time creating table or altering column in sql server. Step-1 : Creating Student table with patindex function. CREATE   TABLE   Student ( Id   INT   IDENTITY (1,1)   PRIMARY   KEY , StudentName   VARCHAR (100)   NOT   NULL   , LivingLocation   VARCHAR (100)   NULL, constraint   chk_studentname Check   ( PATINDEX ( '%[0-9]%' ,   StudentName)=0) ) GO ; Step-2 : Insert Some Sample Data. INSERT   INTO   Student SELECT   'Raki' , 'Hyderabad' GO ; The above query executed successfully. INSERT   INTO   Student SELECT   'Raki123' , 'Hyderabad' GO ; The above query not executed successfully,because n...

PATINDEX FUNCTION IN SQL SERVER

Returns the starting position of first occurrence of pattern in a specified  expression . Syntax: Select Patindex(pattern char,expression char) returns int Note: It returns the integer value. It returns the zero (0) when you are specified position is not matched in the expression. Here is the simple string with postions for using entire examples in the Patindex Function. 1 2 3 4 5 6 7 8 9 10 11 12 L E A D F I R S T S Q L Example-1: SELECT PATINDEX ( '%a%' , 'LEADFIRSTSQL' ) [PATINDEX_POSITION] Example-2: SELECT PATINDEX ( '%S%' , 'LEADFIRSTSQL' ) [PATINDEX_POSITION] Example-3: SELECT PATINDEX ( '%_R_%' , 'LEADFIRSTSQL' ) [PATINDEX_POSITION] Example-4: SELECT PATINDEX ( '%[abcde]%' , 'LEADFIRSTSQL' ) [PATINDEX_POSITION] Here is the example any of the character ...

Simple Trick Using ISNULL

Simple Trick Using ISNULL: Step-1: Create Sample Student Table. CREATE TABLE Student ( Id INT IDENTITY ( 1 , 1 ) PRIMARY KEY , StudentName VARCHAR ( 100 ) NOT NULL, LivingLocation VARCHAR ( 100 ) NULL ) Step-2 :Insert Some Sample Data INSERT INTO Student SELECT 'Rakesh' , 'Hyderabad' UNION ALL SELECT 'Raju' , 'Delhi' UNION ALL SELECT 'Madhu' , 'Hyderabad' UNION ALL SELECT 'Naresh' ,NULL UNION ALL SELECT 'Venaktesh' , 'Chennai' Step-3 : Select Data from Student Table SELECT * FROM Student If we observe above data of student table we have student id 4 record LivingLocation is NULL. Step -4 :Select Data from Student Table With Where Clause. SELECT * FROM Student WHERE LivingLocation = 'Hyderabad' In the above query we are included the where clause w...

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...