Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Intro to tsql unit 10


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Intro to tsql unit 10

  1. 1. Introduction To SQL Unit 10 Modern Business Technology Introduction To TSQL Unit 10 Developed by Michael Hotek
  2. 2. Functions <ul><li>In addition to the standard SQL statements, SQL Server and most other DBMSs include a set of functions to perform additional processing </li></ul><ul><li>The functions fit into 6 categories </li></ul><ul><ul><li>string </li></ul></ul><ul><ul><li>conversion </li></ul></ul><ul><ul><li>date </li></ul></ul><ul><ul><li>mathematical </li></ul></ul><ul><ul><li>aggregate </li></ul></ul><ul><ul><li>general </li></ul></ul>
  3. 3. Functions <ul><li>To determine which functions are available for your particular DBMS, you should consult the online help for your DBMS </li></ul><ul><li>Overview of Books Online </li></ul>
  4. 4. String Functions <ul><li>The string functions we will discuss are: </li></ul><ul><ul><li>substring </li></ul></ul><ul><ul><li>right </li></ul></ul><ul><ul><li>upper </li></ul></ul><ul><ul><li>lower </li></ul></ul><ul><ul><li>charindex </li></ul></ul><ul><ul><li>patindex </li></ul></ul><ul><ul><li>ascii </li></ul></ul><ul><ul><li>char </li></ul></ul><ul><ul><li>ltrim </li></ul></ul><ul><ul><li>rtrim </li></ul></ul><ul><ul><li>space </li></ul></ul><ul><ul><li>str </li></ul></ul><ul><ul><li>soundex </li></ul></ul><ul><ul><li>replicate </li></ul></ul>
  5. 5. String Functions <ul><li>Substring gives you the ability to extract a portion of a string from a another string or piece of data </li></ul><ul><li>substring(expression,start,length) </li></ul><ul><li>substring('abcde',2,3) </li></ul><ul><li>Results </li></ul><ul><li>bcd </li></ul><ul><li>Right returns the right portion of a string </li></ul><ul><li>right(expression,length) </li></ul><ul><li>right('abcde',3) </li></ul><ul><li>Results </li></ul><ul><li>'cde </li></ul>
  6. 6. String Functions <ul><li>Upper converts a string to upper case </li></ul><ul><ul><li>upper(expression) </li></ul></ul><ul><ul><li>upper('abcde') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>ABCDE </li></ul></ul><ul><li>Lower converts a string to lower case </li></ul><ul><ul><li>lower(expression) </li></ul></ul><ul><ul><li>lower('aBCDE') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>abcde </li></ul></ul>
  7. 7. String Functions <ul><li>charindex returns the starting position of a string the specify </li></ul><ul><ul><li>charindex(pattern,expression) </li></ul></ul><ul><ul><li>charindex('bc','abcde' </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>2 </li></ul></ul><ul><li>patindex performs the same operation as charindex, but can also include wildcard characters and can be used on text columns </li></ul><ul><ul><li>patindex(pattern,expression) </li></ul></ul><ul><ul><li>patindex('%bc%') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>2 </li></ul></ul>
  8. 8. String Functions <ul><li>ascii converts a character to it's equivalent ASCII value </li></ul><ul><ul><li>ascii(expression) </li></ul></ul><ul><ul><li>ascii('T') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>84 </li></ul></ul><ul><li>char takes an ascii value and returns the corresponding character </li></ul><ul><ul><li>char(expression) </li></ul></ul><ul><ul><li>char(84) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>T </li></ul></ul>
  9. 9. String Functions <ul><li>ltrim and rtrim remove leading or trailing spaces. ltrim from the left and rtrim from the right </li></ul><ul><ul><li>ltrim(expression) </li></ul></ul><ul><ul><li>ltrim(' abcde') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>abcde </li></ul></ul><ul><ul><li>rtrim(expression) </li></ul></ul><ul><ul><li>rtrim('abcde ') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>abcde </li></ul></ul><ul><ul><li>ltrim(rtrim(' abcde ')) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>abcde </li></ul></ul>
  10. 10. String Functions <ul><li>Space constructs a string of the number of spaces specifiied </li></ul><ul><ul><li>space(expression) </li></ul></ul><ul><ul><li>space(2) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>2 spaces </li></ul></ul><ul><li>Str will convert a number to a string and round it off to the specified number of decimal places </li></ul><ul><ul><li>str(numeric_value,length,decimals) </li></ul></ul><ul><ul><li>str(456.56,5,1) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>456.6 </li></ul></ul>
  11. 11. String Functions <ul><li>soundex converts a character expression to a four character code. This is used to find similar words. It ignores any vowels </li></ul><ul><ul><li>soundex(expression) </li></ul></ul><ul><ul><li>soundex('smith'), soundex('smythe') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>'S530' </li></ul></ul><ul><li>replicate creates a string composed of the number of copies of a string that you specify </li></ul><ul><ul><li>replicate(expression,number) </li></ul></ul><ul><ul><li>replicate('abc',3) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>abcabcabc </li></ul></ul>
  12. 12. Converting Data <ul><li>Some data within SQL Server is implicitly converted </li></ul><ul><li>A money datatype will convert to a real or a float without any intervention </li></ul><ul><li>Sometimes though, we want a number to be treated as a character or vice versa </li></ul><ul><li>To do this we need to use the convert function </li></ul>
  13. 13. Convert <ul><li>The string function str gave us a quick way to convert a number to a string </li></ul><ul><li>The convert function gives us much more control over the exact datatype the data is converted to. </li></ul><ul><li>convert(datatype,expression [,style]) </li></ul><ul><li>select title_id, 'The number of books sold: ' + convert(varchar(10),ytd_sales) from titles where title_id like 'PS20%' </li></ul><ul><li>title_id </li></ul><ul><li>-------- ------------------------------------ </li></ul><ul><li>PS2091 The number of books sold: 2045 </li></ul><ul><li>(1 row(s) affected) </li></ul>
  14. 14. Convert <ul><li>By using the third optional argument of the convert function, you can utilize some formatting while converting data. </li></ul><ul><li>This argument is used when converting a date to a string </li></ul><ul><li>select convert(varchar(8),pubdate,2) from titles where title_id = 'MC3026' </li></ul><ul><li>Results </li></ul><ul><li>91.06.24 </li></ul>
  15. 15. Date Conversion <ul><li>Some of the most commonly used values are as follows: </li></ul><ul><ul><li>2 </li></ul></ul><ul><ul><li>3 dd/mm/yy </li></ul></ul><ul><ul><li>4 </li></ul></ul><ul><ul><li>5 dd-mm-yy </li></ul></ul><ul><ul><li>102 </li></ul></ul><ul><ul><li>103 dd/mm/yyyy </li></ul></ul><ul><ul><li>104 </li></ul></ul><ul><ul><li>105 dd-mm-yyyy </li></ul></ul><ul><li>Consult your online manual for other values </li></ul>
  16. 16. Date Conversion <ul><li>A datetime value can be directly compared to a string datatype that has a datetime value, because the string can be implicitly converted </li></ul><ul><li>select title_id from titles where pubdate = '6/30/86' </li></ul><ul><li>Results </li></ul><ul><li>PC1035 </li></ul><ul><li>The built in function getdate() will return the current date and time from OS where the DBMS resides </li></ul>
  17. 17. Date Functions <ul><li>Sometimes we only want a portion of a date or we want to do date arithmetic </li></ul><ul><li>To do this we will use the following functions: </li></ul><ul><ul><li>datename </li></ul></ul><ul><ul><li>datepart </li></ul></ul><ul><ul><li>dateadd </li></ul></ul><ul><ul><li>datediff </li></ul></ul>
  18. 18. Date Functions <ul><li>datename will return the name of a specified part of a date </li></ul><ul><ul><li>datename(param,date) </li></ul></ul><ul><ul><li>datename(mm,'2/12/96') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>February </li></ul></ul><ul><li>The parameter values are as follows: </li></ul><ul><ul><li>yy year </li></ul></ul><ul><ul><li>qq quarter </li></ul></ul><ul><ul><li>mm month </li></ul></ul><ul><ul><li>dw day of week </li></ul></ul><ul><ul><li>dy day of year </li></ul></ul><ul><ul><li>dd day </li></ul></ul><ul><ul><li>wk week </li></ul></ul>
  19. 19. Date Functions <ul><li>datepart will return the numeric part of a date </li></ul><ul><ul><li>datepart(param,date) </li></ul></ul><ul><ul><li>datepart(mm,'2/12/96') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>6 </li></ul></ul><ul><li>dateadd adds or subtracts a specified amount of time to a date </li></ul><ul><ul><li>dateadd(param,number,date) </li></ul></ul><ul><ul><li>dateadd(dd,5,'6/5/85') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>6/10/85 </li></ul></ul><ul><li>To subtract from a date, specify a negative value: dateadd(dd,-5,'6/10/85') </li></ul>
  20. 20. Date Functions <ul><li>datediff is used to calculate the difference between two dates of times </li></ul><ul><ul><li>datediff(param,start_date,end_date) </li></ul></ul><ul><ul><li>datediff(dd,'6/10/85','6/20/85') </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>10 </li></ul></ul><ul><li>Available paramters for datediff are: </li></ul><ul><ul><li>yy year </li></ul></ul><ul><ul><li>qq quarted </li></ul></ul><ul><ul><li>mm month </li></ul></ul><ul><ul><li>dw day of week </li></ul></ul><ul><ul><li>dd day </li></ul></ul><ul><ul><li>wk week </li></ul></ul><ul><ul><li>hh hour </li></ul></ul>
  21. 21. Year 2000 <ul><li>SQL Server is year 2000 compliant </li></ul><ul><li>It accomplishes this by utilizing a comparison technique </li></ul><ul><li>If you enter a two digit year less than 50, SQL Server assume 20xx. </li></ul><ul><li>If the two digit year is greater than or equal to 50, SQL Server will assume 19xx </li></ul><ul><li>If you send it a four digit year, it stores the entire year in the database thus avoiding any confusion. </li></ul>
  22. 22. Mathematical Functions <ul><li>Mathematical functions allow to you manipulate and do calculations on numeric data. The functions we will examine are: </li></ul><ul><ul><li>abs </li></ul></ul><ul><ul><li>ceiling </li></ul></ul><ul><ul><li>floor </li></ul></ul><ul><ul><li>round </li></ul></ul><ul><ul><li>exp </li></ul></ul><ul><ul><li>rand </li></ul></ul><ul><ul><li>log </li></ul></ul><ul><ul><li>pi </li></ul></ul><ul><ul><li>power </li></ul></ul><ul><ul><li>sqrt </li></ul></ul><ul><ul><li>sin </li></ul></ul><ul><ul><li>cos </li></ul></ul><ul><ul><li>tan </li></ul></ul>
  23. 23. Mathematical Functions <ul><li>abs returns the absolute value of a number </li></ul><ul><ul><li>abs(number) </li></ul></ul><ul><ul><li>abs(-59) </li></ul></ul><ul><ul><li>Result </li></ul></ul><ul><ul><li>59 </li></ul></ul><ul><li>round returns a value rounded off to the number of digits specified </li></ul><ul><ul><li>round(number,digits) </li></ul></ul><ul><ul><li>round(123.4567,2) </li></ul></ul><ul><ul><li>Result </li></ul></ul><ul><ul><li>123.4600 </li></ul></ul><ul><li>You can round down by specifying a negative value </li></ul><ul><li>The result is padded with zeros to keep the total number of digits the same </li></ul>
  24. 24. Mathematical Functions <ul><li>ceiling returns the next highest integer value </li></ul><ul><ul><li>ceiling(number) </li></ul></ul><ul><ul><li>ceiling(123.4) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>124 </li></ul></ul><ul><li>floor returns the next lowest integer value </li></ul><ul><ul><li>floor(number) </li></ul></ul><ul><ul><li>floor(123.4) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>123 </li></ul></ul>
  25. 25. Mathematical Functions <ul><li>exp raises the constant e to the specified power </li></ul><ul><ul><li>exp(number) </li></ul></ul><ul><ul><li>exp(0) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>1 </li></ul></ul><ul><li>log returns the natural logarithm of the specified number </li></ul><ul><ul><li>log(number) </li></ul></ul><ul><ul><li>log(1) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>0 </li></ul></ul>
  26. 26. Mathematical Functions <ul><li>rand returns will generate a set of pseudo-random numbers </li></ul><ul><ul><li>rand([seed_value]) </li></ul></ul><ul><ul><li>rand(39) </li></ul></ul><ul><ul><li>Result </li></ul></ul><ul><li>The rand function is seeded at server startup by the DBMS and uses the system clock as part of the seed value </li></ul><ul><li>You can ensure a repeatable sequence by using the same seed value and making sure that only one person accesses the rand function while the repeatable sequence is needed </li></ul>
  27. 27. Mathematical Functions <ul><li>pi returns the mathematical constant pi </li></ul><ul><ul><li>pi() </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>3.1415926… </li></ul></ul><ul><li>power raises a number to a specified power </li></ul><ul><ul><li>power(number,power) </li></ul></ul><ul><ul><li>power(10,3) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>1000 </li></ul></ul><ul><li>sqrt returns the square root of a number </li></ul><ul><ul><li>sqrt(number) </li></ul></ul><ul><ul><li>sqrt(100) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><ul><li>10 </li></ul></ul>
  28. 28. Mathematical Functions <ul><li>sin, cos, and tan return the sine, cosine, and tangent of an angle </li></ul><ul><li>The angle is measured in radians and not degrees </li></ul><ul><ul><li>sin(radians) </li></ul></ul><ul><ul><li>sin( ) </li></ul></ul><ul><ul><li>Results </li></ul></ul><ul><li>You can also use the degrees and radians functions to convert values from one to another </li></ul>
  29. 29. Aggregate Functions <ul><li>We've already touched on most of the aggregate functions previously, but a brief overview follows </li></ul><ul><li>count(*) number of selected rows </li></ul><ul><li>count(column) number of non null rows </li></ul><ul><li>sum(column) total of all values in a column </li></ul><ul><li>avg(column) average of aq column </li></ul><ul><li>max(column) maximum value in a column </li></ul><ul><li>min(column) minimum value in a column </li></ul>
  30. 30. Isnull <ul><li>The isnull function can be used to avoid some of the side effects of null values </li></ul><ul><li>This function substitute a specified value for a null </li></ul><ul><li>isnull(expression,value) </li></ul><ul><li>select title_id, isnull(notes, 'No notes for title') from titles </li></ul><ul><li>title_id </li></ul><ul><li>-------- ----------------------------------------------------------------- </li></ul><ul><li>BU1032 An overview of available database systems with ... </li></ul><ul><li>BU1111 Helpful hints on how to use your electronic resources to ... </li></ul><ul><li>BU2075 The latest medical and psychological techniques for living... </li></ul><ul><li>BU7832 Annotated analysis of what computers can do for you: ... </li></ul><ul><li>MC2222 Favorite recipes for quick, easy, and elegant meals. </li></ul><ul><li>MC3021 Traditional French gourmet recipes adapted for modern... </li></ul><ul><li>MC3026 No notes for title </li></ul><ul><li>... </li></ul><ul><li>(18 row(s) affected) </li></ul>
  31. 31. Unit 10 Review <ul><li>The functions fit into 6 categories </li></ul><ul><ul><li>string </li></ul></ul><ul><ul><li>conversion </li></ul></ul><ul><ul><li>date </li></ul></ul><ul><ul><li>mathematical </li></ul></ul><ul><ul><li>aggregate </li></ul></ul><ul><ul><li>general </li></ul></ul><ul><li>You can convert data from one data type to another </li></ul><ul><li>Most of the functions you will use are the standard aggregates of count(*), sum, min, and max </li></ul><ul><li>Isnull allows a graceful way of handling nulls </li></ul>
  32. 32. Unit 10 Exercises <ul><li>There are no exercises for this unit </li></ul>