I have extracted data to a temporary table in SQL Server using DBI::dbGetQuery
.
Even though, in the real query (not the play query below), I
select convert(date, date_value) as date_value
, the dates are still stored as character.
I then try to mutate the character representing the date using lubridate::ymd
, however I obtain a message saying
date_value not found
I have also tried, convert(date, date_value)
and as.Date
to no avail.
require(dplyr)
if (dbExistsTable(con, "##temp", catalog_name = "tempdb")){
dbRemoveTable(con, "##temp")
}
DBI::dbGetQuery(con, paste(
"select
convert(date, '2013-05-25') as date_value
into ##temp
"))
tbl(con, "##temp")
# Error - date_value not found
tbl(con, "##temp") %>% mutate(date_value= lubridate::ymd(date_value))
# this works
tbl(con, "##temp") %>% mutate(temp= date_value)
# this doesn't work - date value not found
tbl(con, "##temp") %>% mutate(temp= lubridate::ymd(date_value))
How can I work this field as a date?
Note: When I write the following in SQL Server, date_value shows as a date Type
select
convert(date, '2013-05-25') as date_value
into #hello
select *
from #hello
exec tempdb..sp_help #hello
in response to the comment from @Kevin Arseneau, the following image shows the results from executing a show_query()
show_query
to yourdplyr
pipes so that we can see what is going across to SQL? – Rodrigorodriguetidyverse
verbs are DBI compliant, you may need tocollect
first and thenmutate
. You could trytbl(con, "##temp") %>% collect %>% mutate(temp = lubridate::ymd(date_value))
. – Rodrigorodrigue