Searching Between dates where STOP date is null - mysql

I need your help.
The following is my database table.
http://i42.tinypic.com/10xh4j7.png
A small request to please have a look at the image.
My problem is, when I want to retrieve the rate from date_from and date_to.
For Eg: Lets say retrieving_date = 2013-11-14.
rs = stat.executeQuery("select rate from rate_chart where '"+retrieving_date+"' BETWEEN date_from AND date_to");
This query gives me my required result, for the 11th month.
I get 10.40 as my required rate with the above query.
Lets say, rate has never been changed since 2013-11-26, and now my retrieving_date is 2013-12-20. And now, when I try to execute the above query, it doesn't work. No errors but doesn't give me the last rate. In my case, is 10.80 as mentioned in the above image.
I actually want to search between dates even if the stop date or (date_to) is null.
No, idea how to go forth with this. Stuck for a while now.
Any help would be greatly appreciated.

if the stop date (or date_to) is null, set it to the current date.

Related

How to calculate the difference between days in a table field

Ok this should be a relatively easy thing to do, yet I'm at the head desk stage trying to figure out the insanity here.
I have a table called tblPersonnel. I'm tracking two document expiration dates in date/time fields called CED and PPED. When I run a query against tblPersonnel I need it to look at PPED, determine if that document is expired and if so use CED instead. I have a few fields in the query that need to use this concept to determine what the output value is, but I am hitting a wall here trying to get the query to spit out the correct value. Here's what I'm using for one of the fields - Document Expiration Date: IIf([PPED]-Now()<0,[CED],[PPED]). What's happening is that the expression is constantly popping as false, so PPED is getting used regardless if it's an expired date or not. Does anyone have any ideas as to what I'm doing wrong here?
I've also tried to set this up as its own field in tblPersonnel, but that's even more aggravating. If I try to set the field to just a text field - IIf([PPED]-Now()<0,"Yes","No"), the formula will accept the use of Now(), but it doesn't like the reference to the other fields in the table. If I set it as a calcuated column, I can reference the other fields but it doesn't like Now(). I'm at a loss here.
If PPED is less than Date(), it is expired. Don't need to subtract. Assuming CED and PPED are just date parts, no time, consider:
IIf([PPED] < Date(), [CED], [PPED])
If PPED could be null:
IIf(Nz([PPED],0) < Date(), [CED], [PPED])
Ok finally fixed it here. I had another issue in that I wasn't accounting for how Access would handle a Null or blank value in PPED. The functioning formula is Document Expiration Date: IIf(Len([PPED])>0,IIf([PPED]<Date(),[CED],[PPED]),[CED]) Thanks to June7 for helping me simplify the expression, as I was using DateDiff('d',[PPED],Date())<0 but their answer is just so much cleaner and quicker to type.

SSRS Iif always true when false part contains expression

I've got a report outputting to an Excel file, with some fields in a database being empty.
I'm trying to filter these null values out using the following Iif expression in a cell:
=Iif(
IsNothing(Fields!START_DATETIME.Value),
0,
Fields!START_DATETIME.Value
)
This doesn't work, I get "#VALUE" in the cells where the data was null, and the time value of the cell where there was data.
If I use the same expression without an expression as the third parameter:
=Iif(
IsNothing(Fields!START_DATETIME.Value),
0,
1
)
I get 1s where there is a date in the table and 0s where there is nothing.
I'm sorry if this is a duplicate, I couldn't find anything that worked for me...
Edit:
I'm using VisualStudio Professional 2013
Update
What I've got for each entry in the database is: a datetime for authorization time, another for start time and another for stopped time. There is always an entry for authorization time, but not always for start and stopped.
I've split the date and time out from the various datetimes into columns like so:
Authorized date
Authorized time
Start time
Stop time
The idea is that if no start and stop time are present, just use the authorized time
The following expressions are put in the Start time column. Sorry, I don't have enough rep to post images or more than 2 links. I've put program output in this album:
http://imgur.com/a/zJSAY
Doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value))
)
Outputs the authorized time, so that works fine.
However, doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!START_DATETIME.Value))
)
Outputs the start time where there is an entry, and #VALUE! (null or some such) when there isn't anything.
And doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!START_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value))
)
Outputs the authorized time where there is an entry in start time, and #VALUE! when there isn't anything.
I can't figure this out. I understand (1) and (3), but what I need is for (2) to display the correct start time while there is one, and the authorized time when there isn't.
Update 2
It turns out that Iif() evaluates each conditions even if it is not selected, and any error encountered is passed on regardless. See comments to Why isn't my iif statement evaluating true when the condition is correct?
To fix this I did:
=DateAdd(
DateInterval.Day,
693594,
TimeValue(
Iif(
Fields!START_DATETIME.Value is Nothing,
Fields!AUTH_DATETIME.Value,
Fields!START_DATETIME.Value
)
)
)
This seems to work for me, and should have been the common sense way to do this in the first place...
Thanks again for your help guys.
Excel will guess the type of a column based on the first few cells. If there's typical DATETIME values in those, it'll guess the column is a date column, and it will error with "#VALUE! on values that don't match the type, such as "0".
You should ask yourself what you want to display in Excel for NULL values:
If you just want an empty cell, don't use an expression at all. Just Fields!START_DATETIME.Value will render a NULL value as an empty cell.
If you want a "baseline" datetime, make sure to use that instead of "0". Easiest is probably in your query with something like ISNULL(START_DATETIME, GETDATE()).
If you really want a "0", make sure it appears in one of the first few cells by ordering. Excel will see the "0" and not set the column type to date/time.

CakePHP: Search BETWEEN datetimes and validate form

I'm stuck with this. My own skills aren't enough to solve this myself.
I have a form with these fields:
PLACE_ID
START_DATE (DATETIME)
END_DATE (DATETIME)
What I try to achieve:
If there are already bookings in database in the same time range,
in the same place, the submit will fail.
So, the query would look something like this:
SELECT *
FROM bookings
WHERE place_id=".$_POST['placeId']."
AND('".$_POST['startDate']."'
BETWEEN start_date
AND end_date
OR '".$_POST['endDate']."'
BETWEEN start_date
AND end_date)"
If this returns NULL, the submit will success. How to get this working with CakePHP? Please, help...
From a separation of logic perspective, I think this should be handled in your controller. Building from Ollie Jones advice, upon post-back from the user's browser, you should do something like the following:
$this->Booking->find('all', array('conditions' => array('Booking.start_date BETWEEN' => array($new_start_date, $new_end_date), 'Booking.end_date BETWEEN' => array($new_start_date, $new_end_date))
$new_start_date should be set to $this->data['Booking']['start_date'] and the same goes for $new_end_date. The answer just starts to look messy otherwise!
If the query returns nothing then you can go ahead and save your new booking. If there are bookings, you should advise your user accordingly.
The comments warning you about SQL injection are correct.
Using BETWEEN for DATETIME data type search is problematical. I'm not sure this is your problem, but it might be.
Consider a DATETIME value of, say, '2013-04-13 11:00:00' You'd think this would be BETWEEN '2013-04-13' AND '2013-04-13', but it isn't, because it's after '2013-04-13 00:00:00'.
One of the unpleasant problems with this use of BETWEEN is that single day ranges don't work.
What you need for a date range match is
date_to_test >= start_date
AND date_to_test < end_date + INTERVAL 1 DAY

Last Active List - VB.NET

I've got a client application that's going to update a database every five minutes with the current time, and then I want to output this time as a last active table in a seperate VB application.
I know about mysql time, but I don't quite understand how I can use it to display when a client was last active.
I've looked around and found some stuff about mysql times but I don't fully understand it.
Any help would be great, I'm going to place the results in a ListView with 'Client Name' and 'Last Active' if this helps, and I already know how to connect to my database and retrieve information.
Thank you.
I'd recommend using a DATETIME for storage. The TIME data type is limited to a single "time of day" or a timespan. True, you're looking for the time of day, but to calculate the "Last Active" time you need the date attached. Consider these "Last Active" values (using a 24-hour clock):
3/26/2013 at 17:00:00 <-- this has the maximum time (5PM), but...
3/27/2013 at 08:15:00 <-- ...this is the most recent time because it happens the following day
In other words, you need the date so you can sort the time.
The MySQL DATETIME data type should be supported by VB.NET, but I've never used the two together so I can't guarantee it. To query and report just the time component of the date you have a ton of options. Here are two:
Query the entire date/time from MySQL and return it as a System.DateTime value to VB.NET. In VB.NET you can format it using DateTime.ToString to show only the time components. The MySQL query would go something like this:
SELECT ClientName, MAX(LastActive) AS LastActiveDateTime
FROM your_table
GROUP BY ClientName
Format the time in MySQL and return it as a String to VB.NET. In VB.NET you'll just need to display the string as is. The MySQL query would go something like this:
SELECT ClientName, DATE_FORMAT(MAX(LastActive), '%r') AS LastActiveTime
FROM your_table
GROUP BY ClientName
The format code %r in the above query will return the time in a 12-hour format with AM/PM, for example 07:55:29 PM. To return a 24-hour format (19:55:29), use %T instead.

Mysql temporary database in codeigniter?

i have a deployment to do within 2 days time and for the last three days i have been busy trying to understand this one thing am stuck in. It probably might be very silly of me but please take me as a newbie.
I am using an opensource software php opensource pos and it is made on CI. Now the issue is that the reports in the application are different than what my client wants. I want to change that to the way they want it but i cant.
this is the code:
$this->db->select('sale_date, sum(total) as total, sum(profit) as profit');
$this->db->from('sales_items_temp');
if ($inputs['sale_type'] == 'sales')
{
$this->db->where('quantity_purchased > 0');
}
elseif ($inputs['sale_type'] == 'returns')
{
$this->db->where('quantity_purchased < 0');
}
$this->db->group_by('sale_date');
$this->db->having('sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');
$this->db->order_by('sale_date');
return $this->db->get()->result_array();
every thing is working fine and as expected. Now i need to make some changes but the issue is i cannot find the temp database to see which fields it has. This database table (the temp one) is being used in all the reports functions, same database.
Can some one be so helpful and explain me the way it works, or how is that i can make a change to it!?
The scenario is that it is giving a summary of all the sales in the selected date range as total. I want a break down in terms of cash, credit, credit card, debit card etc.
I have another report which shows that, but not in date wise as in not saying how much in which day but in total of the date range. This one gives the total as in total sales in a day but not the break up of it as in what was what.
You can login here first:
https://demo.phppointofsale.com/index.php/login (id and password is saved on the page)
Then you can paste this link on the address bar and see the report.
https://demo.phppointofsale.com/index.php/reports/summary_sales/1969-12-31/2012-03-13/all/0
I need it to be a breakdown of the total and remove the tax, profit and subtotal columns and change that with the kind of payment as in cash | credit | cheque etc and then a total for that day.
PS: I am sorry for such a big writing but i am really stuck, please help me out. Thank you in advance.
The table name is sales_items_temp.
To see the actual database config (for database name, host, etc), look in either /system/application/config/database.php or /application/config/database.php depending on the version of Codeigniter.
To see about changing the query, check out http://codeigniter.com/user_guide/database/active_record.html.