开发者

How do I name a column as a date value

the results开发者_如何学编程 look like this but wher the column name says 'Today' I want it to be todays date.

How do I name a column as a date value


Try this technique:

declare @dt datetime
declare @sql varchar(100)
set @dt = getdate()
set @sql = 'select 1 as [ ' + convert( varchar(25),@dt,120) + ']'  
exec (@sql)

In your Case:

declare @dt datetime
declare @sql varchar(100)
set @dt = getdate()
set @sql = 'select 0 as [ ' + convert( varchar(25),@dt,120) + ']'  
exec (@sql)


I would return an integer representing a day offset and parse it in the client, failing that you are going to have to use dynamic SQL or do something with the underlying column name itself;;

declare @sql nvarchar(128) = '
select 
  col1,
  col2,
  0 as [' + cast(getdate() as nvarchar(32)) + ']
from T'
exec(@sql)

Or

--today
declare @now varchar(32) = cast(getdate() as varchar(32)) 

--result to temp table
select col1, col2, 0 as [Now] into #T from TheTable
--rename col
exec tempdb..sp_rename '#T.Now', @now, 'COLUMN'
--select
select * from #T
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜