dBWriteTable cannot write NULLs into SQL Server table for date fields - r

I cannot insert NULL (NA) values into an SQL Server Date column field using dbWriteTable.
dbWriteTable(con, "BIOutput", data[Cdate == date,], append = TRUE);
I get the error:
Error in result_insert_dataframe(rs#ptr, values) :
nanodbc/nanodbc.cpp:1587: 22007: [Microsoft][ODBC SQL Server Driver]
[SQL Server]Conversion failed when converting date and/or time from
character string.
I understand this works for numeric fields, but why doesn't it work for Dates? What workaround can I use?

Related

Fetching Char(1) column with Informatica Export mode returning error

I am using TPT FastExport in Informatica source. In the source Teradata table, there is a column with datatype char(1). When I run the workflow, I am getting an error as Source column data length is 3 but the Target column data length is 6.
I am not facing issues with Varchar or numeric columns.

pyodbc bulk Insert

With SQL Server ODBC Driver 17, Object nulls is not an issue. But it gives error below on dates and integers
Invalid character value for cast specification (0) (SQLExecute)')
Process works fine when cursor.fast_executemany = False, or when default values (0, 1900-01-01) set for the null dates and integers.
Is there a way to bulk insert keeping the Nulls in place?

Insert multiple rows - SQL Error: near ",": syntax error

In my SQLite I want to insert 2 rows.
INSERT INTO notification_invoice (amount,currencyISOCode) VALUES
(1.1,'498'),
(34.2,'980');
But I get error:
30-Nov-18 12:21:21: SQL Error: near ",": syntax error INSERT INTO
notification_invoice (amount,currencyISOCode) VALUES (1.1,'498'),
(34.2,'980');
>

Querying mixed case columns in SQL with R

I have a mixed case column in my_table that can only be queried using double quotes in psql. For example:
select "mixedCase" from my_table limit 5; would be the correct way to write the query in psql, and this returns records successfully
However, I am unable to replicate this query in R:
I have tried the following:
dbGetQuery(con, "SELECT '\"mixedCase\"' from my_table limit 5;")
which throws: RS-DBI driver warning: (unrecognized PostgreSQL field type unknown (id:705) in column 0)
dbGetQuery(con, "SELECT 'mixedCase' from my_table limit 5;")
which throws: RS-DBI driver warning: (unrecognized PostgreSQL field type unknown (id:705) in column 0)
dbGetQuery(con, "SELECT "mixedCase" from my_table limit 5;")
which throws Error: unexpected symbol in "dbGetQuery(con, "SELECT "mixedCase"
What is the solution for mixed case columns with the RPostgreSQL package?
You seem to understand the problem, yet you never actually tried just using the literal correct query in R. Just escape the double quotes in the query string and it should work:
dbGetQuery(con, "SELECT \"mixedCase\" from my_table limit 5;")
Your first two attempts would have failed because you are passing in mixedCase as a string literal, not as a column name. And the third attempt would fail on the R side because you are passing in a broken string/code.

RS-DBI driver warning: (unrecognized MySQL field type 7 in column 1 imported as character)

I'm trying to run a simple query that works with MySQL or other MySQL connector API's,
SELECT * FROM `table` WHERE type = 'farmer'
I've tried various methods using the RMySQL package and they all get the same error
RS-DBI driver warning: (unrecognized MySQL field type 7 in column 1 imported as character)
Type = 'farmer'
(Query<-paste0("SELECT * FROM `table` WHERE type = '%",Type,"%'"))
res<-dbGetQuery(con, Query)
Query<-paste("SELECT * FROM `table` WHERE type = \'farmer\'")
Query<-paste("SELECT * FROM `table` WHERE type = 'farmer'")
What am I doing wrong?
"type" is a keyword in MYSQL. Surround the it with backticks to escape field names.
SELECT * FROM `table` WHERE `type` = 'farmer'
Also you probably have a time stamp column in your table. R is known to not recognize that column type. Convert it to a unix time stamp in the portion of the SQL statement.
Looks like the db schema has something in column which is of type 7 -- and that type appears to be unknown to the RMySQL driver.
I try to exclude column one in the query, or cast it at the select * ... level eg via something like
select foo as character, bar, bim, bom from 'table' where ...
To be clear, when I encountered this error message, it was because my data field was a time stamp.
I verified this by changing my query to SELECT created_at FROM ... which caused the error. I also verified this by changing the query not to include the column names that were timestamps, then I had no errors.
Note too, that the error message counts columns starting from 0 (instead of 1 as R does)
IMHO, the answer is you aren't doing anything wrong, but it's something that needs to be fixed in RMySQL.
The workaround is after you read in your data, you need to call one of the several possible character to datetime conversion functions. (Which one depends on what you want to do with the time stamp exactly.)

Resources