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.

1

1 Answer 1

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
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.