Deeptechtons
Deeptechtons

Reputation: 11125

TSQL Check if date of work completion is less than today

In a employee task application i have to find if the employee has finished the task on time or he is working overtime so i put together this to find it's not working

SELECT case when (CAST('2011-1-1' as datetime) < cast('2011-1-2' as datetime)) THEN 'Finished' ELSE 'UnFinished' end

well the dates are hardcoded here because i wanted to test the logic first before going on.

Edit

Sorry i missed the select before the select statement hence the error

Incorrect syntax near the keyword 'case'.

But now i want to know if there are more efficient ways to do above taking into account that compared fields are datetime

Upvotes: 0

Views: 4069

Answers (2)

Oleg Dok
Oleg Dok

Reputation: 21766

If you need TODAY and date of work finished is datetime, then better use this expression:

DECLARE @today DATETIME 
SET @today = CONVERT(varchar, GETDATE(), 112)

SELECT 
  case when WorkFinishedAt_Column < @today THEN 'Finished' ELSE 'UnFinished' end
FROM YourTable

OR

if you have only one value in variable

DECLARE @today DATETIME
SET @today = CONVERT(varchar, GETDATE(), 112)

SELECT 
  case when @WorkFinishedAt < @today THEN 'Finished' ELSE 'UnFinished' end

Upvotes: 2

Kambo_Rambo
Kambo_Rambo

Reputation: 1715

You cant use IF ? if (CAST('2011-1-1' as datetime) < cast('2011-1-2' as datetime)) select 'Finished' ELSE select 'UnFinished'

Upvotes: 0

Related Questions