How to Format the Date & Time in MySQL

In MySQL, the DATE_FORMAT() function allows you to format the date and time.

Here’s an example:

SELECT DATE_FORMAT('2018-12-01', '%W, %d %M %Y');

Result:

Saturday, 01 December 2018

In this example, %W is for the weekday name, %d is for the day of the month, %M is for Month, and %Y is for Year. There are many more format specifiers available that enable you to specify a precise format for dates, as well as the time component.

Continue reading

List of Date Format Specifiers in MySQL

MySQL has a number of functions that allow you to format the date and time. These include functions such as DATE_FORMAT() and TIME_FORMAT().

When using these functions, you provide arguments that tell it what format you’d like the date or time to be in. These arguments are called format specifiers, and they begin with a percentage sign (%) followed by a character (usually a letter, but not always).

The following table lists the format specifiers that can be used when formatting the date and time in MySQL.

Continue reading

How to Format Numbers in SQL Server

Starting from SQL Server 2012, you can format numeric types using the T-SQL FORMAT() function. This function accepts three arguments; the number, the format, and an optional “culture” argument.

It returns a formatted string of type nvarchar.

The format is supplied as a format string. A format string defines how the output should be formatted.

Here’s an example:

SELECT FORMAT(1, 'N');

Result:

1.00

In this case, I used N as the second argument. This is the standard numeric format specifier for Number. This particular format specifier (N) results in the output being formatted with integral and decimal digits, group separators, and a decimal separator with optional negative sign. This argument is case-insensitive, so either N or n is fine.

Continue reading

How to Format the Date & Time in SQL Server

In SQL Server, you can use the T-SQL FORMAT() function to format the date and/or time. Simply provide two arguments; the date/time and the format to use.

The format is supplied as a format string. A format string defines how the output should be formatted.

The FORMAT() function also accepts an optional “culture” argument, which allows you to specify a language/locale that the results should adhere to.

Continue reading

How to Add a Separator to a Concatenated String in MySQL – CONCAT_WS()

In MySQL, the CONCAT_WS() function allows you to add a separator to concatenated strings. If you just use the CONCAT() function, you’d have no separator (unless you explicitly added a separator as an argument between each string argument).

A common usage of the CONCAT_WS() function is to create a comma-delimited list.

Continue reading

How to Add a Separator to a Concatenated String in SQL Server – CONCAT_WS()

In SQL Server and Azure, if you need to concatenate two or more strings, you can use the T-SQL CONCAT() function. As with any basic concatenation operation, this function joins the strings together, end-to-end.

But what if you need to add a separator between each string?

For example, you might want to make a comma-separated list of strings. In this case, you’d want to insert a comma in between each string. Like this:

Paris, France

Instead of this:

ParisFrance

Fortunately, T-SQL provides the CONCAT_WS() function that helps you do exactly that. The CONCAT_WS() function works just like the CONCAT() function, except that it takes an extra argument – the separator you’d like to use.

Continue reading

How to Concatenate Strings in SQL Server with CONCAT()

In SQL Server, you can concatenate two or more strings by using the T-SQL CONCAT() function. You can also use SQL Server’s string concatenation operator (+) to do the same thing. Both are explained here.

In SQL Server (and in any computer programming environment), string concatenation is the operation of joining character strings end-to-end.

Here’s an example:

SELECT CONCAT('Peter', ' ', 'Griffin') AS 'Full Name';

Result:

Full Name    
-------------
Peter Griffin

Note that I actually concatenated 3 strings here. I concatenated the first name, the last name, plus a space.

Continue reading

How to Concatenate Strings in MySQL with CONCAT()

MySQL has the CONCAT() function, which allows you to concatenate two or more strings. The function actually allows for one or more arguments, but its main use is to concatenate two or more strings.

In MySQL (and in any computer programming environment), string concatenation is the operation of joining character strings end-to-end.

Here’s an example:

SELECT CONCAT('Homer', ' ', 'Simpson') AS 'Full Name';

Result:

+---------------+
| Full Name     |
+---------------+
| Homer Simpson |
+---------------+

Note that I actually concatenated 3 strings here. I concatenated the first name, the last name, plus a space.

Continue reading