Sams Teach Yourself SQL in 24 Hours
- Table of Contents
- Copyright
- About the Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Part I: A SQL Concepts Overview
- Hour 1. Welcome to the World of SQL
- Part II: Building Your Database
- Hour 2. Defining Data Structures
- Hour 3. Managing Database Objects
- Hour 4. The Normalization Process
- Hour 5. Manipulating Data
- Hour 6. Managing Database Transactions
- Part III: Getting Effective Results from Queries
- Hour 7. Introduction to the Database Query
- Hour 8. Using Operators to Categorize Data
- Hour 9. Summarizing Data Results from a Query
- Hour 10. Sorting and Grouping Data
- Hour 11. Restructuring the Appearance of Data
- Hour 12. Understanding Dates and Times
- Part IV: Building Sophisticated Database Queries
- Hour 13. Joining Tables in Queries
- Hour 14. Using Subqueries to Define Unknown Data
- Hour 15. Combining Multiple Queries into One
- Part V: SQL Performance Tuning
- Hour 16. Using Indexes to Improve Performance
- Hour 17. Improving Database Performance
- Part VI: Using SQL to Manage Users and Security
- Hour 18. Managing Database Users
- Hour 19. Managing Database Security
- Part VII: Summarized Data Structures
- Hour 20. Creating and Using Views and Synonyms
- Hour 21. Working with the System Catalog
- Part VIII: Applying SQL Fundamentals in Today's World
- Hour 22. Advanced SQL Topics
- Hour 23. Extending SQL to the Enterprise, the Internet, and the Intranet
- Hour 24. Extensions to Standard SQL
- Part IX: Appendixes
- Appendix A. Common SQL Commands
- Appendix B. Using MySQL for Exercises
- Appendix C. Answers to Quizzes and Exercises
- Appendix D. CREATE TABLE Statements for Book Examples
- Appendix E. INSERT Statements for Data in Book Examples
- Appendix F. Glossary
- Appendix G. Bonus Exercises
Conversion Functions
|
|
Conversion functions are used to convert a data type into another data type. For example, there may be times when you want to convert character data into numeric data. You may have data that is normally stored in character format, but occasionally you want to convert the character format to numeric for the purpose of making calculations. Mathematical functions and computations are not allowed on data that is represented in character format. |
The following are general types of data conversions:
- Character to numeric
- Numeric to character
- Character to date
- Date to character
The first two types of conversions are discussed in this hour. The remaining conversion types are discussed during Hour 12, "Understanding Dates and Times," after date and time storage is discussed in more detail.
Converting Character Strings to Numbers
There are two things you should notice regarding the differences between numeric data types and character string data types:
- Arithmetic expressions and functions can be used on numeric values.
- Numeric values are right-justified, whereas character string data types are left-justified in the output results.
When a character string is converted to a numeric value, the value takes on the two attributes just mentioned.
Some implementations may not have functions to convert character strings to numbers, whereas some will have such conversion functions. In either case, consult your implementation documentation for specific syntax and rules for conversions.
The following is an example of a numeric conversion using an Oracle conversion function:
![]()
SELECT EMP_ID, TO_NUMBER(EMP_ID) FROM EMPLOYEE_TBL;
EMP_ID TO_NUMBER(EMP_ID) --------- ----------------- 311549902 311549902 442346889 442346889 213764555 213764555 313782439 313782439 220984332 220984332 443679012 443679012 6 rows selected.
The employee identification is right-justified following the conversion.
Converting Numbers to Strings
The conversion of numeric values to character strings is precisely the opposite of converting characters to numbers.
The following is an example of converting a numeric value to a character string using a Transact-SQL conversion function for Microsoft SQL Server:
![]()
![]()
SELECT PAY = PAY_RATE, NEW_PAY = STR(PAY_RATE) FROM EMPLOYEE_PAY_TBL WHERE PAY_RATE IS NOT NULL;
PAY NEW_PAY ---------- ------- 17.5 17.5 14.75 14.75 18.25 18.25 12.8 12.8 11 11 15 15 6 rows affected.
The following is the same example using an Oracle conversion function:
![]()
SELECT PAY_RATE, TO_CHAR(PAY_RATE) FROM EMPLOYEE_PAY_TBL WHERE PAY_RATE IS NOT NULL;
PAY_RATE TO_CHAR(PAY_RATE) ---------- ----------------- 17.5 17.5 14.75 14.75 18.25 18.25 12.8 12.8 11 11 15 15 6 rows selected.
The Concept of Combining Character Functions | Next Section

Account Sign In
View your cart