Need to store difference in dates into an int

I am trying to put the difference in minutes, between two dates into an int in SQL server 2005. I get the error -

Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'SELECT'.

SQL -

DECLARE @id varchar(10)
DECLARE @diff int

SET @id = 'D5234'
SET @diff = SELECT DATEDIFF(minute, [PROC].[START_DATE], [PROC].[END_DATE]) 
FROM [PROCESSES] AS [PROC]
WHERE [PROC].[ID] = @id

How do I fix it ?

Answers


You either need to use:

SELECT @diff = DATEDIFF(minute, [PROC].[START_DATE], [PROC].[END_DATE]) 
FROM [PROCESSES] AS [PROC]
WHERE [PROC].[ID] = @id;

Or

SET @diff = (SELECT DATEDIFF(minute, [PROC].[START_DATE], [PROC].[END_DATE]) 
            FROM [PROCESSES] AS [PROC]
            WHERE [PROC].[ID] = @id);

(With your select statement in parentheses).

If you have multiple rows matching, the former will only set @diff to be the last row encountered, the latter will fail if you have multiple rows.


Need Your Help

Difference between @TypeChecked and @CompileStatic

groovy

Can someone explain the difference between @TypeChecked and @CompileStatic?