Unit 5 - Invoke Conversion Functions and Conditional Expressions
Unit 5 - Invoke Conversion Functions and Conditional Expressions
Expressions
Agenda
• In some cases, the server receives data of one data type where it
expects data of a different data type; the server can automatically
convert the data to the expected data type. This data type
conversion can be done implicitly by the server or explicitly by the
user.
• Implicit data type conversions work according to the rules
explained in the following slides.
• Explicit data type conversions are performed by using the
conversion functions. Conversion functions convert a value from
one data type to another.
• Although implicit data type conversion is available, it is
recommended that you do the explicit data type conversion
Implicit Data Type Conversion
SQL provides three functions to convert a value from one data type to
another:
• DATE_FORMAT(date , fmt) - converts a date value to a string with
the format model fmt;
• STR_TO_DATE(char, fmt) - converts a character string
representing a date to a date value according to fmt that is
specified;
• CAST(expression AS TYPE) - converts a value to a specified type.
The target type can be: BINARY, CHAR, DATE, DATETIME, TIME,
DECIMAL, SIGNED, UNSIGNED.
• CONVERT(expression, TYPE) - converts a value into the specified
datatype or character set;
DATE_FORMAT Function
DATE_FORMAT(date,'format_model')
• If the first expression is not null, the COALESCE function returns that
expression; otherwise, it does a COALESCE of the remaining
expressions.
Syntax
COALESCE (expr1, expr2, ... exprn)
• CASE expression
• IF expression
CASE Expression
• Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE
statement:
CASE expr
WHEN comparison_expr1 THEN return_expr1
[WHEN comparison_expr2 THEN return_expr2
WHEN comparison_exprn THEN return_exprn
ELSE else_expr]
END
IF(cond, val_true,val_false)