0% found this document useful (0 votes)
5 views

SQL - STRING - FUNCTIONS - 2

Uploaded by

Aparna Panya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL - STRING - FUNCTIONS - 2

Uploaded by

Aparna Panya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

--CHARINDEX(<FindString>, <WithInString>, <StartingPosition>): It is used get

specified character position


--It returns number.

--Write a SQL Statement to get FirstAPosition, SecondAPosition and FirstCPosition


SELECT
'soma sekhar' AS Name,
CHARINDEX('a', 'soma sekhar', 1) AS First_a_Position,
CHARINDEX('a', 'soma sekhar', CHARINDEX('a', 'soma sekhar', 1)+1) AS
Second_a_Position,
CHARINDEX('c', 'soma sekhar', 1) AS First_c_Position

--Apply above logic on Users table


SELECT
*,
CHARINDEX('a', UserName, 1) AS First_a_Position,
CHARINDEX('a', UserName, CHARINDEX('a', UserName, 1)+1) AS Second_a_Position,
CHARINDEX('c', UserName, 1) AS First_c_Position
FROM Users

--Write a SQL Statement to get FirstName from UserName


SELECT
UserName,
LEFT(UserName, CHARINDEX(' ', UserName, 1)-1) AS FirstName
FROM Users

--Write a SQL Statement to get LastName from UserName with RIGHT function
--Note: Total Number of characters from UserName Minus SpacePisition from UserName is
Number Of Characters for LastName
SELECT
UserName,
LEN(UserName)-CHARINDEX(' ', UserName, 1) AS TotalLenMinusSpacePosition,
RIGHT(UserName, LEN(UserName)-CHARINDEX(' ', UserName, 1)) AS LastName
FROM Users

--Write a SQL Code To get FirstName and LastName from UserName with SUBSTRING()
SELECT
UserName,
SUBSTRING(UserName, 1, CHARINDEX(' ', UserName, 1)-1) AS FirstName,
SUBSTRING(UserName, CHARINDEX(' ', UserName, 1)+1, LEN(UserName)-CHARINDEX(' ',
UserName, 1)) AS LastName
FROM Users

/*
CONCATE_WS(delimiter/separator, text1, text2...): It is also used to concatenate two
or more strings with common delimiter
--Write a SQL Code to show FullName by concatenating FirstName and LastName

*/
USE ContosoRetailDW
SELECT
FirstName,
LastName,
CONCAT_WS('*', FirstName, LastName) AS FullName
FROM DimCustomer
--Write a SQL Code to get FullName by concatenationg FirstName, MiddleName and
LastName
SELECT
FirstName,
MiddleName,
LastName,
CONCAT_WS('*', FirstName, MiddleName, LastName) AS FullName
FROM DimCustomer

--Write a SQL Code to get FullName by concatenationg FirstName, MiddleName and


LastName
--Note: Use case statement to get FullName with CONCAT Function
SELECT
FirstName,
MiddleName,
LastName,
CASE
WHEN MiddleName IS NULL THEN CONCAT(FirstName, '*', LastName)
ELSE CONCAT(FirstName, '*', MiddleName, '*', LastName)
END AS FullName1,
CONCAT_WS('*', FirstName, MiddleName, LastName) AS FullName2,
CONCAT(FirstName, '*', MiddleName, '*', LastName) AS FullName3
FROM DimCustomer

--Write a SQL Statement to get User Alias and User Domain from DimCustomer
SELECT
CustomerKey,
EmailAddress
FROM DimCustomer

/*
REPLACE(Text, FindText,ReplaceText): It is used to replace text with specified text
*/

--Write a SQL Statement to replace space with * from UserName


USE DATASTORE
SELECT
UserName,
REPLACE(UserName, ' ', '*') AS Replace_Space_With_Star
FROM Users

--Write a SQL Statement to repace a with @ from UserName


SELECT
UserName,
REPLACE(UserName, 'a', '@') AS Replace_a_with_@
FROM Users

--Write a SQL Statement to find number of characters from text without a characters
SELECT
UserName,
LEN(UserName) AS LenOfUserName,
LEN(REPLACE(UserName, 'a', '')) AS LenWithOuta,
LEN(UserName)-LEN(REPLACE(UserName, 'a', '')) AS TotalNoOfas
FROM Users
/*
TRIM(text): It is used to trim from starting of the text and ending of the text
LTRIM(text): It is used to trim from only starting of the text
RTRIM(text): It is used to trim from only ending of the text
*/

DROP TABLE IF EXISTS empdata

CREATE TABLE empdata(


empno INT,
ename VARCHAR(50)
)

SELECT * FROM empdata

--Feed sample data


INSERT INTO empdata VALUES
(101, ' Bhaskar'),
(102, 'Raja '),
(103, 'Smith'),
(104, ' Sandy ')

SELECT
empno,
ename,
LEN(ename) AS LenOfEname,
TRIM(ename) AS trim_ename,
LEN(TRIM(ename)) AS lenAfterTrim,
LTRIM(ename) AS ltrim_ename,
LEN(LTRIM(ename)) AS lenAfterLtrim,
RTRIM(ename) AS rtrim_ename,
LEN(RTRIM(ename)) AS lenAfterRtrim
FROM empdata

You might also like