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')
Related
I have a table with date values stored as strings, like '2012-01-15'. Some of them are invalid, like '2012-04-31'. I would like to insert the valid dates into a DATE type column in another table, and default the day to 1 if it is too large for the month.
DAYNAME seems to be the only function in MySQL that will check whether a date is valid. However, it issues a warning for an invalid date (in addition to returning NULL), which upgrades to an error in an INSERT or UPDATE statement.
So I'd like to do something like
INSERT INTO date_tbl (date_value)
SELECT IF(DAYNAME(date_string) IS NOT NULL, date_string, CONCAT(LEFT(date_string, 8), '1')
FROM date_string_table;
This fails with Data truncation: Incorrect datetime value: '2010-04-31' even though I am not actually inserting invalid data.
The problem with using INSERT IGNORE is running the risk of actually inserting invalid data, which I would really like to avoid.
EDIT Oct 5:
This problem can be reproduced without creating the intermediate table simply as
CREATE TABLE date_tbl (
date_val DATETIME
);
INSERT INTO date_tbl (date_val)
SELECT IF(DAYNAME('2012-04-31') IS NOT NULL, '2012-04-31', NULL);
I would like the above INSERT to insert NULL for that invalid date, instead of failing.
You can compare days of the proper date and last day for that month with LAST_DAY and STR_TO_DATE.
So your query would be:
INSERT INTO date_tbl (date_val)
SELECT IF(DAY(STR_TO_DATE('2012-02-30','%Y-%m-%d')) > DAY(LAST_DAY(STR_TO_DATE('2012-02-30','%Y-%m-%d'))), NULL,'2012-02-30');
DB Fiddle
A workaround is to use INSERT IGNORE and then validate after the fact:
SELECT date_value
FROM date_tbl
WHERE DAYNAME(date_value) IS NULL;
Should return zero rows.
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
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.
Below is the insert command i am using, and when i run the command it gives OpenDate cannot be null error, but i'm not sure why. If I switch around the Y-m-d it says invalid format. I really dont know why i'm getting this error. The column is DateTime and the DB is InnoDB
INSERT INTO tbldealershipcars(Price,
OpenDate,
`Year`,
Make,
Model,
CarTrim,
Miles,
DeID,
VIN)
VALUES
('16888',
STR_TO_DATE('8/12/2011', '%m-%d-%Y'),
'2007',
'87',
'701',
'12299',
'73387',
'Demo',
'MyTestID')
Your date has /s and your format has -s. They need to match.
The column is DateTime
Needs to be Date if you are only inserting Y-m-d
Or you'll need to change your str_to_date function to include a time.
You can try
STR_TO_DATE('8/12/2011', '%m/%d/%Y')
or
STR_TO_DATE('8-12-2011', '%m-%d-%Y')
STR_TO_DATE(str,format) with all Specifiers.
Date and Time Functions from dev.mysql.com
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', ...)";