Sunday, 25 February 2018

Example of Left, Right, Charindex, Patindex using Sql Server

like left
select substring(jobdesc, 0, charindex('Teachers are not eligible for overtime because', jobdesc)) from Order_Gen where Client=48670

--1.Right
select RIGHT(CEM.ReimbHospital,CHARINDEX('-',REVERSE(CEM.ReimbHospital)) - 1) from tblClaimsEntryMaster cem  WHERE cem.ClaimNo=234006607

--1.Left
select Left(CEM.ReimbHospital,CHARINDEX('-',(CEM.ReimbHospital)) - 1) from tblClaimsEntryMaster cem  WHERE cem.ClaimNo=234006607

--2.Right
select right(policyname, len(policyname)-charindex('-',policyname)) from tblpolicy
select RIGHT('policy-12',9-7)
--ans->12

--2.Left
select left(policyname, charindex('-',policyname)-1 ) from tblpolicy
select LEFT('policy-12',7-1)
--ans->policy

--it returns one value
select left(ClientName,CHARINDEX(' ',ClientName)-1) from  tblClientMaster where ClientNo=89 or ClientNo=71

select left('Client Name',CHARINDEX(' ','Client Name')-1)
--it returns the word frm left when see the space

select right('Client dName',CHARINDEX(' ','Client dName')-1)
--it returns the word frm right when see the space

--it returns multiple value
select left(ClientName,CHARINDEX(' ',ClientName,1)) from  tblClientMaster
--it returns 1st word frm left

select right(ClientName,CHARINDEX(' ',ClientName,1)) from  tblClientMaster
--it returns 1st word frm right--wrong

select CHARINDEX(' ','Client Name') --it return position of space-7

--It returns 3 letters from left and right
select left(ClientName,3) from  tblClientMaster
select right(ClientName,3) from  tblClientMaster

-- I need to get the last RIGHT 3 characters and I need to get whatever is to the LEFT
select right(ClientName,3),LEFT(ClientName,LEN(ClientName)-3) from  tblClientMaster

-- finds the position at which the pattern ensure starts in a specific row of the Clientname column in the Document table in the AdventureWorks2012 database.
select PATINDEX('%MANSARD%',Clientname) from tblClientMaster

SELECT PATINDEX ( '%ein%', 'Das ist ein Test')
SELECT PATINDEX('%en_ure%', 'please ensure the door is locked')

No comments:

Post a Comment