Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

MSSQL – Sort By Day Of Week

Posted on March 21, 2014October 26, 2015 By David Kittell

This script utilizes this Format Date Function to sort results based on a day of the week.

The case statement in this query is easy to update if your region has a different day for the start of the week.

This particular script is part of a metric that I provide for calls that come in for given marketing campaign phone number.

SELECT dbo.fnFormatDate(CallStart, 'DDDD', NULL) AS DayOfWeek
	,Count(PhoneNumber) AS TotalCalls
FROM tblMetricsCalls
WHERE PhoneNumber = '<Phone Number>'
GROUP BY PhoneNumber
	,dbo.fnFormatDate(CallStart, 'DDDD', NULL)
ORDER BY CASE
		WHEN dbo.fnFormatDate(CallStart, 'DDDD', NULL) = 'Sunday'
			THEN 1
		WHEN dbo.fnFormatDate(CallStart, 'DDDD', NULL) = 'Monday'
			THEN 2
		WHEN dbo.fnFormatDate(CallStart, 'DDDD', NULL) = 'Tuesday'
			THEN 3
		WHEN dbo.fnFormatDate(CallStart, 'DDDD', NULL) = 'Wednesday'
			THEN 4
		WHEN dbo.fnFormatDate(CallStart, 'DDDD', NULL) = 'Thursday'
			THEN 5
		WHEN dbo.fnFormatDate(CallStart, 'DDDD', NULL) = 'Friday'
			THEN 6
		ELSE 7
		END
Originally Posted on March 21, 2014
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MSSQL SQL

Post navigation

Previous post
Next post

Related Posts

Ubuntu – PPTPD VPN Install & Configure

Posted on December 28, 2015December 28, 2015

# Install PPTPD VPN & UFW Firewall sudo apt-get install pptpd ufw # Allow SSH sudo ufw allow 22 # Allow PPTP VPN sudo ufw allow 1723 # Allow HTTP – Only if you need it (If you don’t run a website from the box don’t add this) sudo ufw…

Read More

Apple macOS Server – Reset

Posted on September 30, 2017

If you’ve ran into a road block and need to reset the database and everything makes the profile manager work the scripts below will help. I have had to do this personally after removing a device from profile manager and tried to add it again. NOTE: You will need to…

Read More

Show Records Not In Another Table – Concatenated

Posted on September 24, 2013October 26, 2015

SELECT col1 ,col2 FROM table1 WHERE (col1 + ‘ ‘ + col2) NOT IN ( SELECT (col1 + ‘ ‘ + col2) FROM table2 ) Originally Posted on September 24, 2013Last Updated on October 26, 2015 All information on this site is shared with the intention to help. Before any…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes