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

How to Set the Locale for the Current Connection in MySQL

There are certain date functions in MySQL that the return a day name or month name. In particular, I’m referring to the DATE_FORMAT(), DAYNAME(), and MONTHNAME() functions. These might return a value of say, November, or Monday, depending on the query being used. But the results could just as easily be returned in a different language if required.

The language that these functions use for their return value is derived from the lc_time_names system variable. You can view the value of this variable or set its SESSION value so that the results of these functions is in the desired language/locale.

Locale names have language and region subtags listed by the Internet Assigned Numbers Authority (IANA). Examples include en_US for English – United States, en_NZ for English – New Zealand, or es_PA for Spanish – Panama, etc (for a list of locales supported by MySQL, see Full List of Locales in MySQL).

In this article, I’ll show you how to find the current locale for your connection, change it, and then see how it affects the results of a query. I also show you a function that’s immune to this setting (but don’t worry, this function allows you to specify the locale).

Continue reading

@@DATEFIRST – Get the First Day of the Week in SQL Server

In SQL Server, the first day of the week is determined by the current language settings. You can also override that with the SET DATEFIRST statement, which allows you to explicitly set the first day of the week.

In either case, you can use the @@DATEFIRST function to find out what settings your session is using for the first day of the week. This article demonstrates how.

Continue reading

SET DATEFIRST – Set the First Day of the Week in SQL Server

In SQL Server, you can use SET DATEFIRST to set the first day of the week.

The first day of the week can be different, depending on the language being used. For example the default for us_English is 7 (Sunday), whereas the default for Deutsch (German) is 1 (Monday).

This article demonstrates how to change the first day of the week without changing the language.

Continue reading

CURRENT_TIMESTAMP Examples in SQL Server (T-SQL)

The CURRENT_TIMESTAMP function returns the current date and time as a datetime value. This value is derived from the operating system of the computer that the instance of SQL Server is running on.

This function is the ANSI SQL equivalent to the T-SQL GETDATE() function, so you can use whichever one you prefer. Note that both functions have a lower date range and a lower default fractional precision than the T-SQL SYSDATETIME() function (which returns a datetime2(7) value).

This article provides examples of the CURRENT_TIMESTAMP function, including how you can use it with other functions to return the value you’re interested in.

Continue reading