I want to insert into table some particular values. However, I can not add date to my table. Other values can be added easily. I used this query
INSERT INTO `student` (`bdate`) VALUES ('30.05.1992');
I need to add in this format. I tried to use DATE_FORMAT('30.05.1992','%d.%m.%y')
it also didn't help.
INSERT INTO `student` (`bdate`) VALUES (STR_TO_DATE('30.05.1992', '%d.%m.%Y'));
If your field type is date then you can insert data in yyyy-mm-dd format only, even at the time of fetching data you can convert it into your own format.
If you want to insert date in your own format then you can use varchar data type, even at the time of fetching data it will not be optimized and you can get slowness.
It should be
INSERT INTO `student` (`bdate`) VALUES (STR_TO_DATE('30.05.1992', '%d.%m.%Y'));
as year is 4 digit such that 1992,so it should be %Y
Related
Im trying to insert the current date and a specific time into a table this is my query
INSERT INTO friday_records(apprentice_name,dt,active) VALUES ('$name','CONCAT(CURDATE(), 10:00:00)',1)
and this is the output on my table
Table
The quotes:
INSERT INTO friday_records(apprentice_name,dt,active) VALUES ('$name',CONCAT(CURDATE(), ' 10:00:00'),1);
You were quoting the function. You just had to quote the time.
CSV date format is DD/MM/YYYY like this 16/11/2016. All the date become 0000-00-00 in MySQL. How to solve this differences?
As of now I can think of two solutions to you problem:
Create a additional VARCHAR field to insert those values (like 16/11/2016), and create a TRIGGER on INSERT to update the date field by converting the string date to 'YYYY-mm-dd' type.
These links may help
https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
http://www.w3resource.com/mysql/date-and-time-functions/mysql-str_to_date-function.php
In this step also, Create a additional VARCHAR field to insert those values (like 16/11/2016) and after import is done run a UPDATE query to update your date field using SET dateField = convertDate(dateFromSheet)
I have an INSERT query in ruby and I'm passing parameters from another table. One of the parameters is a timestamp value, for example: 2015-11-22 12:57:06 +0000 which is stored in a variable name created_at (of type Time)
insert into my_tbl set
name = '#{name}',
created_at = #{created_at}
and I'm always getting errors while trying to insert it.
I've tried to convert it to string, and to use str_to_date function, but the problem is that I have a timestamp value.
How can I insert the value to the table?
INSERT INTO my_tbl (name, created_at)
VALUES (name, created_at.to_s.split(' +').first)
Format the input in mysql from chat and from ruby program insert and format in this chat with the sale format
Update:
In mysql :
Select to_date('"+varchar+"','dd/mm/yyyy'......
In Ruby:
Varchar='01/12/2015"
Of course with format
How do I format the Date and Time when inserting it into my MSSQL Query?
I know this has been asked before but I am not sure why I am having so much trouble understanding how to formatting the current date and time when INSERTING it to my database.
Originally the following INSERT Query worked but now I want to add the current date and time with the minutes as the last characters. No seconds or Millaseconds
INSERT INTO Form_Submission (ID,Submission_Date,Status)
VALUES ('23', CURRENT_TIMESTAMP, 'Open');
The formatting I am looking for is: 2015-04-08 13:42
you can use LEFT and CONVERT with style 120 like this
INSERT INTO Form_Submission (ID,Submission_Date,Status)
VALUES ('23', CONVERT(DATETIME,LEFT(CONVERT(VARCHAR(40),CURRENT_TIMESTAMP,120),16)), 'Open')
For more details about different styles used with Convert refer here
If you are trying to insert into a DATETIME or SMALLDATETIME field then this will work:
CONVERT(SMALLDATETIME, GETDATE())
or CONVERT(SMALLDATETIME, CURRENT_TIMESTAMP) if you want.
So your code is:
INSERT INTO Form_Submission (ID,Submission_Date,Status)
VALUES ('23', CONVERT(SMALLDATETIME, GETDATE()), 'Open')
The point is that you are not supposed to format dates and times when inserting into the database, you should only format them on the way out (when displaying). SMALLDATETIME offers the level of granularity you seem to want (accurate to the nearest minute).
See MSDN documentation here: GETDATE and SMALLDATETIME
Maybe you can use FORMAT() T-SQL function:
INSERT INTO Form_Submission (ID,Submission_Date,Status)
VALUES ('23', FORMAT(CURRENT_TIMESTAMP, 'yyyy-MM-dd hh:mm'), 'Open')
I am trying to further a question I asked yesterday where I wanted to know how to query a date in a different format. But now I am trying to do an insert using this method (see below) however I can't get it to work. I have checked the manual but it is not beginner friendly!
INSERT INTO custorder VALUES ('Kevin','yes'), STR_TO_DATE('1-01-2012', '%d-%m-%Y');
Put the date in single quotes and move the parenthesis (after the 'yes') to the end:
INSERT INTO custorder
VALUES ('Kevin', 'yes' , STR_TO_DATE('1-01-2012', '%d-%m-%Y') ) ;
^ ^
---parenthesis removed--| and added here ------|
But you can always use dates without STR_TO_DATE() function, just use the (Y-m-d) '20120101' or '2012-01-01' format. Check the MySQL docs: Date and Time Literals
INSERT INTO custorder
VALUES ('Kevin', 'yes', '2012-01-01') ;
Looks like you've not encapsulated your string properly. Try this:
INSERT INTO custorder VALUES ('Kevin','yes'), STR_TO_DATE('1-01-2012', '%d-%m-%Y');
Alternatively, you can do the following but it is not recommended. Make sure that you use STR_TO-DATE it is because when you are developing web applications you have to explicitly convert String to Date which is annoying. Use first One.
INSERT INTO custorder VALUES ('Kevin','yes'), '2012-01-01';
I'm not confident that the above SQL is valid, however, and you may want to move the date part into the brackets. If you can provide the exact error you're getting, I might be able to more directly help with the issue.
The date format for mysql insert query is YYYY-MM-DD
example:
INSERT INTO table_name (date_column) VALUE ('YYYY-MM-DD');
An add-on to the previous answers since I came across this concern:
If you really want to insert something like 24-May-2005 to your DATE column, you could do something like this:
INSERT INTO someTable(Empid,Date_Joined)
VALUES
('S710',STR_TO_DATE('24-May-2005', '%d-%M-%Y'));
In the above query please note that if it's May(ie: the month in letters) the format should be %M.
NOTE: I tried this with the latest MySQL version 8.0 and it works!
When using a string-typed variable in PHP containing a date, the variable must be enclosed in single quotes:
$NEW_DATE = '1997-07-15';
$sql = "INSERT INTO tbl (NEW_DATE, ...) VALUES ('$NEW_DATE', ...)";