Skip to content

Instantly share code, notes, and snippets.

View DavidPatShuiFong's full-sized avatar

David Fong DavidPatShuiFong

View GitHub Profile
@DavidPatShuiFong
DavidPatShuiFong / testthat_mssql_char_to_int.R
Last active January 26, 2021 00:51
Testing behaviour of character to numeric conversion in dbplyr
df <- data.frame(x = c("1.3", "2x"))
# dbplyr::test_register_src(
# "mssql",
# odbc::dbConnect(
# odbc::odbc(), Driver = "ODBC Driver 17 for SQL Server",
# Server = "<server>", UID = "<uid>", PWD = "<password>")
# )
con <- src_test("mssql")
db <- copy_to(con, df, "##test")
@DavidPatShuiFong
DavidPatShuiFong / test_mssql_char_to_int.R
Created January 26, 2021 00:16
Test MSSQL casting from (invalid) character to integer/numeric.
library(dbplyr)
library(dplyr, warn.conflicts = FALSE)
df <- data.frame(x = c("1.3", "2x"))
# both a 'float' (non-integer) and an 'invalid' number in the column
con <- DBI::dbConnect(...) # connection to a MSSQL database
db <- copy_to(con, df, "##test")
db %>% mutate(
float = TRY_CAST(x %as% sql("FLOAT")),