I am trying to insert datetime string value in Oracle Table on Timestamp(6) column.
But I am getting ORA-01843: not a valid month error.
Is there a way to this groovy specific without specifying oracle functions like to_date or to_timestamp?
2017-12-15 14:39:45
Yes, you can set session parameter:
alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS';
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
After you set this parameter you can enter your date as text string.
You can set these values also as Environment variable or in your Registry at HKLM\SOFTWARE\Wow6432Node\ORACLE\KEY_%ORACLE_HOME_NAME%, resp HKLM\SOFTWARE\ORACLE\KEY_%ORACLE_HOME_NAME%
Try using TO_TIMESTAMP:
SELECT
TO_TIMESTAMP ('2017-12-15 14:39:45', 'YYYY-MM-DD HH24:MI:SS')
FROM DUAL;
Demo
Related
I have a column of type varchar which stores date with timestamp, and without timestamp in postgres.
While trying to hit the below query,
select * from table where cast(column as date) = CURRENT_DATE::date
it is returning those columns also which has date with timestamp also. How can I modify the query to find the exact match of only dates and not with timestamp.
sample input data:
Column1
2022-12-09 17:38:53.415367
2022-12-09
Expected output:
2022-12-09
Actual: getting both the columns in the result
2022-12-09 17:38:53.415367
2022-12-09
Per my comment:
SELECT
t.dt_val
FROM (
VALUES ('2022-12-09'),
('2022-12-09 17:38:53.415367')) AS t (dt_val)
WHERE
t.dt_val::timestamp = '2022-12-09'::date + '00:00:00'::time;
dt_val
------------
2022-12-09
I used '2022-12-09'::date instead of current_date to make the answer relevant in the future. At time of answer current_date resolves to '2022-12-09'::date.
I have looked all over for this answer for a whole day but I'm not getting anywhere.
I have a string that is formatted like this:
dd/mm/yyyy
I want to enter this in to a SQL database
The database field (of type Date) shows the format as yyyy-mm-dd.
At the moment I have the following code:
String inputDate = dateBookingEntry.getText();
LocalDate interDate = LocalDate.now();
interDate.parse(inputDate,DateTimeFormatter.ofPattern("dd/MM/yyyy"));
java.sql.Date finalDate = java.sql.Date.valueOf(interDate);
When I enter the date 12/12/2020 as the string, I get the error message:
java.sql.SQLSyntaxErrorException: You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near ''2020-01-29'
I don't get what I'm doing wrong, or how to fix it. I get that it's showing the current date and not the entered date, but even if that was the problem, why would it show an SQL Syntax error?
What should I change here for it to work?
EDIT:
The SQL insertion code to this table looks like this:
st.executeUpdate("INSERT INTO `fitnessClasses`(`session`, `date`, `price`, `trainer`, `level`, `type`) VALUES ('"+session+"', '"+sqlDate+"', '"+price+"', '"+trainer+"', '"+level+"', '"+classType+"')");
The full SQL code mentioned above with the error looks like this:
ResultSet result = st.executeQuery("SELECT * FROM `fitnessClasses` WHERE `session` = '"+session+"' AND `date` '"+finalDate+"' AND `type` = '"+classType+"' AND `level` = '"+level+"'");
EDIT 2:
OK, I now realise I was missing an =.
Now the SQL SELECT works but it searches for today's date and not the entered date
I think you want the class-level of parse such as
LocalDate interDate = LocalDate.parse(inputDate blah blah)
Currently you are setting interDate to now() which is the current date and that is why you end-up with today's date and not the date entered.
I have problem with HQL where I am setting the query parameters. One of them is Date. When I debug the code there is Date with time entering the method. I set the parameter using setParameter(timestamp, new Timestamp(date.getTime())) or query.setTimestamp...etc etc I used many combinations...
When I use p6spy to examine the SQL comming from app to the DB there is only '29-Jan-21' or other date without time.
I am using hibernate 5.1.0 final and postgre DB. I'll be glad for any help.
Example:
Query query = getSessionFactory().getCurrentSession().createQuery("SELECT user FROM UserEntity cr WHERE user.userStatus.id = :statusId AND :timestamp >= user.valid_to");
This is how I tried to set the timestamp parameter:
query.setParameter("timestamp", new Timestamp(date.getTime()));
query.setParameter("timestamp", date, TimestampType.INSTANCE);
query.setTimestamp("timestamp", date);
query.setTimestamp("timestamp", new Timestamp(date.getTime()));
Problem is that the generated SQL replace timestamp by '29-Jan-21' or other date I choose but without time. The date parameter comes to the method from UI and it contains full date with time.
I am using HSQL as an in memory database for testing. However, I am unable to insert a date into the column.
Below is my table creation script:
SET DATABASE SQL SYNTAX ORA TRUE;
create table OWN_TABLE (
ID DECIMAL NOT NULL,
NAME VARCHAR(24) NOT NULL,
CAPACITY_SUPPLY_DATE DATE ,
);
And the queries I tried:
INSERT INTO OWN_TABLE(ID, NAME, CAPACITY_SUPPLY_DATE) VALUES(2, '4813', '2090-07-15');
But it gives
Caused by: java.sql.SQLDataException: data exception: invalid datetime format
I also tried 2090-07-15 00:00:00 but it didn't work.
Assuming oracle syntax might work, I tried:
INSERT INTO LABS_CAPACITY_SUPPLY(ID, SHIP_NODE, CAPACITY_SUPPLY_DATE) VALUES(1, '4813', 'TO_DATE(2090-07-30,"yyy-MM-dd")');
But got Unparseable date: "TO_DATE(2090-07-30,"yyy-MM-dd")"
Can somebody please tell me the right way to insert dates into HSQL. I have the 2.3.2 jar.
Use a proper standard SQL date literal:
INSERT INTO OWN_TABLE
(ID, NAME, CAPACITY_SUPPLY_DATE)
VALUES
(2, '4813', DATE '2090-07-15');
Or, if you do want to use the to_date() function then don't put it into single quotes.
INSERT INTO LABS_CAPACITY_SUPPLY
(ID, SHIP_NODE, CAPACITY_SUPPLY_DATE)
VALUES
(1, '4813', TO_DATE(2090-07-30,'yyy-MM-dd'));
Note that for the to_date() function the case of the format string does not matter. 'yyyy-MM-dd' is the same as 'YYYY-MM-DD' or 'yyyy-mm-dd'. The format is not the one used for Java's SimpleDateFormat.
If you are doing this from within a Java program you should not use any of the above solutions. Use a PreparedStatement instead:
java.sql.Date dt = ...;
PreparedStatement pstmt = connect.prepareStatement("INSERT INTO OWN_TABLE(ID, NAME, CAPACITY_SUPPLY_DATE) VALUES(?,?,?)";
pstmt.setInt(1, 2);
pstmt.setString(2, "4813");
pstmt.setDate(3, dt);
How you construct the instance of java.sql.Date depends on where you get the data from. You could use a SimpleDateFormat to parse the user's input. Or you could use LocalDate if you are using Java 8, e.g. java.sql.Date.valueOf(LocaleDate.of(2090,7,30))
i am making a program in java.
i am using the following code
u.setLastlogin(new java.util.Date());
above function accepts parameter as java.util.Date but i want to store this value in a database table where the column is of type timestamp?
can any one help how to code so that i can insert the current timestamp of the system in the table. thanks.
You can convert Date to Timestamp as follows:
Timestamp timestamp = new Timestamp(date.getTime());
And if you want timestamp of current date:
new Timestamp(System.currentTimeMillis())
Date now = new Date();
now.getTime();
With JPA:
annotate lastLogin with: #Temporal(TemporalType.TIMESTAMP)
With JDBC:
PreparedStatement#setTimestamp
use this to java.sql.Timestamp timestamp = new java.sql.Timestamp(new Date().getTime());
u.setLastlogin(timestamp);
You could also just issue an SQL statement:
UPDATE MY_TABLE SET LAST_LOGIN = NOW();
This is for MySQL. You should also find this helpful: MySQL Date and Time functions
EDIT
As Rambo requested, setting default value of a column to the actual timestamp (As per MySQL: ALTER TABLE syntax and MySQL Data type default values):
ALTER TABLE MY_TABLE ALTER MY_TIMESTAMP COLUMN SET DEFAULT CURRENT_TIMESTAMP;