I want to show a query where colname - DueDateTime can be verified either date or time (its datatype is VARCHAR only & its content is date & time values separated with a white space)
Using a WHERE condition if any of the value is true, because I have many rows with same date but different time, I want to show all with the same dates.
`SELECT * FROM NAME_TBL WHERE DueDate = ?`
My information is specific but the question is misleading so I changed the question title.
M.Waqas Pervez has the right answer but it should be %'yourDate'%
you can use groupby clause on DueDate.
try below query.
SELECT * FROM NAME_TBL WHERE Convert(Date,DueDate) = '2016-11-25'
Related
I have a table in which there are two columns namely startTime and endTime (both are DateTime dataTypes) there is another column 'Duration' which calculates the difference between these two. I want to retrieve Duration between specific dates like June10-June20. The problem is I've many rows for same date and Few dates between the range dont even exist. I need to plot the graph for the specified range. So, I have to append zero to the output when there is no entry for a particular day and the similar day's value has to be added together and returned as a single value. So, the output rows should be equal to the number of days specified.
Click here for Example
Thanx in advance.
Assuming that the start and end dates in every row would differ only in the time component.
Also assuming that you do not have any other table that has dates listed and you need a dynamic table to list all the dates between the given range.
You could do the following.
select list_of_dates.date, ifnull(sum(your_table.duration),0)
from your_table
right join
(SELECT DATE(ADDDATE('2016-06-20', INTERVAL #i:=#i+1 DAY)) AS date
FROM your_table,
(select #i:=-1) local
HAVING
#i < DATEDIFF('2016-06-25', '2016-06-20') ) list_of_dates
on list_of_dates.date = date(your_table.start_date)
group by list_of_dates.date
If you have a table that stores relevant calendar it gets easier
select calendar_table.date, ifnull(sum(your_table.duration),0)
from your_table
right join
calendar_table
on calendar_table.date = date(your_table.start_date)
where calendar_table.date >= '2016-06-20' and calendar_table.date <= '2016-06-25'
group by calendar_table.date
I created one birt report.It contains one report parameter with name "FromDate".User will insert it in a fromat like "dd/MM/yyyy". I want to get year(if date is 11/01/2013.i want to extract 2013 from this varaible) from this input parameter and want to pass this value to one stored procedure(ie data set).How can i do that.Can anyone pls share sample code
You could define a variable 'selectedYear' like this:
var selectedValue = params["FromDate"].value;
var selectedValueArray = selectedValue.split("/");
selectedValueArray[2]
This code creates an array by "cutting up" the parameter value on the /'s, and then selecting the year (the second value in the array, which is the third block since we count from zero).
You can then use vars["selectedYear"] on your data set.
Alternatively, if your data set contains a list of years, you can create a data set
select distinct YEAR
from [dataSource]
and have your parameter select from that list instead. This will also guarantee that the selected year is in range.
You can map dataset parameters to report parameter-derived values in the Parameters section of the Edit Dataset dialog:
Select the stored procedure year input parameter from the list of dataset parameters and click Edit... .
Click on the fx button next to the Default Value and select Javascript Syntax .
Enter the formula BirtDateTime.year(params["FromDate"].value) .
Click on OK to confirm these changes in each dialog. (The Linked To Report Parameter option needs to be left as None for that dataset parameter, since you don't want to set the procedure input parameter value to be exactly the same as any of the Report Parameters.)
I had a similar problem in the past, however i looked at from a different angle in driving the date selection from the dates stored in the datasource as a dynamic parameter. Normally then a date from the dataset is in a format which can be formatted/extracted by functions to give the year month day etc.
i tried this one..in corresponding stored procedure i will do a substring on input paramaeter(ie FromDate)..it is working
In sql query(Data Set), Use this
"split_part(?,'/',3)" , We will have date parameter in place of '?' . As, you must be aware of that, Split_part will divide the string using the delimiter '/' .
I've been thinking it over, and I'm starting to wonder if this is even possible.
User Perspective:
There's a table of data, and one column contains a date. The user can type in a search term like dec and get all rows that occurred during December.
Backend: A jqGrid is used for displaying the table. It sends the entered search terms to the server. The server uses the code
Criteria cr = session.createCriteria( DetailedLogEntry.class );
Disjunction disjunction = Restrictions.disjunction();
MatchMode matchMode = MatchMode.ANYWHERE;
disjunction.add( Restrictions.ilike( searchKey.getField(), searchKey.getData(), matchMode ) );
cr.add( disjunction );
to apply the search terms, and where DetailedLogEntry contains a Date variable to represent the database's TIMESTAMPfield.
Because searchKey.getData() returns a string, comparing it against a date object results in an empty set.
So I guess the question is...is it possible, preferbly through Hibernate, to apply a restriction against a Date object as if it were a String?
That's not possible. You'd need to use Restrictions.between() and give it an upper and lower date values. You could use SimpleDateFormat to convert from your String values to Date values and then perform the search?
If the user searched for Dec, would you expect all the log entries from December of every year to show up? Can they type in :"1, Dec" and expect to see all the logs from the 1st December for every year? If it is string matching on dates you are looking for, it might be easier to load all the data into your jqGrid and use javascript to filter the table based on the string formatted date values.
Below is the snapshot of what I have got as a query from sqlite db.
After googling and reading a number of question around, I have come to know that to retrieve maximum datetime using aggregate functions like max() is not possible as sqlite doesn't support much datatypes but treats datatype as text.
So, I have brought this data in a List or at java level. So how could I now get the maximum datetime from this list.
Is there any direct construct for this format in java. Or do we have something at sqlite level that I coudn't find.
texts can be compared, sorted and ordered in SQLite.
Your format appears to be YYYY-MM-dd hh:mm:ss. Lucky for you, ordering this format result in ordering by date.
just
select current_test_id from someTable order by test_datetime desc limit 1
or
select current_test_id, max(test_datetime) from someTable
(or something, not entirely sure for the second one)
if you set the type of datetime field text then you can perform following query
but datetime must be yyyy-mm-dd hh:mm:ss
select max(datetime) from tableName;
A common approach is to store the data converted as long.
Use date.getTime() to get long from your Date instance and Date date = new Date(timestamp); to get a date object from your timestamp.
Once you have a long in your db you can perform any ordering / comparison you want.
To retrive max value from a set of Time of type (String) We have to do some concatenations using sub string .Using this Query max or min Time can be find out using sql lite
select max(datetime(case
when substr(TimeIn,7,2)='PM'
then substr(TimeIn,1,2)+12
else substr(TimeIn,1,2)
end || ':' || substr(TimeIn,4,2) || ':' || '00'))
from tablename
where Date='10/06/2016'
I have a database table containing dates
(`date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00').
I'm using MySQL. From the program sometimes data is passed without the date to the database. So, the date value is auto assigned to 0000-00-00 00:00:00
when the table data is called with the date column it gives error
...'0000-00-00 00:00:00' can not be represented as java.sql.Timestamp.......
I tried to pass null value to the date when inserting data, but it gets assign to the current time.
Is there any way I can get the ResultSet without changing the table structure?
You can use this JDBC URL directly in your data source configuration:
jdbc:mysql://yourserver:3306/yourdatabase?zeroDateTimeBehavior=convertToNull
Whether or not the "date" '0000-00-00" is a valid "date" is irrelevant to the question.
"Just change the database" is seldom a viable solution.
Facts:
MySQL allows a date with the value of zeros.
This "feature" enjoys widespread use with other languages.
So, if I "just change the database", thousands of lines of PHP code will break.
Java programmers need to accept the MySQL zero-date and they need to put a zero date back into the database, when other languages rely on this "feature".
A programmer connecting to MySQL needs to handle null and 0000-00-00 as well as valid dates. Changing 0000-00-00 to null is not a viable option, because then you can no longer determine if the date was expected to be 0000-00-00 for writing back to the database.
For 0000-00-00, I suggest checking the date value as a string, then changing it to ("y",1), or ("yyyy-MM-dd",0001-01-01), or into any invalid MySQL date (less than year 1000, iirc). MySQL has another "feature": low dates are automatically converted to 0000-00-00.
I realize my suggestion is a kludge. But so is MySQL's date handling.
And two kludges don't make it right. The fact of the matter is, many programmers will have to handle MySQL zero-dates forever.
Append the following statement to the JDBC-mysql protocol:
?zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=UTF-8&characterSetResults=UTF-8
for example:
jdbc:mysql://localhost/infra?zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=UTF-8&characterSetResults=UTF-8
Instead of using fake dates like 0000-00-00 00:00:00 or 0001-01-01 00:00:00 (the latter should be accepted as it is a valid date), change your database schema, to allow NULL values.
ALTER TABLE table_name MODIFY COLUMN date TIMESTAMP NULL
As an exteme turnaround, when you cannot do an alter to your date column or to update the values, or while these modifications take place, you can do a select using a case/when.
SELECT CASE ModificationDate WHEN '0000-00-00 00:00:00' THEN '1970-01-01 01:00:00' ELSE ModificationDate END AS ModificationDate FROM Project WHERE projectId=1;
you can try like This
ArrayList<String> dtlst = new ArrayList<String>();
String qry1 = "select dt_tracker from gs";
Statement prepst = conn.createStatement();
ResultSet rst = prepst.executeQuery(qry1);
while(rst.next())
{
String dt = "";
try
{
dt = rst.getDate("dt_tracker")+" "+rst.getTime("dt_tracker");
}
catch(Exception e)
{
dt = "0000-00-00 00:00:00";
}
dtlst.add(dt);
}
I wrestled with this problem and implemented the URL concatenation solution contributed by #Kushan in the accepted answer above. It worked in my local MySql instance. But when I deployed my Play/Scala app to Heroku it no longer would work. Heroku also concatenates several args to the DB URL that they provide users, and this solution, because of Heroku's use concatenation of "?" before their own set of args, will not work. However I found a different solution which seems to work equally well.
SET sql_mode = 'NO_ZERO_DATE';
I put this in my table descriptions and it solved the problem of
'0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
There was no year 0000 and there is no month 00 or day 00. I suggest you try
0001-01-01 00:00:00
While a year 0 has been defined in some standards, it is more likely to be confusing than useful IMHO.
just cast the field as char
Eg: cast(updatedate) as char as updatedate
I know this is going to be a late answer, however here is the most correct answer.
In MySQL database, change your timestamp default value into CURRENT_TIMESTAMP. If you have old records with the fake value, you will have to manually fix them.
You can remove the "not null" property from your column in mysql table if not necessary. when you remove "not null" property no need for "0000-00-00 00:00:00" conversion and problem is gone.
At least worked for me.
I believe this is help full for who are getting this below Exception on to pumping data through logstash
Error: logstash.inputs.jdbc - Exception when executing JDBC query {:exception=>#}
Answer:jdbc:mysql://localhost:3306/database_name?zeroDateTimeBehavior=convertToNull"
or if you are working with mysql