MySQL CASE Function

Example

Go through conditions and return a value when the first condition is met:

SELECT OrderID, Quantity,
CASE
    WHEN Quantity > 30 THEN "The quantity is greater than 30"
    WHEN Quantity = 30 THEN "The quantity is 30"
    ELSE "The quantity is under 30"
END
FROM OrderDetails;

Definition and Usage

The CASE statement goes through conditions and return a value when the first condition is met (like an IF-THEN-ELSE statement). So, once a condition is true, it will stop reading and return the result.

If no conditions are true, it will return the value in the ELSE clause.

If there is no ELSE part and no conditions are true, it returns NULL.

Syntax

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END;

Continue reading MySQL CASE Function

MySQL BINARY Function

Example

Convert a value to a binary string:

SELECT BINARY "Iampsp.com";

Definition and Usage

The BINARY function converts a value to a binary string.

 

Syntax

BINARY value

Continue reading MySQL BINARY Function

MySQL BIN Function

Example

Return a binary representation of 15:

SELECT BIN(15);

Definition and Usage

The BIN() function returns a binary representation of a number, as a string value.

Syntax

BIN(number)

Continue reading MySQL BIN Function

MySQL YEARWEEK Function

Example

Return the year and week number for a date:

SELECT YEARWEEK("2017-06-15");

Definition and Usage

The YEARWEEK() function returns the year and week number (a number from 0 to 53) for a given date.

Syntax

YEARWEEK(date, firstdayofweek)

Continue reading MySQL YEARWEEK Function

MySQL YEAR Function

Example

Return the year part of a date:

SELECT YEAR("2017-06-15");

Definition and Usage

The YEAR() function returns the year part for a given date (a number from 1000 to 9999).

Syntax

YEAR(date)

Continue reading MySQL YEAR Function

MySQL WEEKOFYEAR Function

Example

Return the week number for a date:

SELECT WEEKOFYEAR("2017-06-15");

Definition and Usage

The WEEKOFYEAR() function returns the week number for a given date (a number from 1 to 53).

Note: This function assumes that the first day of the week is Monday and the first week of the year has more than 3 days.

 

Syntax

WEEKOFYEAR(date)

Continue reading MySQL WEEKOFYEAR Function

MySQL WEEKDAY Function

Example

Return the weekday number for a date:

SELECT WEEKDAY("2017-06-15");

Definition and Usage

The WEEKDAY() function returns the weekday number for a given date.

Note: 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.

Syntax

WEEKDAY(date)

Continue reading MySQL WEEKDAY Function

MySQL WEEK Function

Example

Return the week number for a date:

SELECT WEEK("2017-06-15");

Definition and Usage

The WEEK() function returns the week number for a given date (a number from 0 to 53).

Syntax

WEEK(date, firstdayofweek)

Continue reading MySQL WEEK Function

MySQL TO_DAYS Function

Example

Return the number of days between the date and year 0:

SELECT TO_DAYS("2017-06-20");

Definition and Usage

The TO_DAYS() function returns the number of days between a date and year 0 (date “0000-00-00”).

The TO_DAYS() function can be used only with dates within the Gregorian calendar.

 

Syntax

TO_DAYS(date)

Continue reading MySQL TO_DAYS Function

MySQL TIMESTAMP Function

Example

Return a datetime value based on the arguments:

SELECT TIMESTAMP("2017-07-23",  "13:10:11");

Definition and Usage

The TIMESTAMP() function returns a datetime value based on a date or datetime value.

Note: If there are specified two arguments with this function, it first adds the second argument to the first, and then returns a datetime value.

Syntax

TIMESTAMP(expression, time)

Continue reading MySQL TIMESTAMP Function