Search

Friday, February 17, 2012

String comparision

There are various method through which we can compare string.


SELECT AccountName FROM AccountMaster WHERE AccountName='REKHA'

SELECT AccountName from AccountMaster WHERE AccountName LIKE '%REKHA%'

SELECT AccountName from AccountMaster WHERE AccountName LIKE '%_REKHA_%'

SELECT AccountName from AccountMaster WHERE AccountName LIKE '%[OO]_%'
SELECT * FROM AccountMaster

INSERT INTO AccountMaster SELECT('SMANJAYQAZXSW')
SELECT * FROM AccountMaster
SELECT * FROM AccountMaster WHERE AccountName IN('SANJAY','REKHA', 'NIKITA')

SELECT AccountName FROM AccountMaster WHERE AccountName LIKE 'S%[NJ]%_Y'

This query searches BETWEEN Start TO End withing a string.

SELECT AccountName FROM AccountMaster WHERE AccountName LIKE 'S____Y'


This one will validate the format of a string.  As shown it will return "Valid".  Change the "8" to an "A" and you get "Invalid":
Declare @string VarChar(50) = '741ZVM543'
SELECT CASE WHEN @string LIKE '[0-9][0-9][A-Z][A-Z][0-9][0-9][0-9]' THEN 'Valid' ELSE 'Invalid' END


This one will find the first upper-case character in a string (it returns "7"):
Declare @string VarChar(50) = 'white Chem'
SELECT PatIndex('%[A-Z]%',@string collate Latin1_General_BIN)

No comments:

Post a Comment