I am looking for some T-SQL code that should pick the date which is the "last Sunday in the month of January".

For example:

```
Current day      expected result
2017-01-29       2016-01-31
2017-02-05       2017-01-29
2017-02-19       2017-01-29
2018-01-28       2017-01-29
2018-02-04       2018-01-28

```

This is because the year starts from last Sunday in January 

I have some T-SQL code which is being used in SQL Server 2014:

```
select 
    convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), GETDATE(), 112) + '0101')), 30)) / 7  * 7, '19000107'), 120)

```

If the current date is between February to December then the above code returns the correct answer. But if we set current date as '2017-01-25' (or any other dates in January), then the result is wrong. 

For the above date (2017-01-25) answer should be '2016-01-31'