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

SQL Functions

SQL functions like ABS, CURRENT_TIMESTAMP, LENGTH, INTERVAL, SUBSTRING, and AVG are described. ABS returns the absolute value, CURRENT_TIMESTAMP returns the current date and time, LENGTH returns the number of characters in a string, INTERVAL adds or subtracts days from a date, SUBSTRING extracts part of a string, and AVG calculates the average of a set of values. Compatibility and variations for functions across databases like Oracle, SQL Server, MySQL, and PostgreSQL are also noted.

Uploaded by

Vignesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

SQL Functions

SQL functions like ABS, CURRENT_TIMESTAMP, LENGTH, INTERVAL, SUBSTRING, and AVG are described. ABS returns the absolute value, CURRENT_TIMESTAMP returns the current date and time, LENGTH returns the number of characters in a string, INTERVAL adds or subtracts days from a date, SUBSTRING extracts part of a string, and AVG calculates the average of a set of values. Compatibility and variations for functions across databases like Oracle, SQL Server, MySQL, and PostgreSQL are also noted.

Uploaded by

Vignesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

SQL FUNCTIONS:

ABS:
ABS returns the absolute value. The output is positive even if the input is negative:
ABS(x) = x if x>=0
ABS(x) = -x if x<0
ABS can be useful for testing values that are "close". For example this query shows
each country that has area that is roughly 70 thousan. !he value 70000 is the target value" #00
is the "tolerance" so the test ABS$area%70000&'#00 tests that the area is between 70000%#00
an 70000(#00. !hat is )*#00'area'70#00.
+x, S+-+.! name" area F/01 bbc 23+/+ ABS$area%70000&'#004
Answer: name area
Georgia 69700
Ireland 701!
CURRENT_TIMESTAMP:
"#$$%&T'TI(%STA() returns the *urrent date and ti+e.
CURRENT_TIMESTAMP -> '2006-12-31 18:03:'

Ex: SE!ECT CURRENT_TIMESTAMP" #$% &R'M ()*i+,(
A%,:
CURRENT_TIMES.. whn
!01,-0.-0. 06:0/:. !011-01-0,
!01,-0.-0. 06:0/:. !011-06-01
!01,-0.-0. 06:0/:. !011-07-01
!01,-0.-0. 06:0/:. !011-11-!.

LENGTH:
0%&GT12s3 returns the nu+ber of *hara*ters in string s.
!EN-T.('.(**/') -> 0
+x, S+-+.! -+56!3$name&" name F/01 bbc4
Ans,
LENGTH(name) name
11 Afghanistan
7 Albania
7 Algeria
7 Andorra
6 Angola
19 Antigua and Barbuda
+ INTERVAL:
7 ( 85!+/9A- i :A;< returns the ate i ays after the ate .
;ou can also a ;+A/" 105!3" :A;" 30=/" 185=!+" S+.05:
;ou can also a a negative value.
:A!+ >?00)%0#%?0> ( 85!+/9A- # :A; %@ :A!+ >?00)%0#%?#>
:A!+ >?00)%0#%?0> ( 85!+/9A- # 105!3 %@ :A!+ >?00)%A0%?0>
:A!+ >?00)%0#%?0> ( 85!+/9A- # ;+A/ %@ :A!+ >?0AA%0#%?0>
8n this example we show the ate 7 ays after the value specifie in #$%
+x, S+-+.! whn" whn(85!+/9A- 7 :A; F/01 eclipse4
Ans,
whn
whn+INTERVA
L ..
?0AA%0A%
0B
?0AA%0A%AA
?0AA%0)%
0A
?0AA%0)%0C
?0AA%07%
0A
?0AA%07%0C
?0AA%AA%
?#
?0AA%A?%0?
!here are variations
oracle
S+-+.! whn" $whn& ( 7
F/01 gisq.eclipse
sqlserver
S+-+.! whn" $whn& ( 7
F/01 eclipse
ingres
S+-+.! whn" whn ( :A!+$7 :A;&
F/01 eclipse
postgres
S+-+.! whn" whn ( 7
F/01 eclipse
SUBSTRING(ansi:
S=BS!/856 allows you to extract part of a string.
S=BS!/856$>3ello worl> F/01 ? F0/ D& %@ >ell>
8n this example you get the ?n to #th character from each country>s name.
+x, S+-+.! name"S=BS!/856$name F/01 ? F0/ #&F/01 bbc4
Ans,
na!"
SUBSTRING(na
!..
Afghanistan fghan
Albania lbani
Algeria lgeri
Anorra norr
Angola ngola
Antigua an
Barbua
ntigu
!here are variations
oracle
S+-+.! name"
S=BS!/856$name" ?" #&
F/01 bbc
sqlserver
S+-+.! name"
S=BS!/856$name" ?" #&
F/01 bbc
oracle
S+-+.! name"
S=BS!/$name" ?" #&
F/01 bbc
AVG:
A96 gives the average $the mean& of a whole column or a group of rows for a single
column of values.
A96 is an aggregate function it is normally use with 6/0=E B;.
!he mean of the values ?" #" # is $?(#(#&FD G A?FD G B" you ivie by D because there
are D numbers in this example.
5=-- values o not contribute to the calculation.
S+-+.! region" A96$population& F/01 bbc 6/0=E B; region4
#ATEPART:
:A!+EA/! allows you to retrieve components of a ate.
;ou can extract also ;+A/" 105!3" :A;" 30=/" 185=!+" S+.05:.
:A!+EA/!$;+A/" & :A!+EA/!$105!3" &
:A!+EA/!$:A;" & :A!+EA/!$30=/" &
:A!+EA/!$185=!+" & :A!+EA/!$S+.05:" &
S+-+.! whn"+H!/A.!$;+A/ F/01 t& AS yr"+H!/A.!$105!3 F/01 t& AS
mnth F/01 eclipse
Ans,
whn $%
!n&
h
?0AA%0A%
0B
?0A
A
A
?0AA%0)%
0A
?0A
A
)
?0AA%07%
0A
?0A
A
7
?0AA%AA%
?#
?0A
A
AA
?0A?%0#%
?0
?0A
?
#
!here are variations,
Sql server,
S+-+.! whn
":A!+EA/!$;+A/" whn& AS yr
":A!+EA/!$105!3" whn& AS mnth
F/01 eclipse
0racle,
S+-+.! whn
"+H!/A.!$;+A/ F/01 t& AS yr
"+H!/A.!$105!3 F/01 t& AS mnth
F/01 gisq.eclipse
MA':
1AH fins the highest values in a column or part of a column
1AH is an aggregate function it is normally use with 6/0=E B;.

S+-+.! region" 1AH$name&
F/01 bbc
6/0=E B; region
.ompatibility
#ATEPART((EAR) *
En+in" O, A-&"%na&i."
ingres 5o
+H!/A.!$;+A/
F/01 &
mysql 5o
+H!/A.!$;+A/
F/01 &
oracle 5o
+H!/A.!$;+A/
F/01 &
postgres 5o
+H!/A.!$;+A/
F/01 &
sqlserve
r
;e
s
:A!+EA/!$;+A/" &
+ (s&%in+:
( allows you to sticI two or more strings together.
!his operation is concatenation.
sA ( s?
!here are variations
oracle
S+-+.! .05.A!$region"name&
F/01 bbc
mysql
S+-+.! .05.A!$region"name&
F/01 bbc
oracle
S+-+.! region JJ name
F/01 bbc
postgres
S+-+.! region JJ name
F/01 bbc
SUBSTRING:
S#BST$I&G allo4s 5ou to e6tra*t part of a string.
SUBSTRIN-('.(**/ #/1*2'" 2" 3) -> '(**'
.ompatibility
s/ + s0
En+in" O, A-&"%na&i."
ingres
;e
s
mysql 5o
.05.A!$sA"s
?&
oracle 5o sA JJ s?
postgres 5o sA JJ s?
sqlserve
r
;e
s
There are variations
ora*le
SE!ECT %34("
SUBSTR(%34(" 2" 0)
&R'M 55)
S&%in+s:
11 (s&%in+s:
JJ allows you to sticI two or more strings together.
!his operation is concatenation.
,1 66 ,2
Ex:
SE!ECT 1(7i/% 66 %34( &R'M 55)8
"o+patibilit5
SUBSTRING(s, i, j)
Enine !" A#$erna$i%e
ingres 7es S#BST$I&G2s 8$9( i 89$ :3
+5s;l 7es S#BST$I&G2s 8$9( i 89$ :3
ora*le &o S#BST$2s<i<:3
postgres 7es S#BST$I&G2s 8$9( i 89$ :3
s;lserver 7es
"o+patibilit5
s& '' s(
Enine !" A#$erna$i%e
ingres 7es
+5s;l &o "9&"AT2s1<s!3
ora*le 7es
postgres 7es
s;lserver &o s1 = s!
!here are variations
mysql
S+-+.! .05.A!$region"name&
F/01 bbc
sqlserver
S+-+.! region ( name
F/01 bbc
#A(:
>A7 allo4s 5ou to retrieve the da5 fro+ a date.
9A:(2)
S+-+.! :A;$whn& AS v "whn "wht F/01 eclipse4
% whn wh$
, !011-01-0, )artial
1 !011-06-01 )artial
1 !011-07-01 )artial
!. !011-11-!. )artial
!0 !01!-0.-!0 Annular
1/ !01!-11-1/ Total
"o+patibilit5
)A*(+)
Enine !" A#$erna$i%e
ingres &o %?T$A"T2>A7 fro+ d3
+5s;l 7es %?T$A"T2>A7 fro+ d3
ora*le &o T9'"1A$2d<@>>@3
postgres &o %?T$A"T2>A7 fro+ d3
s;lserver 7es >AT%)A$T2>A7<d3
There are variations
ingres
SE!ECT E;TRACT(9A: &R'M #$%) AS <
"#$%
"#$=
&R'M ()*i+,(
postgres
SE!ECT E;TRACT(9A: &R'M #$%) AS <
"#$%
"#$=
&R'M ()*i+,(
ora*le
SE!ECT T'_C.AR(#$%"'9A:') AS <
"#$%
"#$=
&R'M 7i,>?()*i+,(
MIN:
POSITION:
)9SITI9&2s1 I& s!3 returns the *hara*ter position of the substring s1 4ithin the larger string s!.
The first *hara*ter is in position 1. If s1 does not o**ur in s! it returns 0.
P'SITI'N('**' IN '.(**/ #/1*2') -> 3
8n this example you return the position of the string >an> within the name of the country.
S+-+.! name"E0S8!805$>an> 85 name& F/01 bbc4
name ,!SITI!N(-an-..
Afghanistan .
Albania ,
Algeria 0
Andorra 1
Angola 1
Antigua and Barbuda 1
"o+patibilit5
,!SITI!N(s& IN s()
Enine !" A#$erna$i%e
ingres 7es
+5s;l 7es
ora*le &o I&ST$2s!<s13
postgres 7es
s;lserver &o )ATI&>%?2@A@=s1=@A@<s!3
!here are variations
sqlserver
S+-+.! name"EA!85:+H$>KanK>" name&F/01 bbc 0/:+/ B; name
oracle
S+-+.! name"85S!/$name" >an>&F/01 bbc
SUM:
CASE:
"AS% allo4s 5ou to return different values under different *onditions.
If there no *onditions +at*h 2and there is not %0S%3 then &#00 is returned.
CASE @.EN )/%2i=i/%1 T.EN <3*A(1
@.EN )/%2i=i/%2 T.EN <3*A(2
E!SE 2(f_<3*A(
EN9
+x, S+-+.! name" population" .AS+
23+5 population'A000000
!3+5 >small>
23+5 population'A0000000
!3+5 >meium>
+-S+ >large>
+5:
F/01 bbc4
Ans,
name ./.0#a$i/n CASE 1HEN ./...
Afghanistan !6000000 large
Albania /!00000 +ediu+
Algeria /!900000 large
Andorra 6,000 s+all
Angola 1,.00000 large
Antigua and Barbuda 77000 s+all
Argentina /9/00000 large
#IV:
a #IV 2 returns the integer value of a ivie by b.
C :89 D %@ ?

+x, S+-+.! name"F-00/$populationFA000000& F/01 bbc4
Ans,
name 2L!!R(./.0#a$..
Afghanistan !6
Albania /
Algeria /!
Andorra 0
MINUTE:
(I&#T% allo4s 5ou to retrieve the +inute fro+ a date.
MINUTE(2)
8n this example you get the minute from the ate fiel whn.
+x, S+-+.! 185=!+$whn& AS v" whn" wht F/01 eclipse4
Ans,
% whn wh$
0 !011-01-0, )artial
0 !011-06-01 )artial
0 !011-07-01 )artial
0 !011-11-!. )artial
0 !01!-0.-!0 Annular
0 !01!-11-1/ Total
"o+patibilit5
a )I3 4
Enine !" A#$erna$i%e
ingres &o 8099$2aBb3
+5s;l 7es
ora*le &o 8099$2aBb3
postgres &o 8099$2aBb3
s;lserver &o 8099$2aBb3
!here are variations
ingres
S+-+.! +H!/A.!$105!3 F/01 whn& AS v"whn"wht F/01 eclipse
postgres
S+-+.! +H!/A.!$105!3 F/01 whn& AS v "whn"wht F/01 eclipse
oracle
S+-+.! !0L.3A/$whn">105!3>& AS v "whn"wht F/01 gisq.eclipse
QUARTER:
C#A$T%$ allo4s 5ou to retrieve the / +onth period fro+ a date.
C#A$T%$S
8irst Cuarter Danuar5 1 (ar*h /1
Se*ond Cuarter April 1 Dune /0
Third Cuarter Dul5 1 Septe+ber /0
8ourth Cuarter 9*tober 1 >e*e+ber /1

BUARTER(2)
"o+patibilit5
MINUTE(+)
Enine !" A#$erna$i%e
ingres &o %?T$A"T2>A7 fro+ d3
+5s;l 7es %?T$A"T2>A7 fro+ d3
ora*le &o T9'"1A$2d<@>>@3
postgres &o %?T$A"T2>A7 fro+ d3
s;lserver 7es >AT%)A$T2>A7<d3
S+-+.! wI" M=A/!+/$wI& AS Muarter" song F/01 totp 23+/+ singer G >.liff
/ichar> 0/:+/ B; wI :+S.4
w5 60ar$er s/n
1999-1!-!. , The (illenniu+ )ra5er
1999-1!-17 , The (illenniu+ )ra5er
1999-1!-10 , The (illenniu+ )ra5er
1999-1!-0/ , The (illenniu+ )ra5er
There are variations
postgres
SE!ECT #C"E;TRACT(BUARTER &R'M #C) AS BA31=(1",/%7
&R'M =/=+
@.ERE ,i%7(1 = 'C*iff Ri)$312'
'R9ER B: #C 9ESC
ora*le
SE!ECT #C"
T'_C.AR(#C"'BUARTER') AS BA31=(1"
,/%7
&R'M 7i,>?=/=+
@.ERE ,i%7(1 = 'C*iff Ri)$312'
'R9ER B: #C 9ESC
s;lserver
SE!ECT #C"
9ATEPART(BUARTER"#C) AS BA31=(1"
,/%7
&R'M =/=+
@.ERE ,i%7(1 = 'C*iff Ri)$312'
'R9ER B: #C 9ESC
TAN:
"o+patibilit5
6UARTER(+)
Enine !" A#$erna$i%e
ingres 7es %?T$A"T2C#A$T%$ fro+ d3
+5s;l 7es %?T$A"T2C#A$T%$ fro+ d3
ora*le &o T9'"1A$2d<@C@3
postgres &o %?T$A"T2C#A$T%$ fro+ d3
s;lserver &o >AT%)A$T2C#A$T%$<d3
CAST:
"AST allo4s 5ou to *onvert fro+ one t5pe to another.
9ften a "AST is i+pli*it - for e6a+ple if 5ou *on*atenate a string 4ith a nu+ber the nu+ber
4ill be auto+ati*all5 *hanged to a string. 1o4ever so+eti+es 5ou need to +aEe the "AST
e6pli*it.
CAST((x+1 T' =D+()

8n this example we get the population in millions by casting the floating point value to
:+.81A-$C"A& % this ensures one ecimal place of accuracy. ;ou can also .AS! a ate to a
string to extract components using S=BS!/856 or maIe up another ate.
+x, S+-+.! .AS!$populationFA000000 AS :+.81A-$C"A&& AS a"populationFA000000
AS b
F/01 bbc4
Ans,
a 4
!6.0 !6.0000
/.! /.!000
/!.9 /!.9000
0.1 0.06,0
1,.. 1,..000
E'TRACT:
%?T$A"T allo4s 5ou to retrieve *o+ponents of a date.
7ou *an e6tra*t also 7%A$< (9&T1< >A7< 19#$< (I&#T%< S%"9&>.
E;TRACT(:EAR &R'M 2) E;TRACT(M'NT. &R'M 2)
E;TRACT(9A: &R'M 2) E;TRACT(.'UR &R'M 2)
E;TRACT(MINUTE &R'M 2) E;TRACT(SEC'N9 &R'M 2)
"o+patibilit5
E7TRACT(*EAR 2R!M +)
Enine !" A#$erna$i%e
ingres 7es
+5s;l 7es 7%A$2d3
ora*le 7es T9'"1A$2d<@7777@3
postgres 7es 7%A$2d3
s;lserver &o >AT%)A$T27%A$< d3
+x,S+-+.! whn "+H!/A.!$;+A/ F/01 t& AS yr "+H!/A.!$30=/ F/01 t& AS
hr
F/01 eclipse4
Ans,
whn 8r hr
!011-01-0, !011
!011-06-01 !011 !1
!011-07-01 !011
!011-11-!. !011 6
!here are variations
sqlserver
S+-+.! whn
":A!+EA/!$;+A/" whn& AS yr
":A!+EA/!$30=/" whn& AS hr
F/01 eclipse
oracle
S+-+.! whn
"+H!/A.!$;+A/ F/01 t& AS yr
"+H!/A.!$30=/ F/01 t& AS hr
F/01 gisq.eclipse
MO#:
(9>2a<b3 returns the re+ainder 4hen a is divied b5 b
If 5ou use (9>2a< !3 5ou get 0 for even nu+bers and 1 for odd nu+bers.
If 5ou use (9>2a< 103 5ou get the last digit of the nu+ber a.
M'9(2E"2) -> 1
M'9(2E"10) -> E
"o+patibilit5
M!)(a,4)
Enine !" A#$erna$i%e
ingres 7es
+5s;l 7es a A b
ora*le 7es
postgres 7es a A b
s;lserver &o a A b
+x, S+-+.! 10:$yr"A0&" yr" city F/01 games4
Ans,
M!)(8r,&9) 8r :i$8
19 Seoul
! 199! Bar*elona
6 1996 Atlanta
0 !000 S5dne5
, !00, Athens
!00 Bei:ing
RAN,:
/A5N$& 09+/ $0/:+/ B; f :+S.& returns the ranI position relative to the expression
f.
RANF() 'GER ('R9ER B: f 9ESC)
There are variations
ora*le
SE!ECT %34("+/+A*3=i/%"
RANF() 'GER ('R9ER B: +/+A*3=i/% 9ESC)
AS 1
&R'M 55) @.ERE +/+A*3=i/%>180000000
'R9ER B: %34(
s;lserver
SE!ECT %34("+/+A*3=i/%"
RANF() 'GER ('R9ER B: +/+A*3=i/% 9ESC)
AS 1
&R'M 55) @.ERE +/+A*3=i/%>180000000
'R9ER B: %34(
TO_CHAR(*a&"s:
CEIL:
FLOOR:
MONTH:
REPLACE:
TRIM:
COALESCE:
HOUR:
NULLIF:
RIGHT:
(EAR:
CONCAT:
IFNULL:
NVL:
ROUN#:
COS:
INSTR:
PATIN#E':
SECON#:
COUNT:
LEFT:
3 MO#ULO:
SIN:
CURRENT_#ATE:
LEN:
+(*a&"s:
SUBSTR:

You might also like