Beginning of start and end dates - from this year yyyy

In MSSQL, given the year “yyyy”, how can I return the start date as 01/01 / yyyy and the end date as 12/31 / yyyy.

+4
source share
4 answers
declare @yyyy int; set @yyyy = 2009; select DATEADD(yy,@yyyy-1900,0), DATEADD(yy,@yyyy-1899,-1) 
+4
source

Are you using SQL Server 2012 (latest version)?

If so, you can use the new DATEFROMPARTS or DATETIMEFROMPARTS .

Example from the first link:

 SELECT DATEFROMPARTS ( 2010, 12, 31 ) AS Result; 

... will return this:

 Result ---------------------------------- 2010-12-31 (1 row(s) affected) 
+1
source

You can try this as well if using a MySQL database, -

  SELECT CONCAT ('01/01','/',2009) start_date ,concat ('12/31','/',2009) end_date FROM table_name 
0
source

Query:

 declare @yyyy int; set @yyyy = 2012; SELECT convert(varchar,DATEADD(yyyy, @yyyy - 1900, 0), 101) AS StartDate, convert(varchar,DATEADD(yyyy, @yyyy - 1899, -1), 101) AS EndDate --OR SELECT convert(varchar,'01/01/'+CAST(@yyyy as varchar(4)), 101) AS StartDate, convert(varchar,'12/31/'+CAST(@yyyy as varchar(4)), 101) AS EndDate 

Result:

 StartDate EndDate 01/01/2012 12/31/2012 
0
source

All Articles