Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign value with max date to variable

Tags:

sql-server

I have table with fields ID and date, I need to assign newest date to a variable where id is some number.

So if I have dates 2011-01-01 and 2011-02-02 where ID = 1, I need to assign 2011-02-02 to a variable.

like image 849
el ninho Avatar asked Dec 21 '11 14:12

el ninho


People also ask

Can we use MAX function on date?

MAX() function will give you the maximum values from all the values in a column. MAX function works with “date” data types as well and it will return the maximum or the latest date from the table.

Can min and max be used on dates in SQL?

The answer is Yes.

What is the max value for datetime in SQL Server?

Remarks. The maximum valid date for a SqlDateTime structure is December 31, 9999.


2 Answers

DECLARE @MAXDATE DATETIME

SELECT @MAXDATE = MAX(DateVal)
FROM YourTable
WHERE ID = @ID
like image 136
Garrett Vlieger Avatar answered Sep 28 '22 05:09

Garrett Vlieger


SELECT @Variable = Date
FROM YourTable
WHERE ID = 1
ORDER BY Date
like image 45
misha Avatar answered Sep 28 '22 06:09

misha