We help IT Professionals succeed at work.

I need to select the current Month (first 3 letters) and use that to select data from a column name in a table.

itdeptvnam
itdeptvnam asked
on
64 Views
Last Modified: 2017-03-31
The table has column names jan, feb ect
Example below of how I tried to access the column name I'm looking for results from Mar column in the #refGoals only based on the current month being March.

CREATE TABLE #RefGoals
(
      Typ                  char(50),
      RefTyp                  char(50),
      PercentAmt                  char(50),
      Jan                  int,
      Feb                  int,
      Mar                  int,
      Apr                  int,
      May                  int,
      Jun                  int,
      Jul                  int,
      Aug                  int,
      Sep                  int,
      Oct                  int,
      Nov                  int,
      Dec                  int

)

INSERT INTO #RefGoals (Typ, RefTyp, PercentAmt, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
VALUES('Cert','company1','19%',77,75,85,78,71,83,64,76,71,69,75,77)

INSERT INTO #RefGoals (Typ, RefTyp, PercentAmt, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
VALUES('Cert','company2','16%',67,65,74,68,61,72,56,66,62,60,65,67)

INSERT INTO #RefGoals (Typ, RefTyp, PercentAmt, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
VALUES('Cert','company3','2%',8,8,9,8,8,9,7,8,8,7,8,8)

INSERT INTO #RefGoals (Typ, RefTyp, PercentAmt, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
VALUES('Cert','company4','2%',10,10,11,10,9,11,8,10,9,9,10,10)

INSERT INTO #RefGoals (Typ, RefTyp, PercentAmt, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
VALUES('Cert','company5','3%',11,11,12,11,10,12,9,11,10,10,11,11)

Select (Left(DATENAME(MONTH, GETDATE()),3)) as dat, * from #RefGoals
Comment
Watch Question

Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
You would have to do some dynamic sql for that.
declare @colname varchar(3) = left(CONVERT(varchar(12), getdate(), 100), 3)

declare @tsql varchar(8000) = 'select Typ, RefType,  ' + @colName + ' from #RefGoals'
select @tsql   -- (this should show the command you want) 
exec (@tsql)  -- now execute it.

Open in new window

Author

Commented:
Its works, thank you.
Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Actually OP didn't say which  of the two proposed solutions worked.  Given that we can't know, I think the point should be evenly split.

Author

Commented:
Sorry about not closing the question Scott Pletcher solution worked well for me. I'm using now.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.