I am using mysql server 5.X
update ACA_CHECK_HISTORY set Datecolumn=CONVERT(VARCHAR(15),getdate(),103);
I am getting syntax error.
Try using MySQL syntax and not SQL Server syntax:
update ACA_CHECK_HISTORY
set CREATED_DATE = date_format(now(), '%Y-%m-%d');
Oh, wait, that's not quite what you want. It is better than what you want.
First, you should store date/time values in the native format in the database. If, for some reason, you cannot do this, then use an ISO standard format -- such as YYYY-MM-DD. This works for sorting and comparisons, which makes it very useful.
You can convert the value to an output format when presenting it to users. You seem to want the format '%d/%m/%Y'. Nothing wrong with that format. It is just a presentation format and should not be stored in the data.
If that column is a DATE or DATETIME or TIMESTAMP datatype, then simply do
update ACA_CHECK_HISTORY
set CREATED_DATE = NOW();
Better yet, you can avoid the UPDATE entirely if you use a TIMESTAMP with suitable DEFAULT.
Related
I am porting my Java application which was developed for Windows to AIX Unix. On Windows it uses SQL Server for configuration. On AIX we are trying to use H2 database. Most of the code works but I am getting following error when executing query which has a datetime criteria.
org.h2.jdbc.JdbcSQLDataException: Cannot parse "DATE" constant "26-Jun-2019"; SQL statement:
SELECT EM_SCHEDULER_DAILY_POLL.* FROM EM_SCHEDULER_DAILY_POLL, EM_CONTROLLER WHERE EM_SCHEDULER_DAILY_POLL.CONTROLLER_ID =
EM_CONTROLLER.CONTROLLER_ID AND EM_SCHEDULER_DAILY_POLL.DATE_TIME
BETWEEN '26-Jun-2019' AND '26-Jun-2019 23:59:59' AND
POLLED_SUCCESSFULLY=0 AND EM_SCHEDULER_DAILY_POLL.CONTROLLER_ID=30
[22007-199]
This SQL works perfectly on SQL server but gives above exception on H2DB. How to solve this issue? I need both date and time in query.
Try using ISO date literals:
WHERE
EM_SCHEDULER_DAILY_POLL.DATE_TIME >= '2019-06-26' AND
EM_SCHEDULER_DAILY_POLL.DATE_TIME < '2019-06-27'
Note that since you are just looking for records on a single date, you could also try casting the column to date and doing a single comparison:
WHERE CAST(EM_SCHEDULER_DAILY_POLL.DATE_TIME AS DATE) = '2019-06-26'
As another comment, the first version I gave, with the two inequalities is sargable, meaning that the database should be able to use an index on the DATE_TIME column, while the second version, using the cast to date, probably cannot use an index. Therefore, the first version is the preferred way to go, if you ever need to tune or optimize your database.
You are passing a value with a time but H2 Date only don't have one.
Just remove the time in your second constant.
'26-Jun-2019 23:59:59' --> '26-Jun-2019'
DATE The date data type. The format is yyyy-MM-dd.
Mapped to java.sql.Date, with the time set to 00:00:00 (or to the next
possible time if midnight doesn't exist for the given date and
timezone due to a daylight saving change). java.time.LocalDate is also
supported on Java 8 and later versions.
Example:
DATE
Source :Data type of H2.
And since you just want one day (at least in that example), you can simply use :
DATE_TIME = '26-Jun-2019'
Note that Tim Biegeleisen's answer about ISO should be checked too, this format is not the best
Use TO_DATE function
Example - TO_DATE('01-12-2019','dd-MM-yyyy')
Insert into student(Id,Name,DOB) values(1, 'Abc', TO_DATE('01-12-2019','dd-MM-yyyy'))
There is a converter PARSEDATETIME() fuction.
For example if the date is 12/03/2013 we need to convert as PARSEDATETIME('12/03/2013','dd/MM/yyyy') check this SO.
The SQL statement look like Insert into invoice(id, invoice_date) values(1, PARSEDATETIME('12/03/2013','dd/MM/yyyy'))
In my Java app I must save data to Oracle 11g with object created date and time and for this I convert java.util.Date() to java.sql.Date() in format as new java.sql.Date(new java.util.Date().getTime()). But I noticed, that after the data inserted, oracle truncate the time part of date and I get something like 16/09/2015. but I need format like this: 16/09/2015 9:55:44. the second format created by oracle's sysdate() procedure. How I can get the second format from java code?
From memory (it's been a while), java.sql.Date is used to hold dates (no time) only, if you want time information as well, you need to use java.sql.TimeStamp instead.
Do not use java.sql.Date if you want to store date and time. Just use java.util.Date without any conversion. Simple and easy. There is no need for java.sql.TimeStamp either.
And make sure your NLS settings (e.g. in SQL Developer) are such that they display both date and time as Oracle does not distinguish between dates with and without time.
I guess Oracle does not truncate. For example, if you use Oracle SQL Developer, you have to update NLS parameter "Date Format" to see a time.
By default it just equal "DD-MON-RR"
When I need to store time/date information, how is it best to be stored in a database?
If I store them as a String representation e.g. get current timestamp (using Date for example) and store the String representation in the DB is it a good idea?
What is the best practice for this? What are any cons?
The best practice is to use a TIMESTAMP or DATE type in the database. All major database vendors support these SQL standard data types.
Apart from performance and storage optimisation, you'll gain expressiveness and you'll be able to use a lot of datetime manipulation functions and operations directly in the database, depending on the database you're using. Think about comparing dates, adding dates, adding intervals to dates (e.g. what day is tomorrow), etc...
Note that...
If you're using Oracle, beware that DATE also contains time information
If you're using SQLite, beware that datetime types may have numeric or text affinity. I.e. they're stored as numbers or text.
If you're using SQL Server, Sybase (ASE and SQL Anywhere), SQLite, the dialect-specific version of the TIMESTAMP data type is called DATETIME (SQL Server's TIMESTAMP is actually a VARBINARY(8))
At least H2, HSQLDB, MySQL, Sybase SQL Anywhere support both TIMESTAMP and DATETIME data type synonyms.
Its best to use a timestamp datatype in both MySQL and T-SQL.
Using timestamps allows you to perform calucations on the data far easier queries, addings or substrating days, months, years whatever you like. Also ordering is easier.
Timestamps also contain a timezone part to, so if you ever need to internationalise your site it would be easier.
Most languages such as PHP also have functions to display timestamps in a format of your choosing so you can convert timestamps to more readable formats.
Finally javascript/jquery plugins often require full timestamps to perform calculations on dates and times jQuery timeago for example.
The Best practice is use the DATE / TIMESTAMP datatype available from your database vendor, one of the benefits of having the essential datatype is that "in future you can query for date time specific conditions", for example in future you may require to
Generate a report of a specific month/year
Generate report between two dates i.e between 01/05/11 and 01/02/12 etc.
To display date wise report (i.e.total sales on 15th August,11), all though this can also be done using String datatype, I think that date comparison are much faster than of String representations (char or Varchar etc).
4.....
Apart from above conditions, they are many more cases for which it is wiser to use Date ? TimeStamp instead of String....
I am inserting a record to orcle db through java application. The date value inserted as 02/10/0010 instead of 02/10/2010 HH:MM:SS AM/PM? I am using oracle jdbc connection. Does it problem with JDBC driver ?
Any input on this.
If you're using Oracle and JDBC, don't store Date in your table as a String. Make it a real Date and you'll spare yourself all this pain.
Check your date source. Is it two digit year or four?
Also, there are Oracle date mask formats which might cause that. Check the default for the installation.
Agree.
If you use a Java Date format (dunno its class just now) then JDBC driver performs all what is needed to store it in a Date column.
If you use String in java and/or varchar2 in Oracle table, you are doing it wrong. This will lead to implicit conversions, NLS settings and all that pain... if you are in this bad shape then you need explicit conversions and format masks on each date usage.
Use proper types.
I'm guessing that the value you're passing in is "02/10/10".
Date functions are pretty inconsistent about what they do with 2 digit years. If you are trying to enter dates for your "timeline of ancient history", having the computer assume that 2-digit dates must be shorthand for the 21st century would be very annoying. We really are better off with a WYSIWYG date interpretation.
I do a
rs.getTimestamp("datetime")
in Java.
In the database, this datetime is 2009/03/06 02:47:18
but the time is returned as 14:47:18
I'm not very knowledgeable about MySQL dates, so I would appreciate any explanation as to why this is happening.
It doesn't matter. Its not about MySQL or any database. This is the format Timestamp shows up by default, I believe. It doesn't mean it missed the date or something.
You can always format the Timestamp returned by the method in any format in your code. Check out java.text.SimpleDateFormat class. Or for better, check out much more sophisticated Joda Time.
Two things. First, I think we need sample code. What's going on is not at all clear from what you've given us. Context, usage, DB schema, and sample rows as well.
Second, ResultSet.getTimestamp() should be returning an object of type Timestamp, rather than a String of any sort.
SimpleDateFormat time = new SimpleDateFormat("HHmmss");
datime = time.format(rs.getTimestamp("datetime"))
and then datime is printed to a file.
the datetime column in the table is a datetime Data Type