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

How to convert an NSString to an unsigned int in Cocoa?

objective-c cocoa nsstring unsigned

My application gets handed an NSString containing an unsigned int. NSString doesn't have an [myString unsignedIntegerValue]; method. I'd like to be able to take the value out of the string without

Adding button to QTableview

c++ qt qt4

I have created one table by using QTableview and QAbstractTableModel .