Assign a value with a maximum date to a variable

I have a table with ID and date fields, I need to assign a new 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 the variable 2011-02-02 .

+6
source share
3 answers
 DECLARE @MAXDATE DATETIME SELECT @MAXDATE = MAX(DateVal) FROM YourTable WHERE ID = @ID 
+16
source
 SELECT @Variable = Date FROM YourTable WHERE ID = 1 ORDER BY Date 
+1
source

Alternativelly:

 DECLARE @yourDate DATETIME SET @yourDate = (SELECT MAX(date) FROM yourTable WHERE ID = 1); 
0
source

Source: https://habr.com/ru/post/904249/


All Articles