sql - Split the given date into days -
i writing stored procedure. need take year , month information user , doing something. problem don't detect given month has how many days. let me explain example;
user give me year , month said;
@year = 2015 @month = 07
so must create rows like;
2015-07-01 2015-07-02 ..... 2015-07-31
my plan adding these rows 1 one temp table.
last status of sp is;
alter procedure [dbo].[usp_createstats] ( @year varchar (40), @month varchar (40) ) begin declare @starttime varchar (10) declare @endtime varchar (10) set @starttime = '00:00:00' set @endtime = '23:59:59' declare @peoples table (name varchar(50)) insert @peoples (name) select distinct name userinfo declare @dates table (date varchar(50)) end
i hope explained correctly.
i've changed year , month variables int
data type, name of date column dates_date
, , used convert
, while loop dateadd
function populate @dates
table:
alter procedure [dbo].[usp_createstats] ( @year int, @month int ) begin declare @starttime varchar (10), @endtime varchar (10), @date date set @starttime = '00:00:00' set @endtime = '23:59:59' set @date = convert(date, right('0000' + cast(@year char(4)), 4) + right('00' + cast(@month char(2)), 2)+ '01', 112) declare @peoples table (name varchar(50)) insert @peoples (name) select distinct name userinfo declare @dates table (dates_date date) while month(@date) = @month begin insert @dates (dates_date) values (@date) set @date = dateadd(day, 1, @date) end end
Comments
Post a Comment