SQL query not returning data - sql

I am trying to retrieve data from a Intersystems Cached database using a where clause with a timestamp, but nothing works.
Query:
select *
from dbo.iSkillsetStat
where Timestamp >= '2014-07-29 00:00:00'
Error:
ERROR: [SQLCODE: <-4>:<A term expected, beginning with one of
the following: identifier, constant, aggregate, %ALPHAUP, %EXACT,
%MVR, %SQLSTRING, %SQLUPPER, %STRING, %UPPER, $$, :,
+, -, (, NOT, EXISTS, or FOR>]
[Location: <Prepare>]
If I run the query without the timestamp all the data is returned.
Please any suggestions!

as TIMESTAMP is a reserved word, you should enclose it in double quotes "TIMESTAP"

In similar cases this worked for me:
select * from dbo.iSkillsetStat where Timestamp >= '2014-07-29T00:00:00'
Note the 'T' between the date and time values.

Try this:
select *
from dbo.iSkillsetStat
where Timestamp >= Convert(datetime, '2014-07-29 00:00:00')
Here you are missing 000 in your datetime 2014-07-29 00:00:00.
It should be like this 2014-07-29 00:00:00:000
instead of 2014-07-29 00:00:00

the field timestamp may be a reserved keyword and shouldn't be used for columnnames (have a look at https://www.drupal.org/node/141051 #728)
HINT:
if you field is of type TIMESTAMP convert the value before you compare it:
SELECT *
FROM dbo.iSkillsetStat
WHERE columnname >= TIMESTAMP('2014-07-29 00:00:00')

Related

Redshift query between date

I'm quite new to Redshift SQL.
select * from myredshift_tbl
where local_date between \'2016-01-01\' and \'2017-02-01\';
But got this error:
[amazon][500310] invalid operation syntax error at or near "\". I believe Redshift use single quote and I need to escape single quote.
If the column local_date is in date format, use:
select * from myredshift_tbl
where local_date between '2016-01-01' and '2017-02-01';
If the column local_date is timestamp:
select * from myredshift_tbl
where local_date between '2016-01-01 00:00:00' and '2017-02-01 23:59:59';
SELECT * FROM schemaName.TableName WHERE datetime > '2017-02-09
00:00:00' AND datetime < '2017-06-09 00:00:00';
The above query Works with Redshift to fetch all the entries in a table.
NOTE: The table I applied the query on had column/field 'datetime' of type 'timestamp'.
I tested this query on Redshift with the help of Workbench J.

Pass a column as parameter to dateadd in SQL Server

I want to convert a column of UTC time to local time.
My data looks like this:
time_utc TZID timezone
------------------------------------------------
2014-02-27 12:00:39.0 America/Toronto -5
2013-05-21 09:35:30.0 America/Goose_Bay -4
2015-01-08 06:58:58.0 America/Creston -7
I know that using
select *, DATEADD(hour, 5,time_utc)
from mytable
will add 5 hours to column time_utc.
However, as you can see, I have a variable time zone column.
How can I pass this variable to the dateadd function?
I tried the following 2 commands but they don't work:
Attempt #1:
select *, DATEADD(hour, timezone, time_utc)
from mytable
Attempt #2:
select *, DATEADD(hour, (select timezone from mytable), time_utc)
from mytable
Both throws this error:
Argument data type varchar is invalid for argument 2 of dateadd function. [SQL State=S0001, DB Errorcode=8116]
For decimal values of timezone, for instance -3.5, how would this work?
Thanks
How can I pass this variable to datetime function?
Just reference the column in the function call:
select *, DATEADD(hour, timezone, time_utc)
from mytable
For decimal values of timezone, for instance -3.5, how would this work?
The "number" parameter of DATEADD takes an integer, so you'd have to change to minutes and scale the hour offset. Since your timezone colume is apparently a varchar column, convert it to a decimal value as well:
select *, DATEADD(minute, cast(timezone as decimal(4,2)) * 60 , time_utc)
from mytable
There is a character value (most probably blank) in your dataset. Sql does implicity conversion but for non numeric value it will fail. Check your table to see if you have blanks or non numeric values for timezone

Time comparison in Advantage SQL

Trying to do a time comparison in an advantage query. Haven't been able to find the answer in the Advantage documentation. Seems like it should be very simple.
Table definition is:
CREATE TABLE TBL (
ID AutoInc,
Date Date,
[T] Time
)
Based on this book on google books I figured I should be able to just do a comparison against a literal expressed like this:
SELECT * FROM TBL WHERE [T] > '9:00:00 AM'
But that throws
Error 7200: AQE Error: State = S0000; NativeError = 2124; [iAnywhere Solutions][Advantage SQL Engine]Invalid operand for operator: > [Invalid TIME] -- Location of error in the SQL statement is: 27
You should always use the TIME'HH:MM:SS' (24h) or TIME'HH:MM:SS am' (12h) literal format:
SELECT * FROM TBL WHERE [T] > TIME'09:00:00 am'
I think you get the 2124 error, because you are using either upper case am/pm notation or because you did not use a leading zero.
See also:
http://devzone.advantagedatabase.com/dz/webhelp/advantage11/master_sql_literals.htm
From the documentation under SQL Literals (in the ADS help file, under the Advantage Developer's Guide, Part II - Advantage SQL, Chapter 11 - Introduction to Advantage SQL, SQL Literals)
Time literals are enclosed in single quotation marks, and use one of the following four formats: HH:MM, HH:MM AM (or PM), HH:MM:SS, or HH:MM:SS AM (or PM). If the AM (or PM) is missing from the literal, 24-hour time is assumed. The AM/PM part of time literals is not case sensitive. The following are valid time literals:
'19:10'
'4:43 AM'
'9:00:45 pm'
'22:19:59'
Using this table definition and data:
create table #temp (ID AutoInc,
Dt Date,
Tm Time);
insert into #temp (Dt, Tm) values (CurDate() - 1, CurTime());
-- Wait to make sure time changes slightly
insert into #temp (Dt, Tm) values (CurDate(), CurTime());
select * from #temp;
/* Output:
ID Dt Tm
-- ---------- -----------
1 02/26/2015 11:50:22 AM
2 02/27/2015 11:51:02 AM
*/
The following query successfully retrieves the proper row based on the time correctly (adjust, of course, to the appropriate times for your data):
select * from #temp where Tm = '11:50:22 AM'
Tested using ARC32 against Advantage 10.10 with the native ADT table type.

SQLite Order By Date1530019888000

Every record in my SQLite database contains a field which contains a Date stored as a string in the format 'yyyy-MM-dd HH:mm:ss'.
Is it possible to query the database to get the record which contains the most recent date please?
you can do it like this
SELECT * FROM Table ORDER BY date(dateColumn) DESC Limit 1
For me I had my query this way to solve my problem
select * from Table order by datetime(datetimeColumn) DESC LIMIT 1
Since I was storing it as datetime not date column
When you sure the format of text field is yyyy-MM-dd HH:mm:ss (ex.: 2017-01-02 16:02:55), So It works for me simply:
SELECT * FROM Table ORDER BY dateColumn DESC Limit 1
Without any extra date function!
You need to convert it to unix timestamp, and then compare them:
SELECT * FROM data ORDER BY strftime('%s', date_column) DESC
But this can be pretty slow, if there are lots of rows.
Better approach would be to store unix timestamp by default, and create an index for that column.
You can convert your column sent_date_time to yyyy-MM-dd format and then order by date:
1) substr(sent_date_time,7,4)||"-"||substr(sent_date_time,1,2)||"-"||substr(sent_date_time,4,2) as date
2) order by date desc
In my case everything works fine without casting column to type 'date'. Just by specifying column name with double quotes like that:
SELECT * FROM 'Repair' ORDER BY "Date" DESC;
I think SQLite makes casting by itself or something like that, but when I tried to 'cast' Date column by myself it's not worked. And there was no error messages.
You can also use the following query
"SELECT * FROM Table ORDER BY strftime('%Y-%m-%d %H:%M:%S'," + dateColumn + ") DESC Limit 1"
I found this ugly hack worked.
select *, substr(date_col_name,7,4)as yy,
substr(date_col_name,4,2) as mm,
substr(date_col_name,1,2) as dd
from my_table
order by yy desc,mm desc,dd desc
it would be better to convert the text column to date field type, but I found that did not work reliably for me.
If you do a lot of date sorting/comparison, you may get better results by storing time as ticks rather than strings, here is showing how to get 'now' in ticks with:
((strftime('%s', 'now') - strftime('%S', 'now') + strftime('%f', 'now')) * 1000)
(see https://stackoverflow.com/a/20478329/460084)
Then it's easy to sort, compare, etc ...
This will work for both date and time
SELECT *
FROM Table
ORDER BY
julianday(dateColumn)
DESC Limit 1

How do I match an entire day to a datetime field?

I have a table for matches. The table has a column named matchdate, which is a datetime field.
If I have 3 matches on 2011-12-01:
2011-12-01 12:00:00
2011-12-01 13:25:00
2011-12-01 16:00:00
How do I query that? How do I query all matches on 1 single date?
I have looked at date_trunc(), to_char(), etc.
Isn't there some "select * where datetime in date" function?
Cast your timestamp value to date if you want simple syntax. Like this:
SELECT *
FROM tbl
WHERE timestamp_col::date = '2011-12-01'; -- date literal
However, with big tables this will be faster:
SELECT *
FROM tbl
WHERE timestamp_col >= '2011-12-01 0:0' -- timestamp literal
AND timestamp_col < '2011-12-02 0:0';
Reason: the second query does not have to transform every single value in the table and can utilize a simple index on the timestamp column. The expression is sargable.
Note excluded the upper bound (< instead of <=) for a correct selection.
You can make up for that by creating an index on an expression like this:
CREATE INDEX tbl_ts_date_idx ON tbl (cast(timestamp_col AS date));
Then the first version of the query will be as fast as it gets.
not sure if i am missing something obvious here, but i think you can just
select * from table where date_trunc('day', ts) = '2011-12-01';
Just use the SQL BETWEEN function like so:
SELECT * FROM table WHERE date BETWEEN '2011-12-01' AND '2011-12-02'
You may need to include times in the date literals, but this should include the lover limit and exclude the upper.
From rails I believe you can do:
.where(:between => '2011-12-01'..'2011-12-02')