Postgresql: Select all data between two dates in playframework 1.x - postgresql

I have a postgresql table with data connected to dates. Each row has data and a date, for example:
2011-02-03 JOHN Male etc
I am using playframework. i have created a model named student.java.
and in application.java i have done something like this where firstDate and lastdate are given by user,
List (student studentList) = student.find("timestamp BETWEEN 'firstDate' and 'lastDate'").fetch();
i want to fetch all the data present between those two dates.
But i know the query is not correct. Please help me out.
Thanks in advance. :)

public static final List<Student> findWithDates(Date firstDate, Date lastDate) {
return find("from Student student where student.date >= ? and student.date <=?",
firstDate, lastDate).fetch();
}

Related

How to assign current date to a date field in odoo 10

How to show current date before clicking the date field in odoo?
Odoo Date field class provides methods to get default values for like today.
For dates the method is called context_today() and for datetimes context_timestamp(). You are able to pass a timestamp to this methods to either get today/now (without timestamp) or a timestamp which will be formed by the logged in users timezone.
Code Example:
from odoo import fields, models
class MyModel(models.Model):
_name = 'my.model'
def _default_my_date(self):
return fields.Date.context_today(self)
my_date = fields.Date(string='My Date', default=_default_my_date)
Or the lambda version:
my_date = fields.Date(
string='My Date', default=lambda s: fields.Date.context_today(s))
I found it.It is Simple, just write this on your python code like:
date = fields.Datetime(string="Date", default=lambda *a: datetime.now(),required=True)
or
like this
date = fields.Datetime(string="Date current action", default=lambda *a: datetime.now())
or
like this
date = fields.Date(default=fields.Date.today)

JPA-Query for a Date in a Datetime column

I'm trying to build a query for all orders which were created today.
My Order-Entity has a datetime field like this:
#Column(name = "OrderCreationDate")
#Temporal(TemporalType.TIMESTAMP)
private Date orderCreationDate;
Named-Query:
#NamedQuery(name = "OrderHeader.findByOrderCreationDate", query = "SELECT o FROM OrderHeader o WHERE o.orderCreationDate = :orderCreationDate")
I tried to build the query like this:
public List<OrderHeader> findFromToday() {
Date dateToday = new Date();
TypedQuery<OrderHeader> query = em.createNamedQuery("OrderHeader.findByOrderCreationDate", OrderHeader.class).setParameter("orderCreationDate", dateToday);
return query.getResultList();
}
Of course the ResultList is empty since the date AND time would have to match.
Unfortunately I need the time in my database, so the orderCreationDate needs to stay datetime/timestamp.
So how can I query for a specific date, ignoring the time?
thanks!
Your call to setParameter needs to pass in the temporal type argument also, defining what to use for comparison.
http://www.datanucleus.org/javadocs/javax.persistence/2.1/javax/persistence/Query.html#setParameter-java.lang.String-java.util.Date-javax.persistence.TemporalType-

Selecting all records created current year with Eloquent

I need to select records created at current year, with Eloquent
So far, this is the code I'm using. How can I filter the results to retrieve only the ones created in the current year?
public function vacationsbalance($typeId) {
// Get vacataions balance for existing employee.
$vacationsBalance = $this->vacations()->where('vacation_type_id', '=', $typeId)->sum('days_num');
return $vacationsBalance;
}
Assuming you have timestamps in your table (that is, you have a created_at column with the record's creation date), you can use:
public function vacationsbalance($typeId) {
// Get vacations balance for existing employee and for the current year.
return $this->vacations()
->where('vacation_type_id', '=', $typeId)
->whereRaw('year(`created_at`) = ?', array(date('Y')))
->sum('days_num');
}
Check Eloquent's documentation and look for whereRaw.

Using IQueryable with and without AsQueryable()

I would like to know what happens when I use IQueryable with and without AsQueryable(). Here is an example:
public partial class Book
{
.......
public Nullable<System.DateTime> CheckoutDate{get; set;}
}
I need to filter the data from SQL server before it is returned to an application server. I need to return books checked out more recently than entered date. Which one should I use?
A.
IQueryable<Book> books = db.Books;
books = books.Where(b => b.CheckoutDate >= date);
B.
IQueryable<Book> books = db.Books.ToList().AsQueryable();
books = books.Where(b => b.CheckoutDate >= date);
Basically I would like to know what is the difference between the above two options. Do they work on the similar grounds? Do they return same values?
With B option, you're basically retrieving every book from database and filtering data in memory.
A option is more performance, as it filters data at the database and return only the rows that match your query.

Retrieve specific table rows using Java Persistence

I have a table patient_details(patient_id, first_name, last_name, address,date_of_birth, gender, contact_number,occupation). I have generated an entity class and a PersistenceUnit. I can only find an object using its ID:
PatientDetails pd = em.find(PatientDetails.class,patient_id);
I want to know how to find an object by using other column name(s) instead of just the primary key.
Have a look on JPQL or JPA Critera.
https://docs.oracle.com/html/E24396_01/ejb3_langref.html
http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html
Execute Querys:
http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#createQuery%28javax.persistence.criteria.CriteriaQuery%29
For example:
if
class PatientDetails
String first_name;
#Column(",date_of_birth")
Date birth;
...
}
then
String sql = "SELECT p FROM PatientDetails p where p.first_name = :fname and p.birth > :generation";
Date generation = new Date(int 1980, 0, 1);
TypedQuery<PatientDetails> query = EM.createQuery(sql);
query.setParameter("fname","John");
query.setParameter("generation",generation);
return query.getResultList
returns patients called John and born after 1980.
But you should read the links recommended by #pL4Gu33