Created
January 26, 2021 00:16
-
-
Save DavidPatShuiFong/7b47a9804a497b605e477f1bf6c38b37 to your computer and use it in GitHub Desktop.
Test MSSQL casting from (invalid) character to integer/numeric.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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")), | |
| integer = TRY_CAST(x %as% sql("INTEGER")), | |
| bigint = TRY_CAST(x %as% sql("BIGINT")), | |
| numeric = TRY_CAST(x %as% sql("NUMERIC")), | |
| doublecast_integer = TRY_CAST(TRY_CAST(x %as% sql("NUMERIC")) %as% sql("INTEGER")), | |
| doublecast_bigint = TRY_CAST(TRY_CAST(x %as% sql("NUMERIC(38,0)")) %as% sql("BIGINT")) | |
| ) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rversion 3.5.3 on Windows 10. Microsoft SQL Server 12.00.2000 (2014), on Windows 10.Results of above gist: