MySQL TIMEDIFF() vs TIMESTAMPDIFF(): What’s the Difference?

This article looks at the difference between the MySQL TIMEDIFF() and TIMESTAMPDIFF() functions.

Both functions do a similar thing, but there are some significant differences between the two.

The following table summarizes the difference between these two functions:

TIMEDIFF() TIMESTAMPDIFF()
Requires 2 arguments. Requires 3 arguments.
Subtracts the 2nd argument from the 1st (date1 − date2). Subtracts the 2nd argument from the 3rd (date2 − date1).
Result is expressed as a time value (and it has the limitations of the time data type). Result is an integer, expressed by a number of units as provided by the first argument.
Accepts time or datetime expressions. Accepts date or datetime expressions.
Both arguments must be the same type (either time or datetime). Both arguments can be of a different type (date or datetime).

Continue reading

MySQL DATEDIFF() vs TIMESTAMPDIFF(): What’s the Difference?

This article looks at the difference between two MySQL functions; DATEDIFF() and TIMESTAMPDIFF().

Both functions return the difference between two dates and/or times, but the result is different between the two functions.

The following table summarizes the difference between these two functions:

DATEDIFF() TIMESTAMPDIFF()
Requires 2 arguments. Requires 3 arguments.
Subtracts the 2nd argument from the 1st (expr1 − expr2). Subtracts the 2nd argument from the 3rd (expr2 − expr1).
Result is expressed as a value in days. Result is expressed as the unit provided by the first argument.
Can compare only the date value of its arguments. Can compare the date and time value of its arguments.

Continue reading

MySQL DATEDIFF() vs TIMEDIFF(): What’s the Difference?

Two date functions included in MySQL are DATEDIFF() and TIMEDIFF().

Both functions do a similar thing, but with some meaningful differences.

The following table summarizes the difference between these two functions:

DATEDIFF() TIMEDIFF()
Result is expressed as a value in days. Result is expressed as a time value.
Compares only the date value of its arguments. Compares the time value of its arguments.
Accepts date or date-and-time expressions. Accepts time or date-and-time expressions.
Both arguments can be of a different type (date or date-and-time). Both arguments must be the same type (either time or date-and-time).

Continue reading

MySQL CHAR() vs T-SQL CHAR(): What’s the Difference?

There are many functions that are included in both MySQL and SQL Server. However, just because both DBMSs share functions of the same name, doesn’t mean that those functions work exactly the same way.

Take CHAR() for example. Both MySQL and SQL Server include this function. Well technically, for SQL Server it’s actually a T-SQL function, but that’s beside the point. The point is that the MySQL CHAR() function provides more functionality than the T-SQL/SQL Server CHAR() function.

In particular, the MySQL version accepts multiple integers, whereas, the T-SQL version only accepts a single integer. The MySQL version also accepts a USING clause that allows you to specify which character set to use (the T-SQL version doesn’t have this clause).

Below are some examples of these differences.

Continue reading

MAX() – Find the Maximum Value in a Column in MySQL

The MySQL MAX() function is an aggregate function that returns the maximum value from an expression.

Typically, the expression would be a range of values returned as separate rows in a column, and you can use this function to find the maximum value from the returned rows. If there are no matching rows, MAX() returns NULL.

For example, you can use this function to find out which city has the largest population out of a list of cities.

Continue reading