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', ...)";
Related
Hi I am trying to run this query below:
INSERT INTO Database2.Table2 ('1', getdate(), getdate(), '',ID,'','0','0','0','',ID,'','',getdate(),getdate(),'','0','"stackoverflow.com/"'+'ID','0','product','','0')
SELECT ID
FROM Database1.Table1;
I am inserting a new row with constant data mixed with data from another table, this case Database1.Table1 "ID". I constantly keep getting a select statement error when I try to run this code. Is there something I am overseeing or is this statement all wrong? Thank You
Put the constants into the SELECT list.
INSERT INTO Database2.Table2
SELECT '1', getdate(), getdate(), '',ID,'','0','0','0','',ID,'','',getdate(),getdate(),
'','0',CONCAT('"stackoverflow.com/"','ID'),'0','product','','0', ID
FROM Database1.Table1;
BTW, I strongly recommend you get out of the habit of using INSERT without listing the column names before the values. Depending on the specific order of columns in the table definition is very error-prone.
Also, MySQL doesn't use + for string concatenation by default, it uses the CONCAT() function.
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
I have a set of code that I'm coverting from Postgres to work on MySQL (PHPmyAdmin) which already has a section of inserts in the following type of format:
INSERT INTO ORD (TOTAL, SHIPDATE, ORDID, ORDERDATE, CUSTID, COMMPLAN)
VALUES ('101.4', '08-JAN-87', '610', '07-JAN-87', '101', 'A');
Since MySQL doesn't like the date as '08-JAN-87' it just ends up displaying 0000-00-00 in the date columns.
From the searches I've done so far the only solutions given seem to be converting it during the select statement so it displays correctly. I would like to know a method of changing the data itself. (not just a select statement)
This is for a uni assignment and the lecturer could only advise manually changing all the insert statments to a format it will accept. This might work for this case but in the long run or for a larger data dump this won't be possible.
You need to modify the import so that the data string is converted for MySQL
INSERT INTO ORD (TOTAL, SHIPDATE, ORDID, ORDERDATE, CUSTID, COMMPLAN)
VALUES ('101.4', STR_TO_DATE('08-JAN-87', '%d-%b-%y'), '610', STR_TO_DATE('07-JAN-87', '%d-%b-%y'), '101', 'A');
This tells mysql that the string 08-JAN-87 is a date in the format DD-MMM-YY and to convert it to a valid date datatype.
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 have a Wordpress table that has a bunch of values currently in date format MM/DD/YYYY. This query successfully shows me the Unix Timestamps for them:
SELECT UNIX_TIMESTAMP(STR_TO_DATE(`meta_value`,'%m/%d/%Y'))
FROM `wp_6222_postmeta`
WHERE `meta_key`="wpcf-start-date"
But I'm struggling to figure out how to write those values back into the field properly. I tried this query and it didn't work:
UPDATE 'wp_6222_postmeta'
SET 'meta_value' = UNIX_TIMESTAMP(STR_TO_DATE(`meta_value`,'%m/%d/%Y'))
WHERE 'meta_key' = 'wpcf-start-date'
Pasting answer here since I couldn't answer it before because my account was too new.
Ok the syntax error ended up being the addition of quotation marks for the column fields which UPDATE must not like. Removing those gave me NULL values but the query ran. I then found I had to wrap the timestamp conversion in a SELECT function. Here's the code I ran that successfully converted all the fields:
UPDATE wp_6222_postmeta
SET meta_value = (SELECT UNIX_TIMESTAMP(STR_TO_DATE(meta_value`,'%m/%d/%Y')))
WHERE meta_key='wpcf-end-date'