How do I select the min from a function in SQL?
I want to take the difference between my date column and the current date, find the minimum and store it in a variable.
I have:
DECLA开发者_如何学编程RE @DAY AS INTEGER;
SET @DAY =
SELECT MIN (
SELECT DATEDIFF(DAY, date_col, GETDATE())
FROM tb1);
but the min function isn't accepting the select and the datediff function.
Try the following:
SELECT MIN (DATEDIFF(DAY, date_col, GETDATE()))
FROM tb1
You don't need the nested select.
select @day = min(DateDiff(DAY,date_col,GetDate())) from tbl
精彩评论