Retrieving date from mysql - java

I have mysql datatype as date, my date format in database is 20/1/2015. Now I wish to retrieve data using date.
For example:
SELECT * FROM Device_Count where Date='20/1/2015'
GO
I am getting error like:
Incorrect date value: '20/1/2015' for column 'Date' at row 1
Please find and post the status

MySQL's date format is yyyy-mm-dd
change your query as below and it should work, provided your Date column data type is date.
SELECT * FROM Device_Count where Date=STR_TO_DATE('20/1/2015', '%d/%m/%Y' )
Refer to Documentation:
STR_TO_DATE(str,format)

Check your date format and compare it by the same format

Date is stored in MYSQL in the format of YYYY-mm-dd. Try this query:
SELECT * FROM Device_Count where Date='2015-01-20'
GO

Related

how to get the date in the format that is displayed in sql table?

I have an oracle database table with a date column. when I query the table date is displayed in dd-MMM-yy format. For example 17-OCT-19 format. But when I query the table from java and print the value I am seeing the date in yyyy-MM-dd format. Is there a way where I can fetch the date the same as that is displayed in dd-MMM-yy format? I have no idea why the date is getting displayed in a different format in java.
DateFormat activityDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date activityDate = activityDateFormat.parse();
activityDateFormat = new SimpleDateFormat("dd-MMM-yy");
finalActivityDate = activityDateFormat.format(activityDate);
I am using the piece of code to convert the date from yyyy-MM-dd to dd-MMM-yy format. The NLS settings for Date format in the database are DD-MON-RR. Is there any reason why I don't get the date the same as DD-MON-RR? can someone please help me?
Select the date column as string and format it dd-mmm-yy from the SQL query.
The client you are using to query Oracle, SQL Developer is often used, is the one formatting the date.
Java gets the date as a date data type and will use whatever is the default format when converting it to a string

MySQL updating strings in a column to be DateTime

I am saving a date formatted with Javas SimpleDateFormat with the format "E. MMM-dd-YYYY hh:mm a zzz" eg "Sat. Apr-15-2017 10:44 PM EST" to a table as a VARCHAR.
How would I go about changing all of the pre-existing data to be in DateTime format?
Have you tried this?
UPDATE `table`
SET `column` = STR_TO_DATE(`column`,'%Y-%m-%d')
refer to change mysql comumn from varchar to datetime and convert data post

insert java date time into Oracle

Oracle date format on server is MM/DD/YYYY HH24:Mi:SS
I would like to insert a variable which contains a date with timestamp into Oracle date column.
I am getting error while inserting date into Oracle "date column ends before format picture ends".
All I want is append specific timestamp to java string date and insert that string/date format into Oracle database
example:
String incoming_date = request.getParameter("insert_date"); //this comes as a string in dd-mon-yyyy format
formatted_incoming_date = incoming_date + " 00:00:01"; //I want to append time factor to above variable with 00:00:01
insert into testtable values(formatted_incoming_date);
Try this
insert into testtable values(TO_DATE (formatted_incoming_date, 'dd-mon-yyyy hh24:mi:ss);
Why you try to insert date as a string? It seems like there is an implicit conversion from string to date in Oracle. Can java.sql.Date be used instead?
Anyway, as long as date comes in format dd-mon-yyyy you have to convert it either into java.sql.Date object or in proper for Oracle string representation as MM/DD/YYYY HH24:Mi:SS i.e.
incoming date "05-12-2016" string for Oracle "12/05/2016 00:00:00"

How to select by date only if field is a timestamp?

I have a database field timestamp without timezone, that has values like 2015-11-23 14:42:55.278.
Now I want to find database records with just using the date part 2015-11-13.
Is that possible?
Ideally using hibernate and spring.
I'm not sure if is the best way in performace terms, but you may search dates between 2015-11-23 00:00:00.000 and 2015-11-23 23:59:59.999
If you want to fetch only for day 2015-11-13 then you can fetch all records using between keyword and by using timestamp of start of day.
dateField between 2015-11-13:<time_of_beginning_of_day> AND 2015-11-14:<time_of_beginning_of_next_day>
or
dateField between 2015-11-13:<time_of_beginning_of_day> AND 2015-11-13:<time_of_end_of_day>
You can cast the column to a date, e.g:
Postgres specific:
the_timestamp_column::date = date '2015-11-13'
or (standard SQL)
cast(the_timestamp_column as date) = date '2015-11-13'
You can also "reduce" the timestamp to different levels using date_trunc()
http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC

Incorrect Date Exception

I am trying to enter a date with year 1900 using java into sql, but I am getting exception as :
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1900-01-01 00:00:00'
If you are using TIMESTAMP to store the dates the minimal value is '1970-01-01 00:00:01'.
Use DATE to store older dates or DATETIME to store older dates with time.
Check the MySQL documentation about the date format http://dev.mysql.com/doc/refman/5.1/en/datetime.html

Categories