In the mysql db I have a field name date type date
the value of the field is as following 2019-11-05
how to query in jooq where date is mentioned as above
I tried following
java.util.Date date = SimpleDateFormat("yyyy-MM-dd").parse("2019-11-05")`
then
.where(DAILY_TRX_SUMMARY.DATE.eq(java.sql.Date(date.getTime()))
But it is not returning result.
What is the problem and how to fix it?
Use java.sql.Date.valueOf("2019-11-05") instead. Otherwise, you inherit JDBC's bad API design decisions, where a java.sql.Date might have timestamp information in some time zone.
Related
I have a list of Rest objects. It's django model
class Rest(models.Model):
product = models.ForeignKey('Product', models.DO_NOTHING)
date = models.DateTimeField()
rest = models.FloatField()
I want to select objects from it for today's date. I do it like this. Maybe there is some more convenient and compact way?
for rest in rests_list:
if rest.date.day == datetime.now().day:
result.append(rest)
First - datetime.now().day will get you the day of the month (e.g. 18 if today is 18th March 2020), but you've said you want today's date. The below is on the assumption you want today's date, not the day of the month.
(NOTE: weAreStarDust pointed out that the field is a DateTimeField, not a DateField, which I missed and have updated the answer for)
The way you are doing it right now seems like it might be fetching all of the Rests from the database and then filter them in your application code (assuming rests_listcomes from something likeRest.objects.all()`. Generally, you want to do as much filtering as possible on the database query itself, and as little filtering as possible in the client code.
In this case, what you probably want to do is:
from datetime import date
Rest.objects.filter(date__date=date.today())
That will bring back only the records that have a date of today, which are the ones you want.
If you already have all the rests somehow, and you just want to filter to the ones from today then you can use:
filter(lambda x: x.date.date() == date.today(), rests_list)
That will return a filter object containing only the items in rests_list that have date == date.today(). If you want it as a list, rather than an iterable, you can do:
list(filter(lambda x: x.date.date() == date.today(), rests_list))
or for a tuple:
tuple(filter(lambda x: x.date.date() == date.today(), rests_list))
NOTE:
If you actually want to be storing only a date, I would suggest considering use of a DateField (although not if you want to store any timezone information).
If you want to store a DateTime, I would consider renaming the field from date to datetime, or started_at - calling the field date but having a datetime can be a bit confusing and lead to errors.
As docs says
For datetime fields, casts the value as date. Allows chaining additional field lookups. Takes a date value.
from datetime import datetime
Rest.objects.filter(date__date = datetime.now().day)
You can use the django filter for filtering and get only today's date data from your model. No need to fetch all data first and then apply loop for get today's date data. You have to write your query like ...
import datetime
Rest.objects.filter(date__date = datetime.date.today())
But be sure that timezone should be same for database server and web server
I'm using node-mssql to get rows from a table that includes a date column (YYYY-MM-DD). I want to pass the date to a client application as a string in that same format. node-mssql is creating date objects, which I'm having to convert and slice at significant cost to get the format that I started with. sql.map.register(String, sql.Date) doesn't seem to work here (or I'm using it wrong). Is there a way to change how node-mssql handles the SQL date data type?
I'm having the same problem. sql.map.register is only for converting JS types to SQL types and I don't think there is any way to avoid SQL Date fields being converted into JS Date objects with node-mssql alone. I just changed my SQL query to convert to a formatted varchar instead of a Date type, e.g.: convert(varchar, birthday, 105) as dob.
I'm using Sequelize with MySQL on my Apollo GraphQL server.
Apollo part is not very important, but here's the deal.
Problem is, Sequelize is returning datetime values as Date object, when you make queries. And Apollo-Server turns the Date object to seconds(using valueOf() function of Date object) after it gets the data from the resolver. However, Apollo server only allow String datatype for Date object, so on my client's side I'm getting some thing like "385823948287" for date values.
I need to get date values as ISO String. Or any other form that moment.js accepts.
I have to make either Sequelize return DATETIME values as ISOString, or Apollo Server to make Date Objects as ISO String, not seconds in quotes.
If you know how to do just one of those jobs, please let me know.
I solved this using Graphql ISO Date. Now I'm getting proper date string.
Install the module, add scalar DateTime and change the data types of date values to DateTime in your schema. Import the module and add it as DateTime on your type resolver.
In a Generic Inquiry, I'm trying to format the time part of a DateTime field in the results. I don't currently see any way to do this without parsing the date as a string, but I must be missing something. Using the Format() function, running the query tells me "The method or operation is not implemented". Using the Minute() function gets the minutes part, but using the Hour() function says "Unsupported formula operator Hour".
Add CRCase table in your tables and don't join with any tables under Relations tab, and in result grid, under Schema field use CRCase.CreatedDateTime, with this you will get the result in DateTime format.
let me know if this is my wrong assumption to your question.
I have a query expression in Cognos where I need to compare a past date to the current date. I don't see one in the functions list and I'm otherwise unsure how to put the query date inside a query object.
How can I use the current date in a query?
Depending on your Database software, the object will be either be current_date (SQL Server) or SYSDATE{} (Oracle). If you don't know which you have, just make an expression of just the function and press the Validate button; if you get an error, you used the wrong function for your database.
You can then use this object like any other Date query object, so you can add/compare it to dates in your query or display it somewhere on the page.
The best way is to use current_date. This method is data source agnostic and will be converted to the appropriate data source equivalent at run-time.
You can use something like this with your query:
SELECT
FIELD1
FROM TABLE
WHERE
FIELD2 = current_date
Asumming that FIELD2 has a date format