Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 657
  • Last Modified:

SQL syntax error near '=' using CAST([Table].Column AS DATE

Hello Experts;

The following query generates a "syntax error near '=' in the CAST([Transaction].Time AS Date) = @TransTime" function;

declare @TransTime as date = :TransTime
declare @TransTime2 as date = :TransTime2

SELECT
SUM(CASE WHEN [Transaction].CashierID = 4
THEN [Transaction].Total ELSE 0 END) AS Bill
FROM [Transaction]
WHERE [Transaction].Time
BETWEEN
CAST([Transaction].Time AS Date) = @TransTime
AND
CAST([Transaction].Time AS Date) = @TransTime2

The CAST AS Date works using a single date but adding the BETWEEN, AND functions for a second CAST AS Date function creates the error. I must be doing something wrong in attempting to query between two dates but I cannot figure why, any help would be greatly appreciated.

Thank you,
Visionetv
0
visionetv
Asked:
visionetv
  • 2
1 Solution
 
Jan LouwerensCommented:
The syntax is supposed to be:
BETWEEN <date1> AND <date2>

but it looks like you're doing:
BETWEEN <assignment_operation> AND <assignment_operation>
(Not to mention that these assignment operations are illegal, since the left sides are not assignable.)

Do you maybe just want?
BETWEEN @TransTime AND @TransTime2
0
 
MurfurFull Stack DeveloperCommented:
BETWEEN
@TransTime
AND
@TransTime2
0
 
MurfurFull Stack DeveloperCommented:
Apologies Jan, your answer! (There were no answers here when I first loaded the page...)
0
 
Scott PletcherSenior DBACommented:
...
WHERE CAST([Transaction].Time AS date)
 BETWEEN @TransTime AND @TransTime2
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now