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

SQL - STRING - FUNCTIONS - 1

Uploaded by

Aparna Panya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL - STRING - FUNCTIONS - 1

Uploaded by

Aparna Panya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

**************************************************************************************
***
String Functions
**************************************************************************************
***
String Functions are used to deal with text data.
Text data will be enclosed in single quotes(')
1. ASCII(<Character>): It is used to get ASCII value of a specific character
2. CHAR(<Number>) : It is used to get character of a number
3. CONCATE(Text1, Text2, ....TextN): It is used to combine text values
4. LEFT(<Text>, <No:Of Characters>: It is used to get part the of the text from left
side with specifified number
of characters
5. RIGHT(<Text>, <No:Of Characters>: It is used to get part the of the text from right
side with specifified number
of characters
*/

--Write a SQL Code to get ASCII value for 'A' and 'a'
SELECT
'A' AS Column1,
ASCII('A') AS ASCII_A,
'a' AS Column2,
ASCII('a') AS ASCII_a

--Write a SQL Code to get Character value for 65 and 97


SELECT CHAR(65) AS Char1, CHAR(97) AS Char2

--Note: When you write Dynamic SQL code, we can use this function to get single quotes

--Write a SQL code to show St.An's school

SELECT 'St.An' + CHAR(39) + 's School'

SELECT
'SELECT * FROM employee
WHERE Job=' + CHAR(39) + 'MANAGER' + CHAR(39) AS Query

SELECT * FROM employee WHERE Job='MANAGER'

--Write a SQL Statement to get FullName by taking data form FirstName and LastName
from ContosoRetailDW, DimCustomer table
USE ContosoRetailDW
SELECT
FirstName,
LastName,
CONCAT(FirstName, ' ', LastName) AS FullName
FROM DimCustomer

/*
Write a SQL Statement to get Full Name, with FirstName, MiddleName, LastName.
Note: If MiddleName is Null then your FullName is FirstName and LastName else
FirstName, MiddleName and LastName
We can provide solution in two ways
1. By using Case Statement
*/
SELECT
FirstName,
MiddleName,
LastName,
CASE
WHEN MiddleName IS NULL THEN CONCAT(FirstName, '*', LastName)
ELSE CONCAT(FirstName, '*', MiddleName, '*', LastName)
END AS FullName
FROM DimCustomer

---

/*
4. LEFT(<Text>, <No:Of Characters>): It is used to get part the of the text from left
side with specifified number
of characters
--Write a SQL Statement to get First Three characters from every employee name
*/

USE DATASTORE
SELECT
EmployeeNo,
Ename,
LEFT(Ename, 3) AS FirstThreeChars
FROM employee

/*
5. RIGHT(<Text>, <No:Of Characters>): It is used to get part the of the text from
right side with specifified number
of characters

--Write a SQL Statement to get Last Three characters from every employee name
*/
SELECT
EmployeeNo,
Ename,
RIGHT(Ename, 3) AS LastThreeChars
FROM employee

/*
6. SUBSTRING(<Text>, <Starting Position>, <No:Of Characters>): It is used to get part
of the text from
starting position to specified number of characters.
--Write a SQL Statement to get First Three Characters from employee table with
Substring fucntion
*/
SELECT
EmployeeNo,
Ename,
SUBSTRING(Ename, 1, 3) AS FirstThreeChars
FROM employee
--Write a SQL Statement to get Last Three Characters from employee table with
Substring fucntion
--LEN(<Text>): It is used to get complete length of the text
--ex: LEN('Raja')-->4
SELECT
EmployeeNo,
Ename,
LEN(Ename) AS LenEname,
LEN(Ename)-2,
SUBSTRING(Ename, LEN(Ename)-2, 3) AS LastThreeChars1,
RIGHT(Ename, 3) AS LastThreeChars2
FROM employee

--Write a SQL Statement to create table by using SELECT statement


SELECT TOP 20
CustomerKey AS UserID,
CONCAT(FirstName, ' ', LastName) AS UserName INTO Users
FROM ContosoRetailDW.dbo.DimCustomer

--Reading data from Users table


SELECT * FROM Users

--Write a SQL Statement to get FirstName from UserName


SELECT
UserId,
UserName,
LEFT(UserName, 3) AS FirstName
FROM Users

You might also like