Can't add Date in Db - mysql

I'm trying to add Dates into a database using MySqlCommand, but the date isn't added correctly. Let me explain. I get the the actual Date like this:
Dim book_datetime = Date.Now.ToString("yyyy-MM-dd HH:mm:ss")
As you can see I parse the Date in this format: yyyy-MM-dd HH:mm:ss
and it's all working well, but when I perform the insert in my db I get this:
2015-12-15
instead of this
2015-12-15 11:48:30
This is my code that peform the insert:
query = "INSERT INTO setting (book_datetime)
VALUES(#book_datetimep)"
MySqlCommand = New MySqlCommand(query, dbCon)
MySqlCommand.Parameters.AddWithValue("#book_datetimep", book_datetime)
My Database book_datetime contains a Date field. What is wrong? The book_datetime is filled correctly after I parse it but when it gets inserted in the DB I get another format. I saw that if I change the field from Date to Text I get the correct format, but why?

From the MySQL documentation here:
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
The DATETIME type is used for values that contain both date and time
parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD
HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to
'9999-12-31 23:59:59'.
Use DATETIME as your field type instead of DATE and the time portion of your date won't be truncated on insert.

Related

how to change date format in mysql

The default date format in my SQL is : '2019-05-06'
and its fine but when i insert date to my table i want this format 2019-5-6 not the above format
It means month and day must be start 1 to 30 not 01 to 31.Is there any way to change default format in my sql?
You seem to be looking for the MySQL STR_TO_DATE function :
It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts.
So if the date coming out of your application is like '2019-5-6', to convert it to a MySQL date you need :
STR_TO_DATE('2019-5-6', '%Y-%c-%e')
In an INSERT statement :
INSERT INTO mytable VALUES(1, STR_TO_DATE('2019-5-6', '%Y-%c-%e'));
Tip :
%Y : Year as a numeric, 4-digit value
%c : numeric month name (0 to 12)
%e: day of the month as a numeric value (0 to 31)
The default way to store a date in a MySQL database is by using DATE. The proper format of a DATE is: YYYY-MM-DD. If you try to enter a date in a format other than the Year-Month-Day format, it might work but it won't be storing the dates as you expect.
In order to run a MySQL Insert command and add the current date into your table you can use MySQL's built-in function CURDATE() in your query.
An example of how to Insert a Date in MySQL using CURDATE
$query_auto = "INSERT INTO tablename (col_name, col_date) VALUE ('DATE: Auto CURDATE()', CURDATE() )";
Also, you can run a query to set the date manually
An example of how to Insert a Date in MySQL manually
$query_manual = "INSERT INTO tablename (col_name, col_date) VALUES ('DATE: Manual Date', '2008-7-04')";
It is recommended to do the date formatting when doing a query, like so:
SELECT DATE_FORMAT(BirthDate, "%W %M %e %Y") FROM Employees;
You can find more examples of formatting the date here:
https://www.w3schools.com/sql/func_mysql_date_format.asp

Cannot scan the right values of Order date in sql

I want to fetch all datas that corresponds in the chosen date range.
So the problem is that. When theres included time in the data. It can't fetch the required data to be displayed. But when I remove the time on it. It displays really well. What can I do to make it right?
EXAMPLE VALUES:
2018-10-29 01:21:29pm
2018-10-30 01:21:29pm
EXAMPLE VALUES THAT WORKS:
2018-10-29
2018-10-30
My query:
`"SELECT *,SUBSTRING(order_date,1,10) from orders where order_date >='$fromdate' AND order_date <='$todate'"`
Ideal Solution: You will need to change the datatype of order_date from Varchar(500) to Datetime type, using Alter Table command.
Now, it is noteworthy that the MySQL datetime value is in YYYY-MM-DD HH:MM:SS format. So firstly, you will need to change your datetime string to MySQL datetime format string. Otherwise, directly changing the datatype will lead to irreparable loss/truncation of data.
Your datetime value 2018-10-29 01:21:29pm is basically of YYYY-MM-DD HH:MM:SS AM/PM (12 hour format). In terms of format specifiers, it would be: '%Y-%m-%d %h:%i:%s%p'. Complete list of available format specifiers can be seen in MySQL docs.
Firstly, we use Str_To_Date() function to convert all your data into proper Datetime format.
UPDATE orders
SET order_date = STR_TO_DATE(order_date, '%Y-%m-%d %h:%i:%s%p');
Now, next step is simple. Just modify the datatype to datetime:
ALTER TABLE orders
MODIFY COLUMN order_date datetime;

How do I load a MySQL timestamp field into a specific format from a flat file?

I have some data in a flat file that includes a timestamp field that looks like 1900-01-01 00:00:00. I've been trying to load it into a MySQL table but MySQL is taking it and setting it as 0000-00-00 00:00:00 which is causing issues for me. I've tried running some update commands on the field, but it stays as 0000-00-00 00:00:00.
I know of the from_unixtime function, but how would I use that here? This is the command I'm running to load the data:
load data local infile 'file.txt' into table tgt_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\'' (tbl_name, db_name, timestamp_field, other_col);
The field in question here is timestamp_field.
I've managed to use from_unixtime like update tgt_table set timestamp_field=FROM_UNIXTIME('1900-01-01 00:00:00') where tbl_name = 'test'; but timestamp_field ended up looking like: 1969-12-31 18:31:40
Would appreciate any help on how to format either the column or the SQL to get this inserted correctly
Looks like it might be a column type issue.
From the MySQL 5.6 documentation on time/date fields:
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
You used the 1900-01-01 in your example which is outside the allowable date range for timestamp. Instead, try using a DATETIME field which has a larger allowable range.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

Why doesn't my DateTime search work?

Sorry about the generic title, I didn't know how to phrase this.
I have a DateTime in my MySQL DB. For example: 14/06/2016 15:01:00
When I try to do a query to find dates which equal certain dates it won't find anything, unless I used the Americanised date format. Yet it's stored in the English way.
Eg:
Select * FROM tbl WHERE Date = '14/06/2016' - Doesn't return any results
But Select * FROM tbl WHERE Date = '2016/06/14' does return results.
Why is this? And how can I swap it around?
http://dev.mysql.com/doc/refman/5.7/en/datetime.html
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
If you're seeing a different format, your SQL client is "helpfully" changing the output from the underlying data storage.

Store Date as mm-dd-yyyy in mysql

I am trying to store the date in mysql as mm-dd-yyyy.
The following query updates the table stores the date as 0000-00-00
UPDATE `h3`.`newbatch` SET `DateCreated` = '11-08-2013' WHERE
`newbatch`.`BatchID` =
1 AND `newbatch`.`DateCreated` = '2013-11-08' LIMIT 1
I can always use DATE_FORMAT(DateCreated,'%m %d %Y') during select but is there a way to store date in that format.
The datatype of DateCreated is Date.
I am using MySQL.
Thanks
Do not modify the storage format of a date. The format for the date data type is ISO 8601 standard for a reason. You will lose the ability to perform most date functions elegantly (without first converting to the standard date format). You do the formatting when you run a query.