1 / 12

CS122 Using Relational Databases and SQL Common Functions in MySQL

Learn about common functions in databases including numeric, string, date/time and NULL-related functions. Explore MySQL functions and their usage.

thargrove
Download Presentation

CS122 Using Relational Databases and SQL Common Functions in MySQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS122 Using Relational Databases and SQLCommon Functions in MySQL Chengyu Sun California State University, Los Angeles

  2. Common Functions in Databases • Numeric functions • String functions • Date and time functions • NULL related functions • Aggregation functions Most functions are DBMS specific.

  3. MySQL Functions • http://dev.mysql.com/doc/refman/5.0/en/functions.html

  4. Precision functions ceiling, floor, round, truncate Power and square root power, pow, exp, sqrt Logarithmic functions log, log2, log10, ln Angular functions pi, sin, cos, tan, cot, asin, acos, atan Misc. abs, sign, rand Numeric Functions

  5. Using Numeric Functions • List the prices of the products after 3% discount

  6. String length length, char_length Concatenation concat, concat_ws Format numbers format Trim white spaces ltrim, rtrim, trim Find substring instr, locate, position Extract substring mid, substring, substr, left, right Change to upper/lower case upper, lower, ucase, lcase String Functions

  7. Using String Functions • List the names of the customers in the format of <First name> <Last name>, e.g. John Doe. Note that the first letter of the first name and last name should be capitalized. • List the cities where the customers live.

  8. Date and Time Types • date – YYYY-MM-DD • time – HH:MM:SS • datetime – YYYY-MM-DD HH:MM:SS • timestamp – YYYY-MM-DD HH:MM:SS

  9. Format date/time to string date_format Convert string to date/time str_to_date Current date/time current_date, current_time, current_timestamp, now Extract date/time fields extract, year, month, day, dayofmonth, dayofweek, hour, minute, second Manipulate date/time date_add, date_sub, addtime, subtime Data and Time Functions

  10. Format Date and Time • http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format date_format( date_ordered, ‘%M %d, %Y’ ) date_format( date_ordered, ‘%M/%d/%y’ ) str_to_date( ‘Jan 18 1968’, ‘%b %d %Y’ )

  11. Using Date and Time Functions • List the orders that were placed in the last 10 days • Find the products sold last year

  12. NULL related functions • ifnull( arg1, arg2 ) • arg1 if arg1 is not null • arg2 if arg1 is null • For example: select id, ifnull( date_shipped, ‘Not shipped yet’ ) from orders;

More Related