SQL Server February exceeded to march -


i have problem in regards february dates:

the result this:

payperiod -------------- 02/15/2017 03/02/2017 

expected result should this:

payperiod -------------- 02/15/2017 02/28/2017 

my code:

create procedure [dbo].[get15thdaypayperiod]             @mindate date = null,             @maxdate date = null -- execute sp: -- exec [get15thdaypayperiod] @mindate = '02/01/2017', @maxdate = '02/28/2017'   -- declare @mindate date = convert(varchar(15),dateadd(month, datediff(month, 0, getdate()), 0), 101),  -- @maxdate date = convert(varchar(15),eomonth(getdate()),101), -- @mindate date = '20170901',  -- @maxdate date = '20170930', declare @date date;  declare @counter int = 0;   if isnull(@mindate, 0) = 0 begin     set @mindate = convert(varchar(15),dateadd(month, datediff(month, 0, getdate()), 0), 101) end  if isnull(@maxdate, 0) = 0  begin     set @maxdate = convert(varchar(15),eomonth(getdate()),101) end  declare my_cursor cursor local static read_only forward_only    select top (datediff(day, @mindate, @maxdate) + 1) date =    dateadd(day, row_number()                   over(                     order    a.object_id) - 1, @mindate)      sys.all_objects           cross join sys.all_objects b;  create table #temp (biweekly varchar(15)) open my_cursor   fetch next my_cursor @date   while @@fetch_status = 0    begin        if( @counter = 15 )          begin         --print @date         --print @date -- here 15th date           if day(@date) between 10 , 30               begin                 set @date = dateadd(day,(15-datepart(day,@date)),@date)                 insert #temp values (convert(varchar(15), @date, 101))                 print convert(varchar(15), @date, 101)                 set @counter = 0               end               if  @counter = 0                begin                     set @date = dateadd(day,(30-datepart(day,@date)),@date)                     insert #temp values (convert(varchar(15), @date, 101))                     print convert(varchar(15), @date, 101)               end         end        set @counter = @counter + 1        fetch next my_cursor @date    end   close my_cursor  deallocate my_cursor  select payperiod = case when datename(weekday, biweekly) <> ('saturday') , datename(weekday, biweekly) <> ('sunday')                     convert(varchar(15), biweekly, 101)                     else case when datepart(dw, biweekly) <= 2                         convert(varchar(15),dateadd(day, -1 * (datepart(weekday, biweekly) + 1), biweekly), 101)                         else convert(varchar(15), dateadd(day, datepart(weekday, biweekly) * -1 + 6, biweekly), 101)                         end                     end  #temp drop table #temp  return 0 

other dates works fine, problem on month of february.

i tried adding condition inside if @counter = 0 no luck...

would able need using date functions?

select distinct      monthstart     = dateadd(month, datediff(month, 0,[date] )  , 0)   , month15th      = dateadd(day,14,dateadd(month, datediff(month, 0,[date] ), 0))   , monthend       = dateadd(day,-1,dateadd(month, datediff(month, 0,[date] )+1, 0))   --, monthendeom    = eomonth(getdate()) /* sql server 2012+*/ dates; 

using adhoc dates table example:

declare @fromdate date = '20170101' declare @thrudate date = '20171231'  ;with n (select n (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n)) , dates (   select top (datediff(day, @fromdate, @thrudate)+1)       [date]=convert(date,dateadd(day,row_number() over(order (select 1))-1,@fromdate))   n deka cross join n hecto cross join n kilo                  cross join n tenk cross join n hundredk    order [date] )  select distinct      monthstart = dateadd(month, datediff(month, 0,[date] )  , 0)   , month15th  = dateadd(day,14,dateadd(month, datediff(month, 0,[date] ), 0))   , monthend   = dateadd(day,-1,dateadd(month, datediff(month, 0,[date] )+1, 0))   --, monthendeom    = eomonth(getdate()) /* sql server 2012+*/ dates; 

for 2017, returns:

+------------+------------+------------+ | monthstart | month15th  |  monthend  | +------------+------------+------------+ | 2017-01-01 | 2017-01-15 | 2017-01-31 | | 2017-02-01 | 2017-02-15 | 2017-02-28 | | 2017-03-01 | 2017-03-15 | 2017-03-31 | | 2017-04-01 | 2017-04-15 | 2017-04-30 | | 2017-05-01 | 2017-05-15 | 2017-05-31 | | 2017-06-01 | 2017-06-15 | 2017-06-30 | | 2017-07-01 | 2017-07-15 | 2017-07-31 | | 2017-08-01 | 2017-08-15 | 2017-08-31 | | 2017-09-01 | 2017-09-15 | 2017-09-30 | | 2017-10-01 | 2017-10-15 | 2017-10-31 | | 2017-11-01 | 2017-11-15 | 2017-11-30 | | 2017-12-01 | 2017-12-15 | 2017-12-31 | +------------+------------+------------+ 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -