0

is it possible to get something like concat('TMVDAY',DATEPART(dw,GETDATE())-1) as column header? so the column header (assuming today is Friday (starting from Monday)) will be "TMVDAY5"

best I can think is some thing like this: SELECT GETDATE() as concat('TMVDAY',DATEPART(dw,GETDATE())-1);

I'm using ms sql server.

5
  • Is it possible? Yes - using dynamic sql. But usually "column header" implies a resultset which you do not discuss and your query generates a scalar value. It might be more effective to discuss your goal.
    – SMor
    Jan 15, 2021 at 19:24
  • Yes stuff like that is usually handled by the application displaying the data and formatting/etc (UI, webiste, other)
    – Brad
    Jan 15, 2021 at 19:34
  • You should fix your data model. You are storing data in different columns that should be in separate rows. Jan 15, 2021 at 20:41
  • You might check out this answer. stackoverflow.com/questions/45706630/… Jan 15, 2021 at 21:04
  • @SMor: The idea is that, I need to select from correct column with out writing too many if (or when) statements Jan 17, 2021 at 16:22

1 Answer 1

0

It could be achieved through dynamic SQL:

declare @header varchar(20)
declare @dsql varchar(200)

set @header=concat('TMVDAY',DATEPART(dw,GETDATE())-1)
set @dsql = 'select getdate() ' + @header 

EXEC sp_sqlexec  @dsql

But it would be better if you express your actual situation if it's not just a simple scaler value.

1
  • You are welcome. If you feel that this answer solved your problem you might accept my answer by clicking green check. This way focuses would be more on unsolved problems. Mar 6, 2021 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.