Task –5
IS NULL ():
ISNULL() replaces NULL with a specified replacement value and if it is not null, it wont replace.
Code:
select e.Name as Employee_name,ISNULL(M.name,'No Manager') As
Manager_name
from Employee e left join Employee m
on e.Managerid=m.Employeeid;
CASE Statement:
Performs conditional logic and returns a value based on whether a condition is true or false.
Syntax used with it are WHEN and THEN.
Code:
select e.name as Employee_name, case when m.name is null then
'No Manager'
else m.Name end as Manager_name
from Employee e left join Employee m
on e.Managerid=m.Employeeid;
COALESCE():
Returns the first non-NULL value from a list of expressions. Very much useful when we deal
with missing data and want to provide fallback options.
Code:
select e.Name as Employee_name,coalesce(M.name,'No Manager') As
Manager_name
from Employee e left join Employee m
on e.Managerid=m.Employeeid;
STRING FUNCTIONS:
String functions are used in data transformation , pattern analyzing , etc,.
➢ ASCII(char) – Returns the ASCII code of the first character in the input string.
➢ CHAR(int) – Returns the character corresponding to the ASCII code provided.
➢ LTRIM(string) – Removes leading spaces from a string.
➢ RTRIM(string) – Removes trailing spaces from a string.
➢ UPPER(string) – Converts all characters in the string to uppercase.
➢ LOWER(string) – Converts all characters in the string to lowercase.
➢ REVERSE(string) – Reverses the order of characters in a string.
➢ LEN(string) – Returns the number of characters in a string (excluding trailing
spaces).
REPLICATE,REPLACE,STUFF ,PATINDEX :
➢ REPLICATE(string, count) – Repeats a string a specified number of times.
➢ SPACE(n) – Returns a string of n spaces.
➢ PATINDEX('%pattern%', string) – Returns the starting position of a pattern in a string
using wildcards.
➢ STUFF(string, start, length, new_string) – Deletes a part of a string and inserts new text
at a specified position.
➢ REPLACE(string, old, new) – Replaces all occurrences of a substring with another
substring.