MySQL

MySQL Datasource Connector Details

MySQL Overview

MySQL is a flexible database for transaction and analytical workloads. ChartSQL can connect to any MySQL database like MySQL Server, MariaDB, Planetscale, and any other MySQL compatible wire protocol database.

Chart Data Type Casting

ChartSQL detects four types of columns: Date, Datetime, String, Numeric. It maps all MySQL types to one of these four chart data types.

If you need to manualy specify the type, you can follow these examples

Date

  1. Using CAST:

SELECT CAST('2021-01-01' AS DATE);
  1. Using STR_TO_DATE for custom formats:

SELECT STR_TO_DATE('01-01-2021', '%d-%m-%Y');

Datetime

Using CAST:

SELECT CAST('2021-01-01 13:25:20' AS DATETIME);

Using STR_TO_DATE with custom formats:

String

Using CAST:

Using implicit conversion with concatenation:

Or by concatenation with the CONCAT function:

These methods work for converting numbers or dates to strings. When converting dates or times to strings, you can also format them using the DATE_FORMAT function:

Use the DATE_FORMAT function if you need specific formatting for your date or datetime values.

Numeric

To cast strings or other types of data to numbers in MySQL, you can use CAST or CONVERT. Here are examples of how to cast to integer (SIGNED) or decimal (DECIMAL) numbers:

Casting to an integer (whole number):

Casting to a decimal (floating-point number):

These functions are appropriate for converting from string representations of numbers or from real or floating-point data types to integers or decimals. If your string has non-numeric characters or improper formatting, the conversion will result in a zero (0) in CAST functions.

For an implicit conversion, you can use an arithmetic operation such as addition or subtraction with a number, which will coerce the string to a numeric type:

This method will work best when you are confident that the string can be converted directly to a number without the need for specifying the exact numeric type (integer or decimal).

MySQL Data Tips

Visualization tips for working with MySQL.

Summarizing by Month

When you want summarize data by month, it is helpful to normalize your dates to the month floor (1st of the month) so that you can group all of your records into that month:

Last updated

Was this helpful?