SQL Server gives syntax error - sql

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.

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)

You can simply use this query, without adding date:
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN '2015-09-12' AND '2015-09-15'

Date literals are tricky... besides the problem with conversion (often depending on your system's culture) you must think of the day's end if you use between!
Here you find a system independent way to literally write dates: https://msdn.microsoft.com/en-us/library/ms187819.aspx?f=255&MSPPError=-2147217396 (look at "ODBC)
So you could write
BETWEEN {d'2015-09-12'} AND {ts'2015-09-12 23:59:59'}

SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN CAST('2015-09-12' AS DATE)
AND CAST('2015-09-15' AS DATE)

Related

What causes error "Strings cannot be added or subtracted in dialect 3"

I have the query:
WITH STAN_IND
AS (
SELECT ro.kod_stanow, ro.ind_wyrob||' - '||ro.LP_OPER INDEKS_OPERACJA, count(*) ILE_POWT
FROM M_REJ_OPERACJI ro
JOIN M_TABST st ON st.SYMBOL = ro.kod_stanow
WHERE (st.KOD_GRST starting with 'F' or (st.KOD_GRST starting with 'T') ) AND ro.DATA_WYKON>'NOW'-100
GROUP BY 1,2)
SELECT S.kod_stanow, count(*) ILE_INDEKS, SUM(ILE_POWT-1) POWTORZEN
from STAN_IND S
GROUP BY S.kod_stanow
ORDER BY ILE_INDEKS
That should be working, but I get an error:
SQL Error [335544606] [42000]: Dynamic SQL Error; expression evaluation not supported; Strings cannot be added or subtracted in dialect 3 [SQLState:42000, ISC error code:335544606]
I tried to cast it into bigger varchar but still no success. What is wrong here? Database is a Firebird 2.1
Your problem is 'NOW'-100. The literal 'NOW' is not a date/timestamp by itself, but a CHAR(3) literal. Only when compared to (or assigned to) a date or timestamp column will it be converted, and here the subtraction happens before that point. And the subtraction fails, because subtraction from a string literal is not defined.
Use CAST('NOW' as TIMESTAMP) - 100 or CURRENT_TIMESTAMP - 100 (or cast to DATE or use CURRENT_DATE if the column DATA_WYKON is a DATE).

Presto SQL + Athena: Cast date fail with error message: INVALID_FUNCTION_ARGUMENT

I have a table with datehourminute column with string datatype and formatted as 202012062302. I want to select the date and cast it to date, so I made a query that looks like this:
select cast(date_parse(created_date, '%Y-%m-%d') as date) created_date,
daily_user
from
(
select concat(substr(datehourminute,1,4),'-',
substr(datehourminute,5,2),'-',substr(datehourminute,7,2))
as created_date, sum(users) as daily_user
from "a-schema".a-table
group by 1
)
order by 1;
However, that query returns this error:
SQL Error [100071] [HY000]: [Simba]AthenaJDBC
An error has been thrown from the AWS Athena client.
INVALID_FUNCTION_ARGUMENT: Invalid format: "(oth-er-)"
Any idea how to fix this? Thanks!

What is wrong with sql query

What is wrong with this sql query. I am getting syntax error near convert
"SELECT * FROM collections c where c.submittedBy='679' AND CONVERT(varchar, c.eventTime, 103) ='2017-06-21'";
The eventTime is "eventTime": "2017-06-21T12:20:03.9366135+05:30"
The error is
{"Message: {\"errors\":[{\"severity\":\"Error\",\"location\":{\"start\":64,\"end\":71},\"code\":\"SC1001\",\"message\":\"Syntax error, incorrect syntax near 'CONVERT'.\"}]}\r\nActivityId: a0c51c53-fca0-4c81-8f10-65348749e3d2"}
The best way to write this query (across databases) is:
SELECT c.*
FROM collections c
WHERE c.submittedBy='679' AND
c.eventTime >= '2017-06-21' AND
c.eventTime < '2017-06-22';
This allows the database to use an appropriate index for filtering. Note that if submittedBy is a number, then don't use single quotes.
convert to date before to convert to varchar
SELECT *
FROM collections c
where c.submittedBy='679'
AND CONVERT(varchar(10), CONVERT(date, c.eventTime, 103)) = '2017-06-21'
if eventTime is already a datetime field should work also this way..
SELECT *
FROM collections c
where c.submittedBy='679'
AND CONVERT(date, c.eventTime) = '2017-06-21'

Google BigQuery Date(String format) failed to convert/cast as Date

I am trying to convert/cast the date in the big query into date format. My query is like this:
SELECT CAST(t.date AS date)
FROM `table` t;
But I got an Error code of Invalid date:'20151108'. it gave me different error date when I run the query.
Any thoughts?
try
SELECT PARSE_DATE('%Y%m%d', t.date) FROM table t

SQLite - select row by year only?

How can I select rows from a table by year only?
SELECT * FROM speckdata AS s WHERE DATE_FORMAT(s.localdate, '%Y') = '2014')
I could get the result in MySQL
Now on sqlite I get this error,
SQLiteManager: Likely SQL syntax error: SELECT*
FROM speckdata AS s
WHERE DATE_FORMAT(s.localdate, '%Y') = '2014') [ near ")": syntax error ] Exception Name: NS_ERROR_FAILURE Exception Message: Component
returned failure code: 0x80004005 (NS_ERROR_FAILURE)
[mozIStorageConnection.createStatement]
Any ideas?
EDIT:
If I change it to,
WHERE DATE(s.localdate, '%Y') = '2014'
It return no result.
BTW, the localdate is in date-time format such as 2014-10-09 14:59:53
Try this:
SELECT * FROM speckdata AS s WHERE strftime('%Y', s.localdate) = '2014'
Since DATE_FORMAT wouldn't work, maybe you can try an alternative solution
SELECT * FROM speckdata AS s WHERE DATE(s.localdate) LIKE '2014%'
P.S. You called DATE instead of DATE_FORMAT in your edit, I don't know it's intended or an accident on you.