0

I have a table with 60 columns: [Actual_Deb01],[Actual_Deb02],[Actual_Deb03],.......,[Actual_Deb60]

one of the columns in the queries is period and I only want to fetch the actual_debt for the period.

how can I use the period to build the actual_deb col name I want to fetch. something like:

SELECT  Actual_Deb04],
        period,
        ['Actual_Deb'+period]
FROM table

period in the above is 03 so I want the query to effectively be:

SELECT  [Actual_Deb04],
        period,
        [Actual_Deb04]
FROM table

Can i do this in the query syntax?

Thanks in advance as always, R

1
  • so I want the query to be effectively actual_deb04 or 03? Commented Mar 13, 2016 at 7:03

1 Answer 1

1

you can do it with dynamic sql,but what if you have multiple periods ??

declare @sql nvarchar(max)
set @sql=
'select
      [Actual_Deb04],period ,'
      +'Actual_Deb'+period+' FROM table;'

  exec(@sql)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.