Date/Time Input Parameters - mysql

I am using two date time parameters in the report. When I use the same parameters in the query, no results are shown. Moreover if I use date parameters then the results are shown but not all records are shown.
I also executed the same query (with date time) in MySQL and the results are shown, I think there is no mistake in the query.
I tried many things. The query is some what like :
SELECT * FROM abc WHERE datetime BETWEEN
date_format($P{fromdate}, '%d-%c-%y 00:00:00')
AND date_format($P{todate}, '%d-%c-%y 23:59:59')

Try without data_format function to check whether error in query or in parameters.
SELECT *
FROM abc WHERE datetime BETWEEN
$P{fromdate} AND $P{todate}

Related

MySQL HOUR(TIMESTAMP) considering timezone

I have a table that contains a column of type TIMESTAMP (which is stored in UTC). My server is currently in timezone 'America/New_York'. I have the following query:
SELECT * FROM table1 WHERE hour(time) > 5;
Which returns the expected output. I then want to change the server's timezone to something else (doesn't matter what) and I want to get the same output as when it was 'America/New_York'. Basically, I want a query that returns all rows where the hour of time in timezone 'America/New_York' is greater than 5, regardless of what the current server timezone is.
I know I could use:
SELECT * FROM table1 WHERE hour(convert_tz(time, ##session.time_zone, 'America/New_York')) > 5;
But I don't want to use variables (##session.time_zone) because I want this SELECT to be included in a view and views don't support variables.
Is there a way to force a TIMESTAMP to be converted to a specific timezone regardless of the current server's timezone?
UPDATE: I found a way to create a function and then call that function from within the view:
create function getTimeZone() returns VARCHAR(50) DETERMINISTIC NO SQL return ##session.time_zone;
This works fine, but I feel like it's a bit of a hack.

Mysql date select from string

i've issue I try this query, do not return any rows. just 0 rows. Even tho there is data matching the request..
select * from repairshop_reservations where date = DATE_FORMAT("11/06/2017 20:00:00", '%d/%m/%Y %H:%i:%s"');
Currently my content of the selected table look like this
The data value of column Date is datetime
you could use str_to_date in this way you can control the proper formatting of the date when you don't use the standard mysql format
select * from repairshop_reservations
where date = str_to_date('11/06/2017 20:00:00', '%d/%m/%Y %H:%i:%s');
You are not inserting a column in your table, so you won't have to define a data type for it. That means that, you are not making changes to the conceptual scheme of your database.
Considering that your table is implemented correctly, the SQL query you would need to give you the desirable result would be:
SELECT * FROM repairshop_reservations
WHERE date = "11/06/2017 20:00:00";
You use the WHERE clause, to filter your record and get an output with a
specified condition. In plain English, what you want to do is:
Select and print for me, every column from the repairshop_reservations table, that has listed date as "11/06/2017 20:00:00"

sql error using between operator with date between two dates

i have an events table having start date and end date I am trying retrieve all the records by giving a date that is between start and end dates.
eg :
SELECT *
FROM `events`
WHERE '2017-01-29' BETWEEN start_date='2017-01-28'
AND end_date='2017-01-31'
but response is syntax error can any one help me to finish the query
Just list the columns.
WHERE '2017-01-29' BETWEEN start_date AND end_date
The values come from the table, you don't put them into the query.
According to mysql documentation (https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between) the syntax for BETWEN is
expr BETWEEN min AND max
it is not
expr BETWEEN blabla=min AND stuff=max
Also, it is rather pointless to be using constants in all three expressions, because in this case the result will be known in advance (either always TRUE or always FALSE) without having to consult the values in your table.
It is kind of hard to give you an example without knowing the structure of your table, but what you probably want is something like
WHERE '2017-01-29' BETWEEN start_date
AND end_date
(assuming start_date and end_date are columns in your table)
or something like
WHERE some_column BETWEEN '2017-01-28'
AND '2017-01-31'
(assuming some_column is a column in your table.)
I believe you're trying to find all the rows where a date is 2017-01-29, and so, your query could be:
SELECT *
FROM `events`
WHERE
date = '2017-01-29';
If, however, you want all rows with date between 2017-01-28 and 2017-01-31, then you could do:
SELECT *
FROM `events`
WHERE
date BETWEEN '2017-01-28' AND '2017-01-31';
Instead of putting 2017-01-29 before WHERE, put the name of the field you want to filter by date, such as EventDate (or whatever your field is named).

How to run a query with date from a form?

I have a select query that returns records based on 2 dates from a form - >=Forms!frmReport!StartDate AND <=Forms!frmReport!EndDate.
The problem is that whenever the user enters the same date in the both StartDate and EndDate no record is returned even though I'm sure there is data in the table that matches the criteria. Can anyone say why the query isn't returning any information?
Cast the form values to just date values in your query:
>= cdate(Forms![frmReport]![StartDate]) and <= cdate(Forms![frmReport]![EndDate])
And set your field default to =date() to capture the date only, instead of =now().
If the field being compared is rs("Date"), try
Int(rs("Date")) >=Forms!frmReport!StartDate AND Int(rs("Date")) <=Forms!frmReport!EndDate
For the Query Builder, modify your date checking field to something like this:
Replace YourDateField with the name of your field.
And if time is relevant, get rid of the field expression in the query builder, and instead compare the record's field to:
>=Forms!frmReport!StartDate AND <= iif(Forms!frmReport!EndDate = Forms!frmReport!StartDate, dateadd("d",1,Forms!frmReport!EndDate), Forms!frmReport!EndDate)
This expression says if the end date is the same as the start date add a day, otherwise just use the end date.

Date functions trim off timestamp in DB2

Is there a way to trim off the timestamp in a DB2 date function?
Have DB2 select statement where I'mm selecting a date form the databease and saving it to a variable. I then use that variable as a parameter for another db2 select by adding 30days to it but I don't think it agrees with the timestamp that it is adding to the end.
Select business_date From DB2INST1.BusDate Where key = 0
There is no timestamp in the database for this date but its adding '12:00:00AM' to the end
it saves this select into a variable and I use it in another select here
where expirdate > (DATE(#BusDate) + 30 DAYS)
I get this error:
{"ERROR [428F5] [IBM][DB2/AIX64] SQL0245N The invocation of routine \"DATE\" is ambiguous. The argument in position \"1\" does not have a best fit."} System.Exception {IBM.Data.DB2.DB2Exception}
Select business date as a varchar/string
Select varchar_format(business_date,'YYYY-MM-DD')
then do this to use it later, convert it to a date then use a date function to add 30days to it:
(DATE(to_date(#BusDate, 'YYYY-MM-DD') + 30 DAYS))
Try
where expirdate > DATE(#BusDate + 30 DAYS)
Is the first SELECT statement and the second SELECT Statement part of same stored procedure? Are you storing the resulting date in any .Net variable? If you are storing it in a .Net DateTime variable, my suggestion is to do the date addition operation in .Net code itself. And then remove the time part from the variable before passing to the database.
Take a look at this too: Datetime field overflow with IBM Data Server Client v9.7fp5
If both the SELECT statements were part of a stored procedure and there is no .Net DateTime variable invloved, then it is a different story.