I had no idea T-SQL's CONVERT had special codes for converting dates. Well, it does.

From How to use ROW_NUMBER() to enumerate and partition records in SQL Server - CodeProject:

Here is our final SQL statement, which achieves the business logic we wanted to implement.

SELECT
       [PersonID]
      ,[FamilyID]
      ,[FirstName]
      ,[LastName]
      ,[DateOfBirth]
      ,ROW_NUMBER() over(PARTITION BY FamilyID,
                         CONVERT(NVARCHAR(25), DateOfBirth, 111)
                         ORDER BY DateOfBirth ASC) TwinCode
  FROM [People]
ORDER BY PersonID

IIn the ROW_NUMBER function above, I am doing several things. Iโ€™m grouping on FamilyID, and also grouping on a converted DateOfBirth. I convert the DateOfBirth to an nvarchar using the 111 conversion code, because that gets results like โ€˜2009/10/11โ€ฒ and โ€˜2009/10/12โ€ฒ which can easily be grouped by to achieve distinct dates. [emph mine -mfn]

The full list is here, at docs.microsoft.com. Here's a taste...

Without century (yy) (1)With century (yyyy)StandardInput/Output (3)
-0ย orย 100(1,2)Default for datetime and smalldatetimemon dd yyyy hh:miAM (or PM)
1101U.S.1 = mm/dd/yy
101 = mm/dd/yyyy
2102ANSI2 = yy.mm.dd
102 = yyyy.mm.dd

Labels: , ,