Jonathan Winks
Jonathan Winks

Reputation: 1101

Converting MSAccess Queries to SQL?

I've been trying to modify and input the following the query from an Access database into a SQL Server database but keep getting errors, e.g. "int is not a recognised built in function name".

Any way around this?

    SELECT Tasks.Task_id, 
          Days_1.next_working_day AS Day_date

    FROM Intervals 
INNER JOIN ((Days INNER JOIN Days AS Days_1 ON Days.day_of_week = Days_1.day_of_week) 
INNER JOIN Tasks ON Days.Day_date = Tasks.Start_date) 
ON Intervals.Interval_id = Tasks.Interval_id


WHERE (((Days_1.next_working_day)>=[tasks].[start_date])
 AND 
((Intervals.Interval_Type_Name)="Fortnightly") 
AND 
((DateDiff("d",[tasks].[start_date],[days_1].[day_date])/14-int(DateDiff("d",[tasks].[start_date],[days_1].[day_date])/14)<>0)=0))
ORDER BY Days_1.next_working_day;

Thanks!

Upvotes: 0

Views: 52

Answers (1)

Manny
Manny

Reputation: 977

The culprit is in your WHERE clause:

... -int(DateDiff("d",[tasks].[start_date],[days_1].[day_date])/14)

...which is not valid out of the box (no such built-in function exists). Presumably you're trying to do some rounding with a cast. See CAST and CONVERT for T-SQL. You might also want to look into functions CEILING and FLOOR. I would be remiss if I didn't suggest revisiting that logic for what you're trying to do.

Regarding the query itself, I gave your WHERE clause a second look. As Gordon Linoff points out, what <>0)=0) is there for is unclear and is going to be the next error you run into. It looks like maybe the original query intended on extracting 2 week intervals in their entirety in a roundabout way. If so, then my best guess as to what that part of the clause should look like is:

(
    DateDiff("d", [tasks].[start_date], [days_1].[day_date]) / 14.0 -
    DateDiff("d", [tasks].[start_date], [days_1].[day_date]) / 14 
    = 0
)

Note now the division by 14.0 and not on the 2nd calculation which will always return an int because of integer division (so that cast becomes unnecessary).

Better yet, use modulo:

(
    DateDiff("d", [tasks].[start_date], [days_1].[daydate]) % 14 = 0
)

I really am not sure if this is what you're going after but it's my best guess without more information.

Upvotes: 3

Related Questions