Python SQL Date insert issue - sql

I am trying to insert a date inside the orderDate column in my sql database, which is of datatype date
date = (start_date.strftime("%Y-%m-%d"))
testData.cur.execute("INSERT INTO customerOrders (orderDate) VALUES %s", (date,))
testData.cn.commit()
Here is the value of the variable 'data':
2019-01-01
Here is the data type of the date:
class 'str'
I am subject to the error of:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ''2019-01-01''
at line 1

Add simple quotes in %s. MySQL needs to know that '2019-01-01' is a date type input.
INSERT INTO customerOrders (orderDate) VALUES '%s'

Consider parameterization of exact date time variable and avoid string formatting. Also, in SQL, VALUES clause requires parentheses enclosure around numbers or literals:
testData.cur.execute("INSERT INTO customerOrders (orderDate) VALUES (%s)",
(start_date,))
testData.cn.commit()

Related

Why can't I insert yyyymmdd (stored as text) to a date column

This has been asked many times before and I tried quite a bit before posting this question. I have 2 columns on my SQL Server table where the dates are stored as CHAR(8) yyyymmdd format. When I try to insert this to another table with date column, I get:
Conversion failed when converting date and/or time from character string.
The typecasting that I used is:
,CAST(convert(char(10),qrda.BillFromDate,120) AS DATE) AS [BillStartDate]
,CAST(convert(char(10),qrda.BillToDate,120) AS DATE) AS [BillEndDate]
With the above code, I am trying to make it SQL Server readable date format and then typecasting it to DATE so that I can insert it to the destination table without any other transformations. Not sure where I am going wrong with this.
Since date are stored in the following format yyyyMMdd you can insert these values without any need to use CONVERT or CAST functions, since this format can be implicitly converted to DATE.
If the data contains invalid values such as 00000000, you can use TRY_CONVERT or TRY_PARSE function to convert these values to NULL
Example:
CREATE TABLE #TBLTEMP(datecolumn DATETIME)
INSERT INTO #TBLTEMP(datecolumn)
VALUES ('20180101')
INSERT INTO #TBLTEMP(datecolumn)
VALUES (TRY_PARSE('00000000' as DATETIME))
SELECT * FROM #TBLTEMP
Result:
From the example above, you can see that 20180101 was inserted succesfly without any casting, while TRY_PARSE function converted the invalid value 00000000 to NULL.
You can use the following syntax:
INSERT INTO TargetTable(DateColumn)
SELECT TRY_PARSE([CharColumn] as DATETIME
FROM SourceTable
References
Understanding SQL Server’s TRY_PARSE and TRY_CONVERT functions
TRY_PARSE (Transact-SQL)
Sorry for the trouble folks, but looks like this is bad production data where some values are literally '00000000'. I dont know how this flows into our system, but since the source table columns are CHAR(8), this is a valid value. Not so much for me.

Insert Date and String on table

I have a table with the following properties:
U. TRADE_ID VARCHAR2
TRADE_DATE DATE
PRICE NUMBER(10.2)
Once I get the data from a file, the colums are as follow:
PSD231,03051982,50.1
My question is, are those the correct datatypes for the table? I'm getting the input from the file for the first sql column as a String. But I can't display it. I think I'm missing something in the sql part. How can I create a new sql row filled with data so I can test it? This is what I tried and works:
insert into myTable(TRADE_ID, TRADE_DATE, PRICE)
values('PSD231', date '2011-01-01', 10.4);
but I'm trying create a query with the date in the format:
01-01-2011, not like the above
"What if I wanted to insert the values like this: 10062019 in the format ddmmyyyy instead of how I have it in the query: 2011-01-01 "
Just put single quotes around the date, '10062019', and make sure it is in the correct date format for your region (ddmmyyyy in your case).
insert into myTable (TRADE_ID, TRADE_DATE, PRICE)
values ('PSD231', '20110101', 10.4);
To change date formats, use the CONVERT function e.g
SELECT CONVERT(varchar, getdate(), 1)
There is a nice list for the convert function here
Since you have defined the first field as a varchar type, it should be more like,
insert into myTable(TRADE_ID, TRADE_DATE, PRICE)
values('PSD231','2011-01-01', '10');
Values should be enclosed into single quotes when they are being inserted.

ORA-00917: Missing Comma SQL Oracle error

I am using Oracle SQL and have just altered a table and added a column named Date_Employed
ALTER TABLE Employees ADD Date_Employed date;
My problem is that whenever I try to insert a value into the Date_Employed column i get an error ORA-00917: Missing comma when I type in the code below
INSERT INTO Employees(Date_Employed) VALUES (26 September 2001);
I would like to know whether the method I am using to try to input data into the column is correct? and if it is not, what is the correct way to insert date data into the column?
Also, I would like to know, why I am getting the error I described?
Use a proper date format for Oracle and enclose it in single quotes:
INSERT INTO Employees(Date_Employed)
VALUES (DATE '2001-09-26');
INSERT INTO
Employees (Date_Employed)
VALUES
(TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'));
You are getting error because oracle is not able to understand the date format you have given.

How to Insert table column values: month & day from column datetype

I'm using Oracle 11g Schema
I want to select the month and day from a date concatenate it and put it into a column.
The syntax I have here makes sense but its throwing an error of..
INSERT INTO OB_SELECT_LST12_SPG WED
VALUES (((TO_CHAR(TO_DATE('RET_DATE', MM))||( TO_CHAR(TO_DATE('RET_DATE', DD)));
"SQL Error: ORA-00907: missing right parenthesis"
Any help is greatly appreciated.
Thanks.
Some points first...
Your table name has a space in in
Your not enough columns error is caused by you having more than one column in the table
You really shouldn't be doing this at all
If ret_date is a column don't encapsulate it in quotation marks (') as you won't be able to convert the string 'ret_date' into a date.
However, assuming ret_date is a string that looks like 13-06-2012
insert into ob_select_lst12_spg_wed (my_column)
values(to_char(to_date(:ret_date,'dd-mm-yyyy'),'mmdd'));
If ret_date is a date data-type then you can remove the inner conversion.
insert into ob_select_lst12_spg_wed (my_column)
values(to_char(:ret_date,'mmdd'));
to_char and to_date both make use of datetime format models, which have a number of options.
Please don't do this though. Always store dates as dates
From your comments ret_date is a date and the column wed is a character. You're getting the error bind variable not declared because you haven't specified the variable. I'm going to assume that ret_date is in another table as it's a date, in which case lose the values key-word and insert directly from that table:
insert into ob_select_lst12_spg (wed)
select to_char(ret_date,'mmdd')
from the_other_table
This shouldn't be required though, you can always convert a date into whatever you want on exit from the database. If you don't store it as a date in the database then it's easy for errors and incorrect values to creep in. I personally would change the column wed to a date, in which case your query becomes the very simple:
insert into ob_select_list12_spg (wed)
select ret_date
from the_other_table
You can then use the correct datetime format model for your needs when selecting from the database.
Sample query to get date, month from date column:
Select ((To_Char(to_date('01-FEB-73','dd-mon-yy'), 'MM'))
||( To_Char(to_date('01-JAN-73','dd-mon-yy'), 'dd')))
from dual;
if RET_DATE is date datatype then use
insert into table_name (columname)
Select ((To_Char(RET_DATE, 'MM'))
||( To_Char(RET_DATE, 'dd')))
from dual;
You are missing extra parentheses. Number of opened ( was 8 and closed ) was 5
INSERT INTO OB_SELECT_LST12_SPG(WED)
VALUES ((TO_CHAR(TO_DATE(RET_DATE, MM)))||( TO_CHAR(TO_DATE(RET_DATE, DD))));
Update
Make sure other columns are not required (i.e. Allow NULLs)

Date time string insert to db

Hi I am trying to insert the following to SQL Server 2005:
INSERT INTO tb_UserLoginTimes (UserID, LoginDateTime)
VALUES (1235,2010/07/06 10:38:44)
But am getting the following error.
Incorrect syntax near '10'.
Do I need to escape the colon?
If so how do I do that?
Real noob at this so my apologies.
It looks like you simply need to enclose your date in quotes:
... VALUES (1235, '2010/07/06 10:38:44')
Try it out in the SEDE. If you leave out the quotes, you'll get the same syntax error you're reporting.
Instead of 2010/07/06 10:38:44 use the ANSI SQL Standard (YYYYMMDD HH:MM:SS)
'20100706 10:38:44'