Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server gives syntax error

I have following SQL for Microsoft SQL Server

SELECT * 
FROM tblA
WHERE CREATION_DATE BETWEEN DATE'2015-09-12' AND DATE'2015-09-15'

But this throws a syntax error:

Error: Incorrect syntax near '2015-09-12'. SQLState: S0001 ErrorCode: 102

What is wrong? I want to use ANSI literal code.

like image 795
Jonas Avatar asked May 06 '26 01:05

Jonas


2 Answers

This is not how you cast in MS SQL Server. Instead, you should try the cast syntax:

SELECT * 
FROM   tblA
WHERE  creation_date BETWEEN CAST('2015-09-12' AS DATE) AND 
                             CAST('2015-09-15' AS DATE)
like image 141
Mureinik Avatar answered May 08 '26 16:05

Mureinik


You can simply use this query, without adding date:

SELECT * 
FROM tblA
WHERE CREATION_DATE BETWEEN '2015-09-12' AND '2015-09-15'
like image 34
Shilpa Soni Avatar answered May 08 '26 14:05

Shilpa Soni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!