質問

I only want to keep durations less than 10 minutes long. my current code is as follows:

Duration = DateDiff(ss, TimeBegin, TimeEnd)

TimeBegin and TimeEnd are in TIME format. Obviously Duration right now comes back as:

00:00:07

That's where I'm running into trouble. Can I use a statement that looks like this:

<= 00:10:00 or <= '00:10:00'

Essentially I want:

Duration = (Datediff(ss, TimeBegin, TimeEnd) *ONLY IF LESS THAN 10 MINUTESg)

I already state earlier in the query that if no result is returned to create a NULL, so when a duration is Greater than 10 minutes, I just want it to be ignored as if it didn't exist.

役に立ちましたか?

解決

DateDiff(ss, TimeBegin, TimeEnd) gives you the difference in seconds. Just use a Case statement to return the value only if that's under 600 (...ELSE Null is implied):

set @Duration = CASE
 WHEN DateDiff(ss, @TimeBegin, @TimeEnd) < 600
 THEN DateDiff(ss, @TimeBegin, @TimeEnd)
END;

他のヒント

Agreed with @aucuparia. But (this is for topic starter) be careful with using datediffs is seconds instead of minutes. Their behavior isn't the same, like months/years datediffs. I mean server rounds your operands:

select datediff(mi,'2014-05-15 19:00:00','2014-05-15 19:10:59')

is not the same like

select datediff(ss,'2014-05-15 19:00:00','2014-05-15 19:10:59')

Just execute them both and see the difference. The first one still is 10-minutes difference, but the second will be cut off by 600-seconds 'where' clause.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top