I have a table in database that contains the 'Date_transaction' column his type is varchar.
In my Code JAVA, I create a SQL query via several conditions.
When I debug in Eclipse the query generated is like this:
SELECT *
FROM Transaction where 1=1
AND (to_date(Date_transaction,'YYYY/MM/DD HH:MI:SS') between '16/01/01' and '16/02/29')
AND projet = 'Project name'
AND nomtranche = 'tranche name' AND voletctrl = 'volet name'
AND (numeroimmeuble BETWEEN 1 AND 100)
AND validation = 1
AND statutDocNormal = 'statut'
AND numeroAppartement = 14
order by DateTrasaction DESC;
I execute this query in SQL DEVELOPER, the query is executed successfully without any error.
But in my code Java I get this Error : java.sql.SQLException: ORA-01843: not a valid month.
When I want to generate the query, I use this method to convert my date, this I spend in parameter (In the query it's : 16/01/01 and 16/02/29):
public static String parseDate2(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
String dt = sdf.format(date);
return dt;
}
I try this answer but it's not working.
You are relying on the session's NLS_DATE_FORMAT, which is set differently in the client and probably indirectly via your Java locale. Use explicit conversion with a specific format mask:
... between to_date('16/01/01', 'RR/MM/DD') and to_date('16/02/29', 'RR/MM/DD') ...
But it would be better to use four-digit years and YYYY (remember Y2K?), or date literals - those those don't work with variable values.
This also looks wrong:
to_date(Date_transaction,'YYYY/MM/DD HH:MI:SS')
If `date_transaction is already a date then you are implicitly converting it to a string and then back to a date, which is pointless and dangerous. And then possibly back to a string to compare with your fixed values. If it is a string then it shouldn't be. Either way you need HH24 rather than just HH so you can distinguish between AM and PM.
If it is a date you need:
...
date_transaction between to_date('2016/01/01', 'YYYY/MM/DD')
and to_date('2016/02/29', 'YYYY/MM/DD')
...
I retrieve timestamp type data from the mysql table. but I just need to return only the date part of this timestamp. Tried in convering timestamp to a date data type. But in jooq this gives some errors.Here is what I retrieve
Field<Timestamp> transaction_date = LINKLK_TRANSACTIONS.MODIFIED_AT.as("transaction_date");
This cannot work:
Field<Timestamp> transaction_date = LINKLK_TRANSACTIONS.MODIFIED_AT.as("transaction_date");
All you're doing is renaming your column to a different name that happens to contain the name "date". You have to use MySQL's date() function, for instance
Field<Date> transaction_date = DSL.date(LINKLK_TRANSACTIONS.MODIFIED_AT);
Or you can cast your Field:
Field<Date> transaction_date = LINKLK_TRANSACTIONS.MODIFIED_AT.cast(Date.class);
There are many other options to do the same, but the above will be sufficient for your particular use-case.
i have table manager where i have columns like date , phonenumber, user.
i want to retrieve records like say i want the phonenumbers from the table whos date lies between 01-01-2014 to 31-01-2014.The operation is like everyday 50 to 100 numbers will be inserted into the table i would like to view the numbers inserted in a particular week or particular date interval.
i am using postgres ans my date column is a varchar type. i am trying to do this in a java web application and the date will be picked using a jquery datepicker.
Sample Data
Date Number user
01-01-2014 244548845 user1
02-01-2014 545454454 user2
05-01-2014 244540045 user1
10-01-2014 244540045 user1
20-01-2014 244540000 user2
when i select 01-01-2014 to 06-01-2014 i would like to fetch first 3 records
SET DATESTYLE = "DMY, DMY";
SELECT
*
FROM
xxx
WHERE
Date::date BETWEEN '01-01-2014' AND '06-01-2014'
ORDER BY
Date::date
LIMIT
3;
Also, mind using the type date for storing dates, and not varchar!
While creating a table the column should be mentioned as date(have to make sure column name is NOT DATE)
ex : create table abc(dataentrydate Date not null);
and for retrieving the records based on date this below query can be used
SELECT * FROM abc WHERE dateentrydate BETWEEN '17-01-2014' AND '22-01-2014' ORDER BY dateentrydate;
Thanks a lot for the response and help provided.
I am getting the following error when inserting data into my oracle database.
java.sql.SQLException: ORA-01843: not a valid month
In database date is as: dd-MMM-yy (06-MAR-12)
I am converting 06-03-2012 to dd-MMM-yy by the following method:
String s="06-03-2012";
String finalexampledt = new SimpleDateFormat("dd-MMM-yy").format(new SimpleDateFormat("dd-MM-yyyy").parse(s));
So i got 06-Mar-12 which is same as the above database date format still i am getting the error. I am inserting as:
in index.jsp
String todaydate="";
Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
todaydate = dateFormat.format(calendar1.getTime());
<input type="text" name="datename" value="<%=todaydate%>"/>
in servlet(doPost)
String s=request.getParameter("datename");
PreparedStatement ps=con.prepareStatement("insert into tablename(rest_dt, othercolname) values (to_date(?, 'dd-mm-yyyy'), ?)");
ps.setString(1, s);
ps.setString(2, otherstringdata);
int rs=ps.executeUpdate();
Any idea please
so make
("insert into mytablename (rest_dt) values to_date(?, 'DD-MM-YYYY')");
Try this
TO_DATE(?, 'DD-MM-YYYY','NLS_DATE_LANGUAGE = American')
// gets from Oracle docs
The datatype of your rest_dt columns is a DATE, so you need to supply one. You can use the TO_DATE function to convert a string to an Oracle DATE, so your insert statement
insert into tablename(rest_dt, othercolname) values (to_date(?, 'dd-mm-yyyy'), ?)
is fine.
Just make sure the string value you bind to your first ?-variable is in the format dd-mm-yyyy. And don't convert or format that value yourself: the TO_DATE function does that part.
There is no need to anything about session settings like nls_date_language here, since you have wisely chosen to use a language agnostic setting for the month with your MM mask (instead of MON).
Regards,
Rob.
Problem is that oracle uses NLS_DATE_LANGUAGE to get the current name of the month. So you should do
select * from nls_session_parameters
and check if you have the correct values. You can also check with the following select which name you get for the month
select TO_CHAR(TO_DATE('01-03-01', 'DD-MM-YY'), 'MON') from dual
I really don't understand why you insert the variable as a string value. Just use a date type (do the conversion on the client) in java and insert it without converting. If you really want to insert it as a string I would use a conversion to something like dd-MM-yyyy and insert it with TO_DATE(, 'DD-MM-YYYY').
Edit:
Do the conversion of the date on the client and use
ps.setDate(2, <yourDate>);
The same issue faced while running big query (multiple union) in Java and issue not with actual input since I have properly converted the with to_date('30-06-2021', 'dd-MM-yyyy') and found issue is with the date1 in query.
e.g.
select a,b,c from table1 where date1='31/12/2015'and date2=<actual input>
union
select a,b,c from table2 where date1='31/12/2015'and date2=<actual input>
union
select a,b,c from table3 where date1='31/12/2015'and date2=<actual input>
.
.
date1 also should be convert to to_date like below
e.g.
select a,b,c from table1 where date1=to_date('31/12/2015', 'dd-MM-yyyy') and date2=<actual input>
Hence issue resolved. My suggestions is, if you are getting such issues check the date part in the query and mention with to_date.
Java code:
#Autowired
private NamedParameterJdbcTemplate namedJdbcTemplate;
List<ResponseDTO> list = new ArrayList<>();
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("value1", dto.getValue1());
params.addValue("value2", dto.getValue2());
list = namedJdbcTemplate.query(SQL_QUERY, params, new CustomValueMapper());
Its purely only my own experience. Click up vote if it helps.
java.time and JDBC 4.2
Don’t transfer a date as a string to or from your database. Transfer a proper date object. I am assuming that your JDBC driver is at least JDBC 4.2 compliant. About all drivers are these days. In this case LocalDate is the type to use for dates, both in your Java program and in the transfer to the database.
So what you basically need is this:
LocalDate date = LocalDate.of(2012, Month.MARCH, 6);
PreparedStatement ps = con.prepareStatement(
"insert into tablename(rest_dt, othercolname) values (?, ?)");
ps.setObject(1, date);
ps.setString(2, otherstringdata);
int rs = ps.executeUpdate();
If you are receiving your date as string input from JSP, immediately parse it into a LocalDate object. There’s no need to wait until you need to put it into your database.
String inputString = "06-03-2012"; // Meaning 6 March 2012
LocalDate date = LocalDate.parse(inputString, DATE_PARSER);
I have been using this formatter:
private static final DateTimeFormatter DATE_PARSER
= DateTimeFormatter.ofPattern("dd-MM-uuuu", Locale.ROOT);
Links
Oracle tutorial: Date Time explaining how to use java.time.
Related question: Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2
I'm a java newbie but I'm not sure what I'm doing wrong. When I try to retrieve the last two dates from a database it only displays the year(while in mysql the same command provides the correct result).
Mysql command: SELECT DISTINCT date From fundanalysis ORDER BY date DESC LIMIT 2
Expected result:
2011-06-13
2011-06-08
Here's my java code:
preparedStatement = con.prepareStatement("SELECT DISTINCT date From fundanalysis ORDER BY date DESC LIMIT 2");
ResultSet numberofrowsresultset = preparedStatement.executeQuery();
numberofrowsresultset.next();
// most recent date
currentdate.add(numberofrowsresultset.getInt("date"));
System.out.print(numberofrowsresultset.getInt("date"));
numberofrowsresultset.next();
// last date before most recent
currentdate.add(numberofrowsresultset.getInt("date"));
return currentdate;
The final result is: [2011, 2011]
I basically want the exact same result as I get when I run the mysql query because I have to submit it as is to do another query later in the program.
pls help!
it is .getDate not .getInt
try:
numberofrowsresultset.getDate("date");
Try use .getDate() instead of .getInt():
currentdate.add(numberofrowsresultset.getDate("date"));
You are using .getInt which returns a numerical value. You need to use .getDate instead when you are getting a date value:
System.out.print(numberofrowsresultset.getDate("date"));
^^^^ change Int to Date
Date is not an integer so your '.getInt("date")' method is not returning the result you expect.
You need
java.sql.Date myDate = numberofrowsresultset.getDate("date");