sql - Why are the dates between a given range not getting picked? -
i trying pick dates between ranges. have hard coded date , month year dynamically concatenated it. trying pick dates between these ranges table doesn't give dates between given range.
declare @year varchar(10) set @year= '2017-2018' declare @yearfrom varchar(15) set @yearfrom= (select substring(@year, 1, 4) ) declare @yearto varchar(15) set @yearto= (select substring(@year, 6, 9) ) set @yearfrom= '01-07-'+@yearfrom set @yearto= '30-06-'+@yearto select convert(varchar(11),c.registrationdate,106) contractors c convert(varchar(11),c.registrationdate,106) between convert(varchar(11),@yearfrom,106) , convert(varchar(11),@yearto,106)
the registration date saved as:
yyyy-mm-dd e.g. 2017-07-03
another way solve using datefromparts, year, month , day inputs converted dates, follows:
declare @year varchar(10) set @year= '2017-2018' declare @yearfrom int set @yearfrom= (select substring(@year, 1, 4) ) declare @yearto int set @yearto= (select substring(@year, 6, 9) ) declare @startdate date set @startdate = datefromparts(@yearfrom,1,7) declare @enddate date set @enddate = datefromparts(@yearto,6,30)
now can use @startdate , @enddate in comparison registration date.
Comments
Post a Comment