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.
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.
The answer is Yes.
Remarks. The maximum valid date for a SqlDateTime structure is December 31, 9999.
DECLARE @MAXDATE DATETIME
SELECT @MAXDATE = MAX(DateVal)
FROM YourTable
WHERE ID = @ID
SELECT @Variable = Date
FROM YourTable
WHERE ID = 1
ORDER BY Date
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With