Dealing with custom date formats in T-SQL
Should I be formatting dates on the database side? What tools can I use in SQL Server to format and parse dates? This article helps you decide which one best suits your needs.
2012-03-23
20,515 reads
MaxiPalle,
2014-11-10 (first published: 2014-10-01)
--Gets number of days for a month. Leap years taken in consideration. CREATE FUNCTION dbo.fnGetMonthDays(@myDate DATE) RETURNS INT AS BEGIN DECLARE @isLeap INT = 0 IF (YEAR(@myDate) % 400 = 0 OR (YEAR(@myDate) % 4 = 0 AND YEAR(@myDate) % 100 !=0)) SET @isLeap=1 DECLARE @month INT = MONTH(@myDate) DECLARE @days INT SELECT @days = CASE WHEN @month=1 THEN 31 WHEN @month=2 THEN 28 + @isLeap WHEN @month=3 THEN 31 WHEN @month=4 THEN 30 WHEN @month=5 THEN 31 WHEN @month=6 THEN 30 WHEN @month=7 THEN 31 WHEN @month=8 THEN 31 WHEN @month=9 THEN 30 WHEN @month=10 THEN 31 WHEN @month=11 THEN 30 WHEN @month=12 THEN 31 END RETURN @days END
Should I be formatting dates on the database side? What tools can I use in SQL Server to format and parse dates? This article helps you decide which one best suits your needs.
This article presents an easy method to get YTD data grouped by months in T-SQL.
A task was set before our team to find the first Tuesday after 10 days from a specific date. This article shows how we accomplished that.
2011-10-13
1,549 reads
If you are still using the old Sybase DateTime datatype, it is a good idea to move your code to the more standard datatypes that were introduced in SQL Server 2008. Joe Celko explains why, and walks through some of the history of the TSQL way of storing and manipulating dates and times.
2011-05-25
3,801 reads
Fill in another bit of your T-SQL knowledge by learning how to sort a result set by the proper month order, but use the month name instead.
2019-04-12 (first published: 2010-11-15)
23,818 reads