SQL Selecting and Replacing Function

 LEFT ()

Right()

SUBSTRING()

Syntax to select first letters from a string starting left or right


SELECT 

first_name,3 last_name,

country,

    -- Select only the first 3 characters from the first name

LEFT(first_name,3) AS part1

FROM voters;



SELECT 

email,

    -- Extract 5 characters from email, starting at position 3

SUBSTRING(email, 3, 5) AS some_letters

FROM voters;

2]

DECLARE @sentence NVARCHAR(200) = 'Apples are neither oranges nor potatoes.'

SELECT

-- Extract the word "Apples" 

SUBSTRING(@sentence, 1, 6) AS fruit1,

    -- Extract the word "oranges"

SUBSTRING(@sentence, 20, 7) AS fruit2;




Replace Function -


SELECT 

company AS initial_name,

    -- Replace '&' with 'and'

REPLACE(company,'&','and') AS new_name 

FROM ratings

WHERE CHARINDEX('&', company) > 0;

Comments

Popular posts from this blog

Binomial Test in Python

Two - Sample T test

Web Scraping using JSON and API