3

I am new to SQL statements and I am guessing this is something obvious so I apologize ahead of time for the newbie question.

Here is the code:

use test;
declare @time_24_2 int;
declare @hours_diff_2 int;

SELECT 
    end_date_full 
FROM 
    example 
WHERE 
    datediff(hour, getdate(), end_date_full) < 24
GROUP BY 
    end_date_full

SET @time_24_2 = end_date_full;
select @time_24_2

This is my code and I am simply trying to pull the end_date_full column of every row that fits the criteria. However every time I try to use the column end_date_full like seen below I get an error that says Unknown column name.

Any help would be amazing!

P.S end_date_full is a column name and it works for the first select statement, just not the SET statement.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bryce
  • 447
  • 2
  • 9
  • 24
  • Duplicate?: [How to set variable from a SQL query?](http://stackoverflow.com/q/3974683/456814). –  Aug 14 '14 at 08:24

1 Answers1

4

That's because you are trying to use the column name outside a query when you assign it to variable. This should do the trick:

SELECT @time_24_2 = end_date_full 
FROM example 
WHERE datediff(hour, getdate(), end_date_full) < 24
group by end_date_full
shree.pat18
  • 21,449
  • 3
  • 43
  • 63