Select query for viewing data in specific data rang in FoxPro database - visual-foxpro

In foxpro database, how to write select query for viewing data in specific time range. Can anyone show an example.

You just write it as you would do with any SQL database. The only thing that differs is the parameters' syntax. For example below query retrieves all the transactions made in, say July 2020:
local ltFrom, ltUpTo
ltFrom = DateTime(2020,7,1)
ltUpTo = DateTime(2020,8,1) && Midnight
select * from myTable ;
where transactionDatetime >= ?m.ltFrom and ;
transactionDatetime < ?m.ltUpTo ;
into cursor crsResult ;
nofilter
browse

To expand on Cetin's query for a date range query, you can also query for ranges including the time component:
ltFrom = DateTime(2020,8,1,0,0,0) && midnight
ltUpTo = DateTime(2020,8,1,14,03,51) && 2:03:51 pm
*ltFrom = DateTime(2020,8,1,0,0,0) && midnight
*ltUpTo = DateTime(2020,8,3,11,59,59) && 1 second before noon 2 days later
SELECT hostname,timestamp FROM gsm WHERE timestamp BETWEEN ?ltfrom AND ?ltupto INTO CURSOR crsResult NOFILTER ORDER BY timestamp
select * from myTable ;
where transactionDatetime between ?m.ltFrom and ?m.ltUpTo ;
into cursor crsResult ;
nofilter
browse
There are 2 time ranges above:
August 1, 2020 from midnight up to 2:03:51pm
August 1, 2020 from midnight up to August 3, 2020, 11:59:59am (commented out)

Related

Oracle sql how to get the date of a week

I have the following query that gets the week of a date:
SELECT pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww') semana,
SUM (rta.kms_acumulados) kms,
COUNT
(DISTINCT (CASE
WHEN v.secuencia BETWEEN rta.sec_origen AND rta.sec_destino
THEN v.cod_inc
ELSE '0'
END
)
)
- 1 numincidencias
FROM (SELECT ms.tren, ms.fecha_origen_tren, ms.secuencia, ri.cod_inc
FROM r_incidencias ri, mer_sitra ms
WHERE ri.cod_serv = ms.tren
AND ri.fecha_origen_tren = ms.fecha_origen_tren
AND ri.cod_tipoin IN (SELECT cod_tipo_iincidencia
FROM v_tipos_incidencias
WHERE grupo = '45')
AND ri.punto_desde = ms.cod_estacion) v,
r_trenes_asignar rta,
r_maquinas rm,
planificador.pl_dh_material pdm
WHERE rta.fecha BETWEEN TO_DATE ('21/09/2018', 'dd/mm/yyyy') AND TO_DATE ('21/09/2018',
'dd/mm/yyyy'
)
AND rta.serie >= 4000
AND rta.matricula_ant IS NOT NULL
AND rm.matricula_maq = rta.matricula_ant
AND rm.cod_serie = pdm.id_material
AND rta.grafico BETWEEN pdm.desde AND pdm.hasta
AND v.tren(+) = rta.tren
AND v.fecha_origen_tren(+) = rta.fecha
GROUP BY pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww')
ORDER BY pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww')
For example week 1
I want to display
week 1 : 1 january - 7 january
How can I get this?
Oracle offers the TRUNC(datestamp, format) function to manipulate dates this way. You may use a variety of format strings to get the first day of a quarter, year, or even the top of the hour.
Given a particular datestamp value, Oracle returns midnight on the first day of the present week with this expression:
TRUNC(datestamp,'DY')
You can add days to a datestamp. Therefore this expression gives you midnight on the last day of the week
TRUNC(datestamp,'DY') + 6
A WHERE-clause selector for all rows in the present week might be this.
WHERE datestamp >= TRUNC(SYSDATE,'DY')
AND datestamp < TRUNC(SYSDATE,'DY') + 7
Notice that the end of the range is just before (<) midnight on the first day of the next week. You need that because you may have datestamps after midnight on the last day of the week. (Beware using BETWEEN for datestamp ranges.)
And,
SELECT TO_CHAR(TRUNC(SYSDATE,'DY'),'YYYY-MM-DD'),
TO_CHAR(TRUNC(SYSDATE,'DY')+6,'YYYY-MM-DD')
FROM DUAL;
displays the first and last dates of the present week in ISO-like format.
Date arithmetic is cool. It's worth your trouble to study the date-arithmetic functions in your DBMS at least once a year.

Oracle - trunc(DateTime) between trunc(Sysdate - 104) and trunc(Sysdate - 75)

I want to select few data from a Oracle table of June month(June 1st to June 30th of 2017) and the start date will be from tomorrow(Sep 13th). Hence I wrote a query in below format,
select * from table where column1='Data1' and Column2='Data2'
and trunc(DateTime) between trunc(sysdate-104) and trunc(sysdate-75)
I'm not able to check this query as I don't have tool for this. I just wrote it in notepad and want to share to my friend.
My Question - Will trunc(DateTime) between trunc(sysdate-104) and trunc(sysdate-75) condition will give data between June1st to June31 or Does any Syntax issue there?
There is no problem with the syntax itself even though your formulation is time sensitive, which means that tomorrow it won't return the same result.
Instead, go with something like this :
AND TRUNC(DateTime) BETWEEN to_date('2016-06-01','YYYY-MM-DD')
AND to_date('2016-06-30','YYYY-MM-DD')
Just cast the date format to month/year and compare against that.
select *
from table
where column1 = 'Data1'
and column2 = 'Data2'
and to_char(DateTime, 'MMYYYY') = '062017';
Hi I think the most accurate would be:
select * from table where column1='Data1' and Column2='Data2'
AND DateTime BETWEEN TRUNC(sysdate-104)
AND TRUNC(sysdate-75)
+ interval '23' hour
+ interval '59' minute
+ interval '59' second;

How to query on a specific date and time range using hive query language taking input from the user?

I have a table in a database in hive.
The table is partitioned based on year month and day.
My query looks something like this
select entity1,entity2
from table_t
INNER JOIN tab_roll.cha alias2
ON alias1.sid = alias2.sid
INNER JOIN net_roll.net alias3
ON alias2.id=alias3.id
where event= 'unknown'
and day >= 10 and day < 12
and month >= 5 and month < 11
and year = 2014
now I want to get results between say mm-dd-yyy HH : MM :SS and mm-dd-yyy HH : MM :SS, how should I do that?
Is is possible to have a pop up where the user chooses the date/time ranges?
Don't know if this helps but the data has about 500 million rows.
Thanks
I think Between should work for you. & to optimize this you can index that column too.

"BETWEEN" SQL Keyword for Oracle Dates -- Getting an error in Oracle

I have dates in this format in my database "01-APR-12" and the column is a DATE type.
My SQL statement looks like this:
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno AND s.salestype = 1
AND (s.salesdate BETWEEN '01-APR-12' AND '31-APR-12');
When I try to do it that way, I get this error -- ORA-01839: date not valid for month specified.
Can I even use the BETWEEN keyword with how the date is setup in the database?
If not, is there another way I can get the output of data that is in that date range without having to fix the data in the database?
Thanks!
April has 30 days not 31.
Change
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno AND s.salestype = 1
AND (s.salesdate BETWEEN '01-APR-12' AND '31-APR-12');
to
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno AND s.salestype = 1
AND (s.salesdate BETWEEN '01-APR-12' AND '30-APR-12');
and you should be good to go.
In case the dates you are checking for range from 1st day of a month to the last day of a month then you may modify the query to avoid the case where you have to explicitly check the LAST day of the month
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno
AND s.salestype = 1 AND (s.salesdate BETWEEN '01-APR-12' AND LAST_DAY(TO_DATE('APR-12', 'MON-YY'));
The LAST_DAY function will provide the last day of the month.
The other answers are missing out on something important and will not return the correct results. Dates have date and time components. If your salesdate column is in fact a date that includes time, you will miss out on any sales that happened on April 30 unless they occurred exactly at midnight.
Here's an example:
create table date_temp (temp date);
insert into date_temp values(to_date('01-APR-2014 15:12:00', 'DD-MON-YYYY HH24:MI:SS'));
insert into date_temp values(to_date('30-APR-2014 15:12:00', 'DD-MON-YYYY HH24:MI:SS'));
table DATE_TEMP created.
1 rows inserted.
1 rows inserted.
select * from date_temp where temp between '01-APR-2014' and '30-APR-2014';
Query Result: 01-APR-14
If you want to get all records from April that includes those with time-components in the date fields, you should use the first day of the next month as the second side of the between clause:
select * from date_temp where temp between '01-APR-2014' and '01-MAY-2014';
01-APR-14
30-APR-14

query by day in PL/SQL(oracle)

I am querying from database:
select * from database where id = 12345
and i get a couple of days where it is equal to
3/4/2010 9:16:59 AM
but if i add
and date = to_date('03/04/2010','DD/MM/YYYY')
giving me
select * from database where id = 12345
and date = to_date('03/04/2010','DD/MM/YYYY')
I comeback with completely nothing.
Any pointers?
btw, I know that there is a time on there, but I don't know how to compare just based on the day!!!
That's because when you are creating the date, you implicitly set the time to 0:00:00, and as 0:00:00 is not equal to 9:16:59, you'll not get the date returned.
To tell Oracle to ignore the time part, just do the following:
WHERE id = 12345
AND trunc(date) = to_date('03/04/2010', 'DD/MM/YYYY')
WHERE id = 12345
AND date >= TO_DATE('03/04/2010', 'DD/MM/YYYY')
AND date < TO_DATE('03/04/2010', 'DD/MM/YYYY') + INTERVAL '1' DAY
http://use-the-index-luke.com/sql/where-clause/obfuscation/dates

Resources