How do I name a column as a date value

后端 未结 2 1840
名媛妹妹
名媛妹妹 2021-01-24 05:02

the results look like this but wher the column name says \'Today\' I want it to be todays date.

\"enter

相关标签:
2条回答
  • 2021-01-24 05:32

    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)
    
    0 讨论(0)
  • 2021-01-24 05:45

    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 讨论(0)
提交回复
热议问题