SQLite - select row by year only? - sql

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.

Related

Why this gives an error when I run in snowflake?

add_months(CURRENT_DATE() - 1,'MONTH')
throws an error
Invalid argument types for function '>=': (ROW(TIMESTAMP_NTZ(9), VARCHAR(5)), DATE)
Correct syntax would be like below as per documentation:
select current_date(), add_months(current_date(), 1);
In Oracle and Snowflake, the syntax is:
ADD_MONTHS( <date_or_timestamp_expr> , <num_months_expr> )
So you would want:
add_months(CURRENT_DATE, -1)
(Note: CURRENT_DATE() can also be used in Snowflake but not in Oracle.)

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!

Error while executing 'select convert' on hive table

I want to execute this sql query on a hive table:
select * from sampleDB.sampleTable
where sampleDate>=(select convert(DATE, dateadd(MONTH,-6,getdate())));
But I am getting this error: Error while compiling statement: FAILED: parse exception line 1:64 cannot recognize input near 'select' 'convert' '(' in expression specification (state=42000,code=40000)
Can someone help me understand how this can be achieved? Basically I want to filter on date say 6 months from current date.
Thanks!
Hive do not supports only >= in sub query it support only below type of sub query
Scalar subqueries
IN subqueries
EXISTS subqueries
you can achieve the same with daenter link description hereteformate
select * from sampleDB.sampleTable
where sampleDate>= date_format(current_date - interval '7' day,'%Y-%m-%d') ;

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

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.
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)