SQL - Dia 7

technonotes-hacker - Nov 1 - - Dev Community

Single Row Function

Image description

Image description

  • 7 types
  • Case Manipulation Function : upper , lower & Initcap
  • Character Manipulation : Instr , Substr , Length , Replace , Reverse , Translate , Trim , Ltrim , Rtrim , Lpad & Rpad.
  • Number
  • Date Function
  • General Function
  • Null
  • Conversion

Initcap

  • First letter in caps

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

> -- select * from HR.COUNTRIES;
> -- select * from HR.COUNTRIES where COUNTRY_NAME='BRAZIL'
> -- select * from HR.COUNTRIES where COUNTRY_NAME=Initcap('BRAZIL')
> -- select *, UPPER(COUNTRY_NAME) from HR.COUNTRIES where COUNTRY_NAME=Initcap('BRAZIL')
> -- select HR.COUNTRIES.*, UPPER(COUNTRY_NAME) from HR.COUNTRIES where COUNTRY_NAME=Initcap('BRAZIL')
> -- select HR.COUNTRIES.*, UPPER(COUNTRY_NAME) AS COUNTRY from HR.COUNTRIES where COUNTRY_NAME=Initcap('BRAZIL')
> select C.*, UPPER(COUNTRY_NAME) AS COUNTRY from HR.COUNTRIES C where COUNTRY_NAME=Initcap('BRAZIL')
Enter fullscreen mode Exit fullscreen mode

Instr

  • Tells the number of the Position.
  • To find Letter position.
  • Number as Output.
  • Number wont change when to count from front or reverse.
  • 1 -- > Front
  • -1 --> Reverse
  • Last one in the bracket is *OCCURRENCE *.
  • instr(letter, which one you need , front or reverse , occurrence )

Image description

Image description

Image description

-- select 'APPLEE' FROM dual;
-- select INSTR('APPLEE','P',1,1) FROM dual;
select INSTR('APPLEE','P',1,2) FROM dual;
select INSTR('APPLEE','P',-1,2) FROM dual;
Enter fullscreen mode Exit fullscreen mode

Substr

  • Character as output.
  • substr ( 'letter' , start , how many character/letter u need to be displayed )
  • substr ( 'letter', 2 ) --> it will print all letters after 2.
  • '-' reverse.

Image description

select substr('APPLEE',3,2) FROM dual;
select substr('APPLEE',-3,2) FROM dual;
Enter fullscreen mode Exit fullscreen mode

Length

  • To find the length of the character.
  • For NULL there is no length.
  • Space will be considered.

Image description

Image description

Image description

-- select length('brettlee') from dual;
-- select DEPARTMENT_NAME, length(DEPARTMENT_NAME) as no_of_letters from hr.DEPARTMENTS;
select '',length(''),length(null) from dual;
select '',length(' '),length(null) from dual;
Enter fullscreen mode Exit fullscreen mode

Reverse

  • Numbers,it won't accept for reverse function.

Image description

Replace

  • replace the word.
  • replace('word','exact_word','replace_with_this');
  • If the second argument is not present in the first then replace won't work.
  • word to word we need to use 'Replace'.
  • character/letter to character/letter use 'Translator'.

Image description

Image description

Image description

Image description

Image description

-- select replace('hcl technology','technology',techno) from dual;
-- select replace('hcl technology','technology','techno') from dual;
-- select replace('hcl technology','soft','techno') from dual;
-- select replace('hcl technology','technology') from dual;
select translate('hcl','hl','ts') from dual;
Enter fullscreen mode Exit fullscreen mode

Trim

  • space can be trimmed.

Image description

Image description

Image description

Image description

Lpad

  • to add.
  • To add in both sides that's the task.

Image description

Image description

Image description

Image description

-- select lpad((rpad('infosys','9','&')),'11','&') from dual;
select rpad(lpad('INFOSYS',length('INFOSYS')+2,'*') ,length(lpad('INFOSYS',length('INFOSYS')+2,'**'))+2,'*') from dual;
Enter fullscreen mode Exit fullscreen mode

Notes

  • dual is a dummy table
  • always single quotes for string.
  • Data in the table is sensitive --> always remember.
  • Query you can write in any format but data is sensitive.
  • Can we put , after * ? Yes , you need to put the table name in the * then there wont be error.
  • AS is the column name or alternate name.
  • AS can be used only in the select column.
  • AS can't be used for table.
  • Alias name won't be saved in database.
  • Alias = Alternate Name
  • String = Group of letters
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .