Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top