Abhilash JA Abhilash JA - 15 days ago 7
SQL Question

Sql Query - Select date only

I have a select query.

SELECT NEXT_SUN = DATEADD(day, (8 + @@DATEFIRST - DATEPART(dw, '2016-11-22 10:38:34.260')) % 7, '2016-11-22 10:38:34.260' )


From this query I want to select only date

Answer

You can try using CONVERT with DATE option.

SELECT NEXT_SUN = CONVERT(DATE,DATEADD(day, (8 + @@DATEFIRST - DATEPART(dw, '2016-11-22 10:38:34.260')) % 7, '2016-11-22 10:38:34.260' ))

You can also use Format() to get the desired result. If you want your return value to be converted into varchar you can do that also using CONVERT. More details on this are here