提问人:paul79 提问时间:6/1/2023 最后编辑:paul79 更新时间:6/2/2023 访问量:56
SQL 日期以 chr 的形式返回。我无法使用 dbplyr 和 SQL Server 强制转换为日期,也无法创建新的日期列 [已关闭]
SQL dates come back as chr. I cannot cast to date, or create new date columns using dbplyr & SQL Server [closed]
问:
在我的结果中,DATE 的 SQL 类型作为字符返回。我无法向 DATE 投射任何东西。这是一个reprex。
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "*****"
Database = "******",
user = "******",
password = "****",
Port = ****)
# Create Test table
dbSendStatement(con, "CREATE table dbo.my_table
(zipcode int, myDate date, myDateTime datetime, animal varchar(20));")
#Create Test Data
dbSendStatement(con,"insert into my_table (zipcode, myDate, myDateTime, animal)
values (90210, '2010-01-01', GETDATE(), 'cat')")
tbl(con, "my_table") %>%
mutate(castDateFail = as.Date(myDate)) %>%
mutate(newDateFail = as.Date("1969-07-29"))
zipcode myDate myDateTime animal castDateFail newDateFail
<int> <chr> <dttm> <chr> <chr> <chr>
1 90210 2010-01-01 2023-06-01 10:06:35 cat 2010-01-01 1969-07-29
- Zipcode 以 int 形式正确返回。
- myDateTime 以 dttm 的形式正确返回。
- 为什么 myDate 以 chr 的形式返回?
- 为什么 myDate 的强制转换会失败?
- 为什么 newDateFail 是 chr 而不是 date?
如果我通过管道连接到show_query(),我会得到:
SELECT *, TRY_CAST('1969-07-29' AS DATE) AS "newDateFail"
FROM (
SELECT *, TRY_CAST("myDate" AS DATE) AS "castDateFail"
FROM "my_table"
) "q01"
如果我先收集()数据,我能够按预期进行转换和变异,但是在收集之前,我需要进行日期比较并与另一个表连接。
我正在运行:
- R 版本 4.1.0
- dbplyr_2.3.2
- DBI_1.1.3
- sql 服务器 15
答: 暂无答案
评论
"SQL Server"
"ODBC Driver 18 for SQL Server"
odbc::odbcListDrivers()