![]() In this tutorial, you have learned how to use the MySQL CAST() function to convert a value with any type into a value with a specified type.String Functions: ASCII CHAR_LENGTH CHARACTER_LENGTH CONCAT CONCAT_WS FIELD FIND_IN_SET FORMAT INSERT INSTR LCASE LEFT LENGTH LOCATE LOWER LPAD LTRIM MID POSITION REPEAT REPLACE REVERSE RIGHT RPAD RTRIM SPACE STRCMP SUBSTR SUBSTRING SUBSTRING_INDEX TRIM UCASE UPPER Numeric Functions: ABS ACOS ASIN ATAN ATAN2 AVG CEIL CEILING COS COT COUNT DEGREES DIV EXP FLOOR GREATEST LEAST LN LOG LOG10 LOG2 MAX MIN MOD PI POW POWER RADIANS RAND ROUND SIGN SIN SQRT SUM TAN TRUNCATE Date Functions: ADDDATE ADDTIME CURDATE CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP CURTIME DATE DATEDIFF DATE_ADD DATE_FORMAT DATE_SUB DAY DAYNAME DAYOFMONTH DAYOFWEEK DAYOFYEAR EXTRACT FROM_DAYS HOUR LAST_DAY LOCALTIME LOCALTIMESTAMP MAKEDATE MAKETIME MICROSECOND MINUTE MONTH MONTHNAME NOW PERIOD_ADD PERIOD_DIFF QUARTER SECOND SEC_TO_TIME STR_TO_DATE SUBDATE SUBTIME SYSDATE TIME TIME_FORMAT TIME_TO_SEC TIMEDIFF TIMESTAMP TO_DAYS WEEK WEEKDAY WEEKOFYEAR YEAR YEARWEEK Advanced Functions: BIN BINARY CASE CAST COALESCE CONNECTION_ID CONV CONVERT CURRENT_USER DATABASE IF IFNULL ISNULL LAST_INSERT_ID NULLIF SESSION_USER SYSTEM_USER USER VERSION SQL Server FunctionsĭATE_FORMAT( date, format) Parameter Values Parameter The following statement converts DOUBLE values into CHAR values and uses the results as the arguments of the CONCAT function: SELECT productName,įROM products Code language: SQL (Structured Query Language) ( sql ) Try It Out +-+-+ģ rows in set (0.03 sec) Code language: JavaScript ( javascript ) RequiredDate BETWEEN CAST( '' AS DATETIME)ĪND CAST( '' AS DATETIME) Code language: SQL (Structured Query Language) ( sql ) To ensure safety, you can use the CAST() function to explicitly convert a string into a TIMESTAMP value, as follows: SELECT RequiredDate BETWEEN '' AND '' Code language: SQL (Structured Query Language) ( sql )īecause the data type of the requireDate column is DATE, MySQL has to convert the literal strings: '' and '' into TIMESTAMP values before evaluating the WHERE condition. The following query retrieves the orders whose required dates are in January 2003: SELECT Let’s take a look at the orders table in the sample database: +-+ġ row in set ( 0.00 sec) Code language: PHP ( php ) 2) Using MySQL CAST function with table data examples | CONCAT( 'MySQL CAST example #',CAST( 2 AS CHAR)) | The following statement explicitly converts an integer into a string and concatenates the string with another string: SELECT CONCAT( 'MySQL CAST example #', CAST( 2 AS CHAR)) Code language: SQL (Structured Query Language) ( sql ) To explicitly convert a string into an integer, you use the CAST() function as the following statement: SELECT ( 1 + CAST( '1' AS UNSIGNED))/ 2 Code language: SQL (Structured Query Language) ( sql ) In the following example, MySQL converts a string into an integer implicitly before doing a calculation: SELECT ( 1 + '1')/ 2 Code language: SQL (Structured Query Language) ( sql )ġ row in set (0.01 sec) Code language: JavaScript ( javascript ) ![]() Let’s take a look at some examples of using the CAST() function. In practice, you often use the CAST() function to return a value with a specified type for comparison in the WHERE, JOIN, and HAVING clauses. The target type can be any one of the following types: BINARY, CHAR, DATE, DATETIME, TIME, DECIMAL, SIGNED, or UNSIGNED. The syntax of the MySQL CAST() function is as follows: CAST(expression AS TYPE) Code language: SQL (Structured Query Language) ( sql ) ![]() The CAST() function allows you to convert a value of one type into another type. ![]() ![]() Summary: in this tutorial, you will learn how to use MySQL CAST function to convert a value of a type into another type. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |