geode oql order by a string and to_date - gemfire

I have a region which contains transaction times input as strings in the format yyyy-MM-dd HH:mm:ss.fff and I'd like to be able to run an OQL query with results ordered by transaction time. Whether as a string or a date...
If I run the order by then the results are not coming back ordered.
query --query="select distinct d.value FROM /deal.entries d order by d.TransactTime"
The string TransactTime is entirely random. So I try to translate the string value back to date by the TO_DATE keyword. Something like:
query --query="select distinct d.value FROM /deal.entries d order by TO_DATE(d.TransactTime,'yyyy-MM-dd HH:mm:ss.fff')"
This is giving the error Query is invalid due for error : <Syntax error in query: expecting StringLiteral, found 'TransactTime'>
What am I missing here please?

I see that you are querying from /deal.entries - that means you are querying (key,value) pairs, not just the value. So maybe you need to use d.value.TransactTime.

Related

Using Where Clause with Dates When Reading SQL Query in R

I am attempting to query an Oracle datebase from R using the SQL function found here.
When I complete an easy query, such as
'SELECT * FROM TABLE_1'
the query executes fine. However, when I add a conditional date statement to the query, such as
'SELECT * FROM TABLE_1 WHERE START_DT BETWEEN '01-JUL-2018' AND '30-JUN-2019'
I get the following error message:
Error in .oci.SendQuery(conn, statement, data = data, prefetch =
prefetch, : ORA-00904: "30-JUN-2019": invalid identifier
Any idea how I can fix this?
The exact error appears to be that you didn't escape the single quotes you placed around the date literals in your R query string. But, fixing that still leaves the problem that your date literals are invalid for Oracle. I recommend using this:
sql <- "SELECT * FROM TABLE_1 WHERE START_DT BETWEEN DATE '2018-07-01' AND DATE '2019-06-30'"
You could also use the TO_DATE function, e.g. TO_DATE('01-JUL-2018', 'DD-MON-YYYY'), but this is a bit harder to read than using the DATE keyword.

Date/Time as string in Access SQL - Select a specific date

I would like run a SQL in MS Access like the following:
SELECT Time, Ask, Bid
FROM AUDCAD
WHERE Time LIKE '2016.10.05'
ORDER BY ID;
However the result is nothing, The Time field data is look like the following:
2016.12.05 09:42:17.026
2016.12.05 09:42:17.387
2016.12.05 09:42:17.951
2016.12.05 09:42:18.464
...
2016.12.06 09:24:41.449
2016.12.06 09:24:41.854
2016.12.06 09:24:42.258
Therefore, I would like to extract the data day by day (this example: 2016.10.05)
Can anyone help me to solve this problem?
Lawrence
You need to check two things first...
Did your insert query work without errors .. are you sure time '2016.10.5' data exist in your DB?
Can you execute standard query to get time data and it works? Meaning can you 'SELECT FROM AUDCAD" ang get time data 2016.10.5
You must use the proper syntax for date expressions in Access SQL:
SELECT [Time], Ask, Bid
FROM AUDCAD
WHERE [Time] = #2016/10/05#
ORDER BY ID;
or, if Time has a time component:
SELECT [Time], Ask, Bid
FROM AUDCAD
WHERE Fix([Time]) = #2016/10/05#
ORDER BY ID;
However, it looks like you retrieve data from a DateTime2 field in SQL Server.
Thus, either change the data type to DateTime, or use the SQL Native Driver version 10 or 11 for your ODBC connection. If not, you will receive the date/time as text, not date/time values.
The separator for DateTime fields is #. Ex: #12/30/2016#.
I would recommend to always use the american order in VBA (m/d/y) even if the local machine is configured otherwise. It works fine that way.
Your sample query
SELECT Time, Ask, Bid
FROM AUDCAD
WHERE Time LIKE '2016.10.05'
ORDER BY ID;
uses a LIKE clause without any wildcards, so WHERE Time LIKE '2016.10.05' behaves just the same as WHERE Time = '2016.10.05'. That won't return any rows because the [Time] column always includes some characters after the date.
By default, Access uses the asterisk (*) as the "0 or more characters" wildcard, so
SELECT Time, Ask, Bid
FROM AUDCAD
WHERE Time LIKE '2016.10.05 *'
ORDER BY ID;
should work. Alternatively, you could use the ALIKE ("ANSI LIKE") keyword with the percent (%) wildcard:
SELECT Time, Ask, Bid
FROM AUDCAD
WHERE Time ALIKE '2016.10.05 %'
ORDER BY ID;

convert SQL string into a date that can be queried

I am pulling from a database using the following:
SELECT
ID, CUSTOMER, NAME, etc, etc, TERM_INDEX
FROM "DAB055.ADT" DAB055
Now I want to limit the date range we pull, so would like to do something like:
WHERE TERM_INDEX >= '01.01.2015'
but the output in that field looks like:
20150629W
How can I convert that into a usual date field within the same statement, so that it can be filtered on?
Thanks.
It looks like your dates are stored in a format that will sort chronologically by using the alphabetical order. You can probably just say WHERE TERM_INDEX >= '20150101'.
Also it shouldn't be difficult to grab the first 8 characters and convert to a date type. Without knowing which platform you're on we'd have to guess at the syntax though.
One of these might work to figure out what your database server is:
select ##version
select * from v$version

SQL date format picture ends before converting entire input string

I am trying to execute a query looking something like this:
create table A as
select
userid, to_date(date1, 'mm/dd/yyyy') as startDate,
to_date(date2, 'mm/dd/yyyy') as endDate
from TABLE;
I am getting the error:
ORA-01830: date format picture ends before converting entire input string
What's really strange here is that when I run only the SELECT... part of the query it works perfectly, I am only getting the error when I try to create a table. I absolutely need to make a table, so how can I get around this?
Thanks!
There's likely some bad data past the first few rows. When you run the select, it will do the conversion for the first few (less than 1000 for me) rows only. The results are paged. You'll need to clean up the data first. You could write a simple function like this to figure out which dates it's failing on.
How to handle to_date exceptions in a SELECT statment to ignore those rows?

selecting data fom a view throws error

I get input as 2011/11/13 00:00:00. So I made the query as:
select * from xxcust_pfoa434p_vw
where week_ending_date = to_date(substr(:value,1,10),'YYYY/MM/DD')
The same statement gives proper result when queried against other tables. But throws error when I query this against the view xxcust_pfoa434p_vw
I have a view xxcust_pfoa434p_vw which has a column week_ending_date of date data type.
The value in that column is like 3/2/2014,12/25/2011 i.e. MM/DD/YYYY
Even
select * from xxcust_pfoa434p_vw where week_ending_date='3/2/2014'
also gives
ORA-01843: not a valid month. What is the cause for this error.
You say
"The same statement gives proper result when queried against other
tables. But throws error when I query this against the view
xxcust_pfoa434p_vw"
So clearly the problem is with the view. You also say
"[the view] has a column week_ending_date of date data type. The value
in that column is like 3/2/2014,12/25/2011 i.e. MM/DD/YYYY "
Those values would only display like that if the default date mask for you system were MM/DD/YYYY. This is easy enough to check with the query
select * from V$NLS_PARAMETERS
where parameter = 'NLS_DATE_FORMAT';
Personally, my money is on that column not being a date column. ORA-01841 always indicates oracle attempting to cast a string into a date and finding a value which doesn't fit the explicit or default format mask. Plus the so-called date '3/2/2014' lacks leading zeroes and that's suspicious too.
I think whoever wrote that view decided to fix the format of week_ending_date and so deployed TO_CHAR to present a string not a date datatype. A DESC in SQL*Plus or looking at the view TEXT in ALL_VIEWS will reveal the answer.
select * from xxcust_pfoa434p_vw
where week_ending_date=to_date('03/02/2014','MM/DD/YYYY');
Even if you see formatted date in this format - it is only a visual representation, when oracle process your query it automatically convers string given by you into its own interal representation.
It is always better to use proper SQL one YYYY-MM-DD:
for 2nd march: select * from xxcust_pfoa434p_vw where week_ending_date = to_date('2014-03-02', 'YYYY-MM-DD')
for 3rd february: select * from xxcust_pfoa434p_vw where week_ending_date = to_date('2014-02-03', 'YYYY-MM-DD')
this conforms to SQL standard and do not produce confusion between DD/MM/YYYY and MM/DD/YYYY
Just quote from standard:
There is an ordering of the significance of <datetime field>s. This
is, from most significant to least significant: YEAR, MONTH, DAY,
HOUR, MINUTE, and SECOND.
UPDATE: it is very good idea always use to_date function to specify exact format and avoid dependancy on any kind of localization settings