Specifying date range in a request in MySQL - mysql

I have a table with some info about trade goods, kinda logistics. It has a column called 'arriving_date', which contains the arrival dates in a format like this "30.06.2019".
Now I need to enter a request which displays only the goods which have not arrived yet, in other words which arrival date is > 30.06.2019. So I'm entering my request
SELECT *
FROM traffic
WHERE arriving_date > '30.06.2019'
But this request shows me just blank field. And I think I've already guessed why. Apparently it can't compare numbers in a format like that. But I'm not sure how should I make it compare dates then. Help is appreciated.

Assuming that arriving_date is just a string (and not an actual date)
SELECT *
FROM traffic
WHERE str_to_date(arriving_date, '%d.%m.%Y') > now()
OR arriving_date is null;
This will convert the arriving_date string into a date and compare it to the current date and time.
I also added a check for arriving_date being null (ie not set) in case it's set upon arrival.
Note that using a function on the value from a column makes all queries run full table scan. There is no way around that since your date format is as it is.

Thanks everyone for help!
I manage to solve my problem this way:
1. I changed arriving_date data type to 'date' (it was integer)
2. I rewrote dates from format 'dd.mm.yyyy' to 'yyyy-mm-dd', not sure if it matters.
3. final request looks like this:
SELECT *
FROM traffic
WHERE arriving_date > NOW()
It works perfectly now.

assuming that the arriving_date column is a porper date data type try convert the date string in a proper date
SELECT *
FROM traffic
WHERE arriving_date > str_to_date('30.06.2019', '%d.%m.%Y')
or if both date

Related

How to load data within date range on Qlik Sense?

I would like to load data within a defined date range on Qlik Sense.
When I load the data, I set it to be in the format below:
SET DateFormat='DD/MM/YYYY(WWW)';
I hope to use a where statement to limit the data where the column variable [Date] is within a date range.
However, the below statement placed after the LOAD columns FROM table statement did not work:
where [Date]<'30/11/2016(Wed)' and [Date]>'01/12/2015(Tue)'
May I know what is the syntax for
If you want to limit a date in MySQL to a range, using the date bounds of the range alone is enough, i.e.
WHERE date BETWEEN '2015-01-12' AND '2016-11-30'
Specifying the day of the week is redundant and unnecessary, because for example Novmeber 12, 2015 is always a Tuesday.
If your source date data has the format dd/mm/YYYY then you can use the STR_TO_DATE() function to parse into a date. After that, you can make the same comparison:
WHERE STR_TO_DATE(date, '%d/%m/%Y') BETWEEN '2015-01-12' AND '2016-11-30'

How to pass a Date value to java API call in Different format?

I have a date column in my table, which will display a date as '17-MAR-15' format. Now I need to pass the date as 'DD-MM-YYYY(17-03-2015)' format to the Java API call. How can I pass this date parameter in that format?
You should take a look at: DATE_FORMAT
But basically what you do is this:
SELECT DATE_FORMAT(dateColumn, '%d-%m-%Y') FROM xyz;
Edit: Well, should have waited until I was really awake to answer, but if you happen to convert your date column to the date type this is how you would get the date in the specified format.

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.

Reporting Services Remove Time from DateTime in Expression

I'm trying to populate an expression (default value of a parameter) with an explicit time. How do I remove the time from the the "now" function?
Something like this:
=FormatDateTime(Now, DateFormat.ShortDate)
Where "Now" can be replaced by the name of the date/time field that you're trying to convert.)
For instance,
=FormatDateTime(Fields!StartDate.Value, DateFormat.ShortDate)
Since SSRS utilizes VB, you can do the following:
=Today() 'returns date only
If you were to use:
=Now() 'returns date and current timestamp
=CDate(Now).ToString("dd/MM/yyyy")
Although you are hardcoding the date formart to a locale.
If you have to display the field on report header then try this...
RightClick on Textbox > Properties > Category > date > select *Format (Note this will maintain the regional settings).
Since this question has been viewed many times, I'm posting it... Hope it helps.
Just use DateValue(Now) if you want the result to be of DateTime data type.
If expected data format is MM-dd-yyyy then try below,
=CDate(Now).ToString("MM-dd-yyyy")
Similarly you can try this one,
=Format(Today(),"MM-dd-yyyy")
Output: 02-04-2016
Note:
Now() will show you current date and time stamp
Today() will show you Date only not time part.
Also you can set any date format instead of MM-dd-yyyy in my example.
In the format property of any textbox field you can use format strings:
e.g. D/M/Y, D, etc.
One thing that might help others is that you can place: =CDate(Now).ToString("dd/MM/yyyy") in the Format String Property of SSRS which can be obtained by right clicking the column. That is the cleanest way to do it. Then your expression won't be too large and difficult to visually "parse" :)
FormatDateTime(Parameter.StartDate.Value)
I'm coming late in the game but I tried all of the solutions above! couldn't get it to drop the zero's in the parameter and give me a default (it ignored the formatting or appeared blank). I was using SSRS 2005 so was struggling with its clunky / buggy issues.
My workaround was to add a column to the custom [DimDate] table in my database that I was pulling dates from. I added a column that was a string representation in the desired format of the [date] column. I then created 2 new Datasets in SSRS that pulled in the following queries for 2 defaults for my 'To' & 'From' date defaults -
'from'
SELECT Datestring
FROM dbo.dimDate
WHERE [date] = ( SELECT MAX(date)
FROM dbo.dimdate
WHERE date < DATEADD(month, -3, GETDATE()
)
'to'
SELECT Datestring
FROM dbo.dimDate
WHERE [date] = ( SELECT MAX(date)
FROM dbo.dimdate
WHERE date <= GETDATE()
)
My solution for a Date/Time parameter:
=CDate(Today())
The trick is to convert back to a DateTime as recommend Perhentian.
Found the solution from here
This gets the last second of the previous day:
DateAdd("s",-1,DateAdd("d",1,Today())
This returns the last second of the previous week:
=dateadd("d", -Weekday(Now), (DateAdd("s",-1,DateAdd("d",1,Today()))))
This should be done in the dataset. You could do this
Select CAST(CAST(YourDateTime as date) AS Varchar(11)) as DateColumnName
In SSRS Layout, just do this =Fields!DateColumnName.Value
Just concatenate a string to the end of the value:
Fields!<your field>.Value & " " 'test'
and this should work!