SQL to select data between a date range
Hello,
I am a new user to HTMLDB and I have an application where I need to select the rows from a table that are between a specified date range. The table description is
name varchar2
class varchar2
sal number
hdate date
I want to select the rows of all names between date value 1 and date value 2. The SQL query I use in SQLPLUS does not work in HTML DB. The query I used is
select null link, name from tab_data where hdate between TO_DATE('06-JAN-04','DD-MON-YY') and TO_DATE('08-JAN-04','DD-MON-YY')
I also have 2 date picker fields in my form and I used a similar query which did not work.
select null link, name from tab_data where hdate between TO_DATE(:P2_DATE1,'DD-MON-YY') and TO_DATE(:P2_DATE2,'DD-MON-YY').
Can someone help me out?
Thanks
To All,
This is getting frustrating... I have tried every combination and permutation I can think of with zero success. The table column I am querying is of type date. The nls_date_format is 'DD-MON-YY' on a 10g database on Linux ES. I am using datepicker 'DD-MON-YY'. I have converted it using TO_DATE, TO_CHAR(TO_DATE(, trunc etc. Is it just my installation? I have upgraded to the latest patch, so that can't be the problem. I created various test scenarios (virgin tables, one row, etc). I've been writing PL/SQL for fourteen years and I think I'm pretty good, but I've sure run up against a stone wall here. Surely somebody has done a date compare out there...
select to_char(to_date('06-JAN-04','DD-MON-YY'),'MM/DD/YYYY') from dual;
06-JAN-2004
select to_char(YOUR_DATE_COLUMN,'MM/DD/YYYY') from YOUR_TABLE_WITH_DATE_COLUMN;
15-OCT-2004
15-OCT-2004
15-OCT-2004
Similar Messages
-
Error when selecting date range in query designer
hi all,
when iam trying to select date range in query designer like 01.04.2009 to 10.04.2009 it has to select only that dates where as it is selecting all the dates in between those like 010.04.2009,01.03.2009,01.02.2009.why this is happening ,iam unable to understand.plzz help me in this issue.
Vamshi D Krishnahi ,
i have created a variable as you told but no use.still i have to select the dates manuallyone after the other.for more user friendly can i have a calander where i can select date ranges.is it posible to have calander for selecting date ranges instead selecting dates one by one,if posible i request you to give the detailed steps.plzz guide me in this issue.thanks in advance.
Vamshi D Krishna -
Devolped an ALV report for daily cash receipts for selected date range
hi,
how to devlop an ALV report for daily cash receipts for selected date range.for this report what are the tables and fields we have to use.what is the selectionscreen&what is logic.give me sample report.hi,
how to devlop an ALV report for daily cash receipts for selected date range.for this report what are the tables and fields we have to use.what is the selectionscreen&what is logic.give me sample report. -
Devloped an ALV report for daily cash receipts for selected date range
hi,
how to devlop an ALV report for daily cash receipts for selected date range.for this report what are the tables and fields we have to use.what is the selectionscreen&what is logic.give me sample report.Hi,
You can develop simple reports using Report Painter.
You may be also interested in:
Check report SAPMF05A for credit memo
See the following Std reports on Payment Advices execute the Tcodes:
S_ALR_87009888
S_ALR_87009889
S_ALR_87009890
S_ALR_87009891
S_ALR_87009892
S_ALR_87009893
S_ALR_87009978
S_ALR_87009979
S_ALR_87009980
S_ALR_87009981
S_ALR_87009982
S_ALR_87009983
S_ALR_87010056
S_ALR_87010057
S_ALR_87010058
S_ALR_87010059
S_ALR_87010060
S_ALR_87010061
S_ALR_87010066
S_ALR_87010067
S_ALR_87012106
S_ALR_87012107
S_ALR_87012108
S_ALR_87012109
S_ALR_87012110
S_ALR_87012111
S_ALR_87012116
S_ALR_87012117
S_ALR_87012200
S_ALR_87012201
S_ALR_87012202
S_ALR_870122
S_ALR_87012204
S_ALR_87012205
S_ALR_87012350
S_ALR_87012351
S_ALR_87012352
S_ALR_87012353
S_ALR_87012354
S_ALR_87012355
sample ALV report:
tables:
marav. "Table MARA and table MAKT
Data to be displayed in ALV
Using the following syntax, REUSE_ALV_FIELDCATALOG_MERGE can auto-
matically determine the fieldstructure from this source program
Data:
begin of imat occurs 100,
matnr like marav-matnr, "Material number
maktx like marav-maktx, "Material short text
matkl like marav-matkl, "Material group (so you can test to make
" intermediate sums)
ntgew like marav-ntgew, "Net weight, numeric field (so you can test to
"make sums)
gewei like marav-gewei, "weight unit (just to be complete)
end of imat.
Other data needed
field to store report name
data i_repid like sy-repid.
field to check table length
data i_lines like sy-tabix.
Data for ALV display
TYPE-POOLS: SLIS.
data int_fcat type SLIS_T_FIELDCAT_ALV.
select-options:
s_matnr for marav-matnr matchcode object MAT1.
start-of-selection.
read data into table imat
select * from marav
into corresponding fields of table imat
where
matnr in s_matnr.
end-of-selection.
Now, we start with ALV
To use ALV, we need a DDIC-structure or a thing called Fieldcatalogue.
The fieldcatalouge can be generated by FUNCTION
'REUSE_ALV_FIELDCATALOG_MERGE' from an internal table from any
report source, including this report.
The only problem one might have is that the report and table names
need to be in capital letters. (I had it )
Store report name
i_repid = sy-repid.
Create Fieldcatalogue from internal table
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = sy-repid
I_INTERNAL_TABNAME = 'IMAT' "capital letters!
I_INCLNAME = sy-repid
CHANGING
CT_FIELDCAT = int_fcat
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = i_repid
I_STRUCTURE_NAME = 'marav'
I_DEFAULT = 'X'
I_SAVE = 'A'
TABLES
T_OUTTAB = imat.
IF SY-SUBRC <> 0.
WRITE: 'SY-SUBRC: ', SY-SUBRC .
ENDIF.
Hope this will help.
Regards,
Naveen. -
File Count with selected date range
Hi,
Our requirement is to get the file count with selected date by the user from two sharepoint date time controls i.e. dtp1 and dtp2 into the data table. I am able to get the file count of specific folder from Pages library through below code. Now need to get
the selected date range from two date time picker controls and check with the item created by is within the date range. If yes I need to get the file count.
So please share your ideas/thoughts to do the same.
SPList list =
wikiweb.Lists["Pages"];
SPFolderCollection oFolders
= list.RootFolder.SubFolders["foldername"].SubFolders;
DataTable dt
= new DataTable();
dt.Columns.Add("Column1");
DataRow dr;
if (oFolders.Count
> 0)
foreach (SPFolder oFolder in oFolders)
if (!oFolder.Name.Equals("Forms"))
dr
= dt.NewRow();
dr["Column1"] = oFolder.ItemCount.ToString();
dt.Rows.Add(dr);
Regards,
Sudheer
Thanks & Regards, SudheerHi,
I have modified the code as below
if((DateTime)(oFolder.Item.File.TimeCreated>dtFromDate.SelectedDate)&&(DateTime)(oFolder.Item.File.TimeCreated<dtToDate.SelectedDate))
But still it is throwing the error.
Please share your ideas on the same.
Regards,
Sudheer
Thanks & Regards, Sudheer -
Resource Assignment Select date range not wokring
The Select Date Range control is not working properly it does not change the results shown in resource assignments under resource center in Project server 2013. Any ideas? Have I missed some configuration?
Regards, Syed Faizan ur Rehman, CBPM®,PRINCE2®, MCTSHi Syed,
It does work correctly for me in my Project Online instance. Have you installed the SP1?
Note that the date range is only available for the timephased data and not for the Gantt chart.
Hope this helps,
Guillaume Rouyre, MBA, MVP, P-Seller | -
Creating report based on user selected Date Range
Hello.
I am trying to display an Apex report that selects data for the report based on a user entered date range.
It's a simple page with two date picker fields (p18_start and p18_end).
The report region SQL looks like this:
SELECT *
FROM prj_items
WHERE LM_DT BETWEEN :p18_start AND :P18_end
One table, one field on the table to search and two Apex variables.
I thought this would be fairly simple, but I am obviously missing something.
I not even sure what other information is needed to help me figure this out.
-JodyHi,
You can set defaults for the datepickers if you need to - this could be done in a computation on the page for each item and conditional on the item being null.
When I've done something similar to this, I've created two hidden page items - eg, :P19_FIRST_DATE and :P19_LAST_DATE and populated these with the earliest/latest date that the user could reasonably select (perhaps, in your case, the MIN(LM_DT) and MAX(LM_DT) values).
Then your SQL would be:
select * from PRJ_ITEMS
where LM_DT BETWEEN TO_DATE(NVL(:P19_START,:P19_FIRST_DATE), 'DD-MON-YY')
AND TO_DATE(NVL(:P19_END,:P19_LAST_DATE), 'DD-MON-YY')If you don't want to set default dates, you could do something like:
SELECT * FROM PRJ_ITEMS
WHERE (:P19_START IS NULL AND :P19_END IS NULL)
OR (:P19_START IS NOT NULL AND :P19_END IS NULL AND LM_DT >= TO_DATE(:P19_START,'DD-MON-YY'))
OR (:P19_START IS NULL AND :P19_END IS NOT NULL AND LM_DT <= TO_DATE(:P19_END,'DD-MON-YY'))
OR (LM_DT BETWEEN TO_DATE(:P19_START,'DD-MON-YY') AND TO_DATE(:P19_END,'DD-MON-YY'))There are various reasons why your two dates are being cleared when the page is reloaded. Firstly, you should check the branch that returns to the same page - make sure you are not clearing the cache for the page. Then, have a look to see if there is a "reset page" process (usually created for you when you create a form page). Then, check the Source settings for the items. Typically, these would be "Only when current value in session state is null" and a Source Type of "Static Assignment" with the "Source Value or Expression" left empty.
Andy -
Hi, I am using Oracle 10g. I want a query which gives me below output.
Data Setup
========
create table t_date_range (
ID number (2),
start_date date,
end_date date);
insert into t_date_range values (1,to_date('20110101', 'YYYYMMDD'),to_date('20110331', 'YYYYMMDD'));
insert into t_date_range values (2,to_date('20110401', 'YYYYMMDD'),to_date('20110531', 'YYYYMMDD'));
insert into t_date_range values (3,to_date('20110701', 'YYYYMMDD'),to_date('20110731', 'YYYYMMDD'));
insert into t_date_range values (4,to_date('20110901', 'YYYYMMDD'),to_date('20111130', 'YYYYMMDD'));
insert into t_date_range values (5,to_date('20111201', 'YYYYMMDD'),to_date('20111231', 'YYYYMMDD'));
commit;
SQL> select ID, to_char(start_date,'DD-MON-YYYY') START_DATE, to_char(end_date,'DD-MON-YYYY') END_DATE from t_date_range;
ID START_DATE END_DATE
1 01-JAN-2011 31-MAR-2011
2 01-APR-2011 31-MAY-2011
3 01-JUL-2011 31-JUL-2011
4 01-SEP-2011 25-OCT-2011
5 26-OCT-2011 30-NOV-2011
6 01-DEC-2011 31-DEC-2011
6 rows selected.
I want result in this form:
START_DATE END_DATE
01-JAN-2011 31-MAY-2011
01-JUL-2011 31-JUL-2011
01-SEP-2011 31-DEC-2011
Means if there is a difference of exact one day between "start_date of 2nd row" and "end_date of first row" then make a single row as shows in above results set.
Thanks!Hi,
Solomon Yakobson wrote:
Keep in mind, Franks's solution assumes date ranges do not overlap. ...That's true. If rows can overlap, then we might need to use two sub-queries, like you did: one to see if a new group starts with this row, and another to count how many new groups have already started.
The solution you posted assumes a relationship between id and dates. If we add a row like this to the sample data:
insert into t_date_range values (6,to_date('20101201', 'YYYYMMDD'),to_date('20121231', 'YYYYMMDD'));that overlaps all the others, then how would that solution work?
LAG won't work, because no matter how we sort the rows, we can't be sure that overlapping rows will be consecutive.
Here's one way to handle overlapping rows:
WITH got_new_grp AS
SELECT id, start_date, end_date
, CASE
WHEN start_date > 1 + MAX (end_date) OVER ( ORDER BY start_date
, id
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
THEN 1
END AS new_grp
FROM t_date_range
, got_grp AS
SELECT start_date, end_date
, COUNT (new_grp) OVER ( ORDER BY start_date
, id
) AS grp
FROM got_new_grp
SELECT MIN (start_date) AS start_date
, MAX (end_date) AS end_date
FROM got_grp
GROUP BY grp
ORDER BY start_date
;This solution uses id (assumed to be unique) just to get a consistent ordering, in case 2 (or more) rows have exactly the same start_date. It does not assume any relationship between ids and either start_date or end_date.
I'm also assuming that start_date<=end_date on each row.
Edited by: Frank Kulash on May 11, 2011 12:54 PM
The above refers to the solution you oriognally posted, which was:
with t1 as (
select id,
start_date,
end_date,
case
when lag(end_date) over(order by id) + 1 >= start_date then 0
else 1
end start_of_group
from t_date_range
t2 as (
select id,
start_date,
end_date,
sum(start_of_group) over(order by id) grp
from t1
select min(start_date) start_date,
max(end_date) end_date
from t2
group by grp
order by grp
; -
Using function(s) to select date range in a computation.
Hello
Within my ApEx report, I would like to be able to have data returned from within a date range selected by the user. Under "Compute" there are lots of "Functions" (Least, Greatest, Last_Day, etc.) but so far I have not been able to find the right syntax for date range.
This seems so simple - any help appreciated.
Thanks
Paul S.I do not use APEX but is the BETWEEN operator available to you?
Normally to select a date range you just write code like
select ....
from table_t
where date_col between to_date(start_date,'format') and to_date(end_date,'format')
or
where date_col >= to_date(start_date,'format')
and date_col < to_date(end_date,'format') + 1
as this form eliminates worrying about the time component.
HTH -- Mark D Powell -- -
I have a query that will retrieve order between selected dates. It works great but returns no record if the 2 dates are the same.
Example:
Orders between 9-1-2010 and 9-30-2010 retunes 35 records.
But if I select between -9-25-2010 and 9-25-2010, so I can see all order from this 1 day, it returns 1 records, and I know there are records for that day!
Here's my query:
<%
Dim rsOrders__MMColParam
rsOrders__MMColParam = "1"
If (Request.QueryString("datefrom") <> "") Then
rsOrders__MMColParam = Request.QueryString("datefrom")
End If
%>
<%
Dim rsOrders__MMColParam2
rsOrders__MMColParam2 = "1"
If (Request.QueryString("dateto") <> "") Then
rsOrders__MMColParam2 = Request.QueryString("dateto")
End If
%>
<%
Dim rsOrders
Dim rsOrders_cmd
Dim rsOrders_numRows
Set rsOrders_cmd = Server.CreateObject ("ADODB.Command")
rsOrders_cmd.ActiveConnection = MM_ezcaldatasource_STRING
rsOrders_cmd.CommandText = "SELECT * FROM dbo.orders WHERE (OrderDate between ? and ?) AND Finalized = 1"
rsOrders_cmd.Prepared = true
rsOrders_cmd.Parameters.Append rsOrders_cmd.CreateParameter("param1", 135, 1, -1, rsOrders__MMColParam) ' adDBTimeStamp
rsOrders_cmd.Parameters.Append rsOrders_cmd.CreateParameter("param2", 135, 1, -1, rsOrders__MMColParam2) ' adDBTimeStamp
Set rsOrders = rsOrders_cmd.Execute
rsOrders_numRows = 0
%>2 possible
1) Change the column's data type from a datetime to a date if supported by your DBMS
2) Use date math to always add 1 day to the end date. So instead of the end date of 9-25-2010 (00:00) it will be 9-26-2010 (00:00) -
Hi,
I want a count of def as well as starting day of the week based on FDate week wise.
I have build a case structure starting from beginning to end of the week. But it is not working.
Please let me know a way where i can get a count of def for each week as well as starting day of the week.
For the WHERE clause i have a given a date range but it should be for the last 20 weeks.
SELECT FDate, count(def)
from Table
WHERE FDate > = '2008-03-17' and FDate <= '2008-08-07'
group by FDate
order by
case when FDate >= '2008-03-17' and FDate <= '2008-03-23' Then '2008-03-17'
when FDate >= '2008-03-24' and FDate <= '2008-03-30' Then '2008-03-24'
when --------
when --------
end
Please help.
Thanks.Hi,
While you could use a 20-pronged CASE statement, it's much eachier to use
GROUP BY TRUNC (fdate, 'IW')
TRUNC (fdate, 'IW') returns the DATE of the last Monday (12 midnight) that was on or before fdate. ("IW" stands for Iinternational Standards Organization Week.) It's an easy way to map an entire week into one value, and, unlike the 'D' format that Kevin and Guido mentioned earlier, it works the same regardless of you NLS settings.
What if you need to combine things by week, but you want to consider Sunday the start of the week? That's 1 day earlier than the ISO Week, and instead of
TRUNC (fdate, 'IW') you would use
TRUNC (fdate + 1, 'IW') - 1. -
Report on user selected Date Range
Please Disregard,
Message was meant for another forum.
Edited by: JodyMorin on Jan 15, 2009 11:29 AMHi,
You can set defaults for the datepickers if you need to - this could be done in a computation on the page for each item and conditional on the item being null.
When I've done something similar to this, I've created two hidden page items - eg, :P19_FIRST_DATE and :P19_LAST_DATE and populated these with the earliest/latest date that the user could reasonably select (perhaps, in your case, the MIN(LM_DT) and MAX(LM_DT) values).
Then your SQL would be:
select * from PRJ_ITEMS
where LM_DT BETWEEN TO_DATE(NVL(:P19_START,:P19_FIRST_DATE), 'DD-MON-YY')
AND TO_DATE(NVL(:P19_END,:P19_LAST_DATE), 'DD-MON-YY')If you don't want to set default dates, you could do something like:
SELECT * FROM PRJ_ITEMS
WHERE (:P19_START IS NULL AND :P19_END IS NULL)
OR (:P19_START IS NOT NULL AND :P19_END IS NULL AND LM_DT >= TO_DATE(:P19_START,'DD-MON-YY'))
OR (:P19_START IS NULL AND :P19_END IS NOT NULL AND LM_DT <= TO_DATE(:P19_END,'DD-MON-YY'))
OR (LM_DT BETWEEN TO_DATE(:P19_START,'DD-MON-YY') AND TO_DATE(:P19_END,'DD-MON-YY'))There are various reasons why your two dates are being cleared when the page is reloaded. Firstly, you should check the branch that returns to the same page - make sure you are not clearing the cache for the page. Then, have a look to see if there is a "reset page" process (usually created for you when you create a form page). Then, check the Source settings for the items. Typically, these would be "Only when current value in session state is null" and a Source Type of "Static Assignment" with the "Source Value or Expression" left empty.
Andy -
ABAP SQL Query (Select data from intervals)
Hi All,
From To Temp
101 200 1111
201 300 2222
301 400 3333
401 500 4444
501 600 5555
From, To & Temp are three Fields.
Consider the above table, if i enter input as 150, need to pick 1111.
If the input is inbetween of From & To values means I want to pick the exact
Data from temp field.
Thanks
Hema
Edited by: hema prabakaran on Jan 19, 2009 7:54 AM
Edited by: hema prabakaran on Jan 19, 2009 8:30 AMHi
Data: itab type table of tbl with header line.
select * from tbl into table itab.
if sy-subrc eq 4.
Message E000(YJK).
else.
loop at itab.
if v_temp between itab-from and itab-to.
write itab-temp.
exit.
else.
continue.
endif.
endloop.
endif.
You can use the above code.
It is not possible to use select stmt with your i/p variable in the where clause as that field is not present in table.
hope this helps
Regards,
Jayanthi.K -
Dynamic flat file name based on selected date range
Hi,
I have created mapping which load data into file, i have to create file with the range specified for the data to be pulled.
e.g if i have to load data from 01-sep-12 to 15-sep-12 the file name should be ext_rio_15-sep_to_15_sep. how can i achive through owbhttps://blogs.oracle.com/warehousebuilder/entry/dynamically_generating_target
Cheers
David -
SQL Help for Date Range display
Gurus,
I have a sql statement like
select MIN(HIREDATE), MAX(HIREDATE) FROM EMP WHERE DEPTNO = 30My output looks like
MIN(HIREDATE) MAX(HIREDATE)
12/30/1998 12/30/2001Based on the values of MIN(HIREDATE) , MAX(HIREDATE) values
I need a SQL to generate the output like
12/30/1998
1/30/1999
2/28/1999
until Max(HIREDATE) value
In Feb we dont have 30th day, In that case take the last day of the month.
Thanks for great help
C 007With 10g.
Regards Salim.WITH t AS
(SELECT min(hiredate) min_hiredate,
max(hiredate) max_hiredate
FROM emp_test
where deptno=30)
SELECT min_hiredate dt
FROM t
model
dimension by ( 1 rn)
measures(min_hiredate,months_between(max_hiredate,min_hiredate) diff )
(min_hiredate[for rn from 1 to diff[1] increment 1]=add_months( min_hiredate[1],cv(rn)-1))
order by rn
SQL> WITH t AS
2 (SELECT min(hiredate) min_hiredate,
3 max(hiredate) max_hiredate
4 FROM emp_test
5 where deptno=30)
6 SELECT min_hiredate dt
7 FROM t
8 model
9 dimension by ( 1 rn)
10 measures(min_hiredate,months_between(max_hiredate,min_hiredate) diff )
11 (min_hiredate[for rn from 1 to diff[1] increment 1]=add_months( min_hiredate[1],cv(rn)-1))
12 order by rn
13 /
DT
1998-12-30
1999-01-30
1999-02-28
1999-03-30
1999-04-30
1999-05-30
1999-06-30
1999-07-30
1999-08-30
1999-09-30
1999-10-30
1999-11-30
1999-12-30
2000-01-30
2000-02-29
2000-03-30
2000-04-30
2000-05-30
2000-06-30
2000-07-30
2000-08-30
2000-09-30
2000-10-30
2000-11-30
2000-12-30
2001-01-30
2001-02-28
2001-03-30
2001-04-30
2001-05-30
2001-06-30
2001-07-30
2001-08-30
2001-09-30
2001-10-30
2001-11-30
SQL>
Maybe you are looking for
-
Data of one group not getting displayed (in rtf template)
Hi We have build one rtf template it has 2 sectrions one is main section where data is being displayed with out any issues. where as for Sub section data is not getting displayed though there is data in xml file 1) Main section <?for-each@section:G_O
-
Remote for iTunes in a Two-PC Network
I've added the "Remote for iTunes" app to my new Android-based phone; but, it's iTunes on my wife's PC that recognizes the application rather than mine. Home sharing is turned on. Windows 7 on both laptops. Suggestions as to how to go about changing
-
Problem with npm installing global packages
If I try to install a global package with npm like : sudo npm install -g grunt npm will install the package in "/usr/lib/node_modules/". But it is not installing the executable anywhere. The result of npm -g bin is /usr/bin So that is where I would e
-
Hi, Office 2007 is on Windows 7. All up to date. Outlook crashes from time to time. Below is the error message in Event viewer. Faulting application name: OUTLOOK.EXE, version: 12.0.6680.5000, time stamp: 0x51c3d112 Faulting module name: mso.dll, ver
-
Customizing form for user submitted content {tag_edit}
Hi,, I have a Webapp with a few custom fields which can be filled by logged-in customers. These customers see all their submitted items on a certain webpage with two links: Edit and Delete. The tags {tag_delete} and {tag_edit} are connected to the li