How to select previous RECORD in VB 6.0 - vb6

USING VB 6.0
How to select previous row value?
Am Selecting a Date between fromdate and todate using date picker
Code
Dim stdate, endate as string
stdate = Fromdate
endate = todate
Example:
fromdate: 01-01-2009
todate: 01-06-2009
Data’s will display fromdate to todate.
I want to select previous date means previous row value.
How to select previous row value?
We cannot give
stdate = - fromdate
endate = - todate
It will display a data’s between 31-01-2008 to 31-05-2009
We cannot give
Stdate = <fromdate
endate = < todate
It will display a data’s before 01-01-2009 and also we cannot use > in between condition
Stdate = fromdate (Here how can I give “from previous row value of the fromdate”)
endate = todate (here how can I give “to previous row value of the todate”)
Example:
id, date, name
01, 02-01-2009, raja
01, 04-01-2009, raja
02, 04-01-2009, ravi
so on.....
01, 28-05-2009, raja
01, 31-05-2009, raja
so on...
am selecting a date stdate = 04-01-2009, endate= 31-05-2009
Output shoud display like this -
01, 02-01-2009, raja
01, 04-01-2009, raja
02, 04-01-2009, ravi
.......
01, 28-05-2009, raja
It should dipslay one record Before ssdate and endate.
How to select previous row value?
Need Help in VB 6 code.

I do not completely understand what you are doing, but if you have a recordset object that is sorted on date you need to need something like
If (recordset.BOF = False) Then
recordset.MovePrevious
End If
If you are asking how to get a recordset that contains one record before the start date you are going to have to expand your query to try to include at least on record before the fromdate, find the first record with the fromdate, then use the MovePrevious method above.
If you are just asking how to get a date prior to a given date then use the DateAdd method.
example - the below code will subtract 1 day from 2/1/2009 and put 1/31/2009 into the valriable.
dtPreviousDate = DateAdd("D", CDate("02-01-2009"), -1)

Related

Next week in Oracle

I'm using oracle dbms and I have in Employe table a column Birthdate. I want to write a query that shows the employees who has a birthday next week.
Is this correct ?
select name
from employe
where to_char(birthdate,'DD-MM')=to_char(next_day(sysdate,1)+7,'DD-MM');
That is not the correct usage of next_day(): that function returns the date of the the next instance of a day. For example, to find the date of next Friday:
select next_day(sysdate, 'FRIDAY') from dual;
To find employees whose birthday is seven days from now, you need to just tweak your query a bit:
select name
from employe
where to_char(birthdate,'DD-MM') = to_char(sysdate+7,'DD-MM');
The correct solution would be
SELECT name
FROM employe
WHERE to_char(birthdate
/* "move" the birthdate to the current year
to get a reliable week number */
+ CAST((EXTRACT(year FROM current_date)
- EXTRACT(year FROM birthdate)) || '-0'
AS INTERVAL YEAR TO MONTH),
'IW')
= to_char(current_date + 7, 'IW');
The IW format returns the ISO week containing the date, which is probably what you are looking for. If you start your week on Sunday, add one to both dates.

How do I update the time in oracle sql?

I need a query to update a time in an appointment date by keeping the date but changing the time.
For example
10-Feb-2016 09:00:00
and i want to change it to 10-Feb-2016 10:00:00.
Update Appointment
set vdate = '10:00:00'
where vdate= '10-Feb-2016'
I get the "0 row has been updated'. Not sure if i'm missing something.
Thanks in advance.
You can use trunc() which sets the time part of a DATE (or TIMESTAMP) to 00:00:00, then add the 10 hours to it:
Update Appointment
set vdate = trunc(vdate) + interval '10' hour
where trunc(vdate) = DATE '2016-02-10'
This would change all rows that have a date 2016-02-10. If you only want to do that for those that are at 09:00 (ignoring the minutes and seconds) then just add one hour to those rows
Update Appointment
set vdate = trunc(vdate) + interval '1' hour
where trunc(vdate, 'hh24') = timestamp '2016-02-10 09:00:00'
trunc(vdate, 'hh24') will set the minutes and seconds of the date value to 00:00, so that the comparison with 2016-02-10 09:00:00 works properly.
Unrelated, but: do not rely on implicit data type conversion. '10-Feb-2016' is a string value, not a DATE literal. To specify a date either use an ANSI DATE literal (as I have done in the above statement) or use the to_date() function with a format mask to convert a string literal to a proper date value.
Your statement is subject to the evil implicit data type conversion and will fail if the SQL client running the statement uses a different NLS setting (it will fail on my computer for example)
If what you want to do is add an hour to a date, then you can do:
Update Appointment
set vdate = vdate + 1/24
where vdate = to_date('10/02/2016 09:00', 'dd/mm/yyyy hh24:mi');
since in Oracle, date differences are measured in number of days, and an hour is 1/24th of a day.
If what you want to do is specify an exact time (e.g. to 10:25:48), then you could do the following instead:
Update Appointment
set vdate = trunc(vdate) + 10/24 + 25/(24*60) + 48/(24*60*60)
where vdate = to_date('10/02/2016 09:00', 'dd/mm/yyyy hh24:mi');
Bear in mind that these updates will update all rows that have a date of 10th Feb 2016 at 9am. You'd need to change your query's where clause if you wanted to specify a more specific row or set of rows.
Try like this.
UPDATE MyTable
SET MyDate = DATEADD(HOUR, 4, CAST(CAST(MyDate AS DATE) AS DATETIME))
or
UPDATE MyTable
SET MyDate = DATEADD(HOUR, 4, CAST(FLOOR(CAST(MyDate AS FLOAT)) AS DATETIME))

Manipulating a data structure in Pig

Earlier I asked about manipulating a data structure in Hive or Pig. I was able to get an answer in SQL, and from there figured out the answer for Hive. I'm still looking for a solution in Pig.
I want to change myTable:
Into myTable2:
I tried:
myTable2 = FOREACH myTable GENERATE item, year,
'jan' AS month, jan AS value,
'feb' AS month, feb AS value,
'mar' AS month, mar AS value;
Which more or less is what worked in Hive, but Pig gives me:
ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1108:
<line 2, column 35> Duplicate schema alias: month
I figured it out, although I would love to see a more concise version:
JAN = FOREACH myTable GENERATE item, year, 'jan' AS month, jan AS value;
FEB = FOREACH myTable GENERATE item, year, 'feb' AS month, feb AS value;
MAR = FOREACH myTable GENERATE item, year, 'mar' AS month, mar AS value;
myTable2 = union JAN, FEB, MAR;
Pig Script:
data = LOAD '/pigsamples/sampledata' USING PigStorage(',')
AS (item:CHARARRAY, year:INT, jan:DOUBLE, feb:DOUBLE, mar:DOUBLE);
--concatenating month name to its value so that they won't get separated when i perform a flatten on the tuple.
concat_data = FOREACH data GENERATE item, year, CONCAT('jan:', (CHARARRAY)jan) AS jan,
CONCAT('feb:', (CHARARRAY)feb) AS feb, CONCAT('mar:', (CHARARRAY)mar) AS mar;
--convert the month (name,value) pairs to a bag and flatten them
flatten_values = FOREACH concat_data GENERATE item, year, FLATTEN (TOBAG (jan, feb, mar)) AS month_values;
--split the string based on the delimiter that we used above to concat
split_flatten_values = FOREACH flatten_values GENERATE item, year, FLATTEN (STRSPLIT (month_values, ':')) AS (month:CHARARRAY, value:CHARARRAY);

BIRT Report: Parameters between two dates

I've got a report I'm writing in BIRT against an Oracle database:
Table:
tranx_no (string)
type (string)
description (string)
amount (number(14,2))
date (date)
Query in BIRT:
SELECT tranx_no, type, description, amount
FROM tranx_table
WHERE date BETWEEN ? AND ?
If I just do plain dates (02-01-2014 and 02-14-2014) in the parameters, it misses things that happen during the day of the 14th (stops at midnight). I've tried concatenating the time onto the date parameter
WHERE date BETWEEN ? || '12:00:00 AM' AND ? || '11:59:59 PM'
and got an ORA 01843 error. I also tried casting it with to_date
WHERE date BETWEEN TO_DATE(? || '12:00:00 AM', 'MM-DD-YYYY HH:MI:SS AM') AND TO_DATE(? || '11:59:59 PM', 'MM-DD-YYYY HH:MI:SS AM')
and no joy there either. ORA 01847 error happens with that one.
Ideas? I know there's probably something simple I'm not thinking of, but Google hasn't helped. I'm wanting to edit the query, not change the date entry on the face of the form.
Thanks.
Correct handling DATEs with BIRT can be tricky.
I recommend to always use VARCHAR2 for passing DATE parameters (for report parameters as well as for query parameters). Always verify the data type of your parameters.
In your SQL, always use TO_DATE / TO_CHAR with an explicit date format - otherwise the results would depend on the locale settings.
Next, be sure that the value the user entered is adjusted to a known date-format before it is used in the query. For example, in Germany the SQL date format DD.MM.YYYY HH24:MI:SS is commonly used.
You could create a utility function which adds the missing parts (e.g. the time) automatically. Use an additional argument in this function to specify if it's a "from" date (adds 00:00:00) or a "to" date (adds 23:59:59).
OTOH if the UI forces the user to enter a from-to date without time (say in the format 'MM-DD-YYYY' as in your example), you could just code
WHERE (date >= to_date(?, 'MM-DD-YYYY') and date < to_date(?, 'MM-DD-YYYY') + 1)
Note the usage of < and not <=.
This works because if no time is specified in the format mask, 00:00:00 (in HH24:MI:SS format) is implied.
As you pointed out the start date is not a problem as it begins at 00:00:00 of the start date. If your paramter is a text box your users can enter 02-01-2014 08:00:00 to get results starting at 8am on Feb 1.
Note that my date format has the year first, while yours has the year last
For the end date, I use a text box paramater with this as my default value
//Creates a date for one second before midnight of the current date,
//which is properly formatted to use as an end date in quires (for my data)
// Note that a custom date format (yyyy-MM-dd HH:mm:ss) is required for proper display in pop-up GUI
var T; T = BirtDateTime.addDay(BirtDateTime.today(),1);
var Y; Y = BirtDateTime.year(T);
var M; M = BirtDateTime.month(T);
var D; D = BirtDateTime.day(T);
{
Y +"-"
+ M +"-"
+ D +" "
+"00:00:00"
}
I also use this help text
Enter date as YYYY-MM-DD. For example, 2013-3-14
Adn this prompt text
End Date (YYYY-MM-DD), if time is blank will default to 00:00:00

Hibernate criteria for get records which have year 2012

i need all records which have year entered from search criteria.
for ex:
String year = "2012";
In hibernate it work for
criteria.add(Restrictions.like("createDate", "%"+year+"%"));
createDate is in format DD-MM-YY.
Please suggest..
criteria.add(Restrictions.like("createDate", "%2012%", MatchMode.ANYWHERE));
But I would prefer changing your db field to a date format(its string at the moment?) and performing a greater than comparison (instead of like).
like only applies for the String . For the date field , you can use Restrictions.between() to generate between '01/01/2012 00:00:00' and '12/31/2012 23:59:59' for the where clause
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date fromDate = df.parse("1-1-2012 00:00:00");
Date toDate = df.parse("31-12-2012 23:59:59");
criteria.add(Restrictions.between("createDate", fromDate, toDate));
Also , please note that all the properties used in the Criteria API is the Java property name , but not the actual column name.

Resources