SQL DAYNAME() Function
The SQL DAYNAME() is a function, and returns the name of the weekday of a given input date value.
The SQL DAYNAME() function returns a String indicating the specified day (Sunday, ..., Saturday) of the week.
The SQL DAYNAME() function is supports only date or datetime based table columns or fields.
It can be used in SELECT statement as well in where clause.
Related Links
SQL DAYNAME() Syntax
The below syntax is used to extract specified dayname of the week from given a input date value.
For SQL SERVER / MY SQL
SELECT DAYNAME('valid_date_value');
For MS Access
SELECT WEEKDAYNAME(valid_integer_number);
SQL DAYNAME() Example - Using Expression Or Formula
The following SQL SELECT statement will extract the day of week value from a given input date value. We use now() function to get the input date value.
For SQL SERVER / MY SQL
SELECT
NOW() AS 'Current Date and Time',
DAYNAME(NOW()) AS 'Day Name';
For MS Access
SELECT
NOW() AS 'Current Date and Time',
WEEKDAYNAME(WEEKDAY(NOW())) AS 'Day Name';
Note: the WEEKDAY() function returns a integer (between 1 and 7) containing a whole number representing the day of the week.
The result of above query is:
Current Date and Time | Day Name |
---|---|
16-06-2013 15:48:52 | Sunday |
Sample Database Table - BookOrder
BookID | BookName | OrderDate |
---|---|---|
1 | Beginning SQL Queries | 15-03-2005 01:15:26 |
2 | The SQL Programming Language | 15-03-2000 03:13:02 |
3 | Advanced SQL Programming | 17-10-2008 09:20:48 |
4 | A Visual Introduction To Sql | 13-08-1998 01:58:25 |
SQL DAYNAME() Example - With Table Column
The following SQL SELECT statement display the column "OrderDate", from the "BookOrder" table. The DAYNAME() function will extract the dayname of week from the column "OrderDate" and we will stored that value to a new column called "Day Name From OrderDate".
For SQL SERVER / MY SQL
SELECT OrderDate,
DAYNAME(OrderDate) As 'Day Name From OrderDate'
FROM BookOrder;
For MS Access
SELECT OrderDate,
WEEKDAYNAME(WEEKDAY(OrderDate)-1) AS 'Day Name From OrderDate'
FROM BookOrder;
The result of above query is:
OrderDate | Day Name From OrderDate |
---|---|
15-03-2005 01:15:26 | Tuesday |
15-03-2000 03:13:02 | Wednesday |
17-10-2008 09:20:48 | Friday |
13-08-1998 01:58:25 | Thursday |
Related Links