I want to compare MMSQL date together and i want to retrieve the nearest date from now.
Here is a sample of my mssql date data : 2013-01-23 09:34:03.000
My table is like this:
NAME : myTable
Column:
- nameUpdate (VARCHAR(255))
- date (DATETIME)
Here is what i've done so far but it won't work.
$query = "SELECT date FROM myTable";
$res_mnem = mssql_query($query) or die(mssql_get_last_message() . "[ " . $query . " ]");
$latestDate = "";
if (!mssql_num_rows($query)) {
while ($row = mssql_fetch_array($res_mnem)) {
if($latestDate == ""){
$latestDate = $row["date"];
}
elseif($row["date"] > $latestDate){
$latestDate = $row["date"];
}
}
}
echo $latestDate;
Thanks to all
SELECT TOP 1 DATEDIFF(s, [date], GetDate())
FROM myTable
ORDER BY DATEDIFF(s, [date], GetDate())
This does a date substraction between Current (GETDATE()) and [date] in your table, on seconds (you could do it at a lower granularity than seconds, if you need to). It orders by the same datediff, in ascending order. The top 1 selects only the first record--that's the record that is the closest to the current date because of the ORDER BY.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.