With the help of a numbers table and a splistring function.
MS SQL Server 2012 Schema Setup:
create table dbo.Number(N int primary key);
go
insert into dbo.Number(N)
select top(11000) row_number() over(order by 1/0) - 1
from sys.all_objects as o1, sys.all_objects as o2
go
create function [dbo].[SplitString](@List nvarchar(max), @Delimiter nvarchar(255)) returns table
with schemabinding
as
return
(
select substring(@List, N, charindex(@Delimiter, @List + @Delimiter, N) - N) as Item
from dbo.Number
where N <= convert(int, len(@List)) and
substring(@Delimiter + @List, N, len(@Delimiter)) = @Delimiter
);
Query 1:
declare @S nvarchar(max) = '2014-01,2014-02'
select T2.D
from dbo.SplitString(@S, N',') as S
cross apply (select cast(S.Item+'-01' as date)) as T1(D)
cross apply (
select dateadd(day, N.N, T1.D)
from Number as N
where N between 0 and datediff(day, T1.D, eomonth(T1.D))
) as T2(D)
| D |
|------------|
| 2014-01-01 |
| 2014-01-02 |
| 2014-01-03 |
| 2014-01-04 |
| 2014-01-05 |
| 2014-01-06 |
| 2014-01-07 |
| 2014-01-08 |
| 2014-01-09 |
| 2014-01-10 |
| 2014-01-11 |
| 2014-01-12 |
| 2014-01-13 |
| 2014-01-14 |
| 2014-01-15 |
| 2014-01-16 |
| 2014-01-17 |
| 2014-01-18 |
| 2014-01-19 |
| 2014-01-20 |
| 2014-01-21 |
| 2014-01-22 |
| 2014-01-23 |
| 2014-01-24 |
| 2014-01-25 |
| 2014-01-26 |
| 2014-01-27 |
| 2014-01-28 |
| 2014-01-29 |
| 2014-01-30 |
| 2014-01-31 |
| 2014-02-01 |
| 2014-02-02 |
| 2014-02-03 |
| 2014-02-04 |
| 2014-02-05 |
| 2014-02-06 |
| 2014-02-07 |
| 2014-02-08 |
| 2014-02-09 |
| 2014-02-10 |
| 2014-02-11 |
| 2014-02-12 |
| 2014-02-13 |
| 2014-02-14 |
| 2014-02-15 |
| 2014-02-16 |
| 2014-02-17 |
| 2014-02-18 |
| 2014-02-19 |
| 2014-02-20 |
| 2014-02-21 |
| 2014-02-22 |
| 2014-02-23 |
| 2014-02-24 |
| 2014-02-25 |
| 2014-02-26 |
| 2014-02-27 |
| 2014-02-28 |