SQL Server: Assign a dynamic column name using current DATENAME as column name -


i using following query return dates in current month in m/d/yyyy format:

select distinct format(findetail.endtime, 'm/d/yyyy') position join findetail on position.position = findetail.position join product on position.product = product.product month(findetail.endtime) = month(getdate()) , year(findetail.endtime) = year(getdate())      order format(findetail.endtime, 'm/d/yyyy') 

this returns single column every date current month column not named.

 | (no column name) | _|__________________|_  |     2/1/2016     |  |     2/10/2016    |  |     2/11/2016    |  |     2/12/2016    |  |     2/13/2016    |  |        ...       | 

for gins, wondering if there way use current date's datename column name in query isn't hard coded? ocd purposes only, isn't accomplishing curious if possible. using following query date name:

select datename(month, dateadd(month, month(getdate()), 0) -1) 

returns february. have gathered way accomplish using dynamic sql have never messed with. appreciated. goal return:

 |     february     | _|__________________|_  |     2/1/2016     |  |     2/10/2016    |  |     2/11/2016    |  |     2/12/2016    |  |     2/13/2016    |  |        ...       | 

** updated **

thanks pointing me in right direction. here got.

query:

declare @sql nvarchar(max); set @sql = 'declare @datefrom date, @dateto date; set @datefrom = convert(varchar(10),dateadd(m,-1,dateadd(mm, datediff(m,0,getdate())+1, 0)), 101); -- first day of previous month set @dateto = convert(varchar(10),dateadd(d,-1,dateadd(mm, datediff(m,0,getdate())+1,0)), 101); -- last day of current month dates (     select @datefrom datesinmonth     union     select dateadd(d,1,datesinmonth) dates datesinmonth < @dateto ) select format(datesinmonth, ''m/d/yyyy'') '+ datename(month, getdate()) +' dates'; exec sp_executesql @sql 

returns:

 |     february     | _|__________________|_  |     2/1/2016     |  |     2/2/2016     |  |     2/3/2016     |  |     2/4/2016     |  |     2/5/2016     |  |        ...       | 

reworked query order results correctly instead of ordering first character, , using @jamied77's suggestion remove scaler function.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -