Use SQL date column as variable in R

# Loading the libraries
library(odbc)
library(dbplyr)
library(dplyr)
library(stringr)

# Authenticate your session
.userpwd = sprintf("", .pw <- .rs.askForPassword("Please enter your windows password"))

system(sprintf("" | kinit %s", .pw <- .rs.askForPassword("Please enter your windows password"), ""))

# Create connection
con <- dbConnect(odbc::odbc(),
                 Driver="impala",
                 Host = "",
                 Port = ,
                 AuthMech = 1, KrbHostFQDN="",
                 KrbRealm ="", KrbServiceName="",
                 TrustedCerts="",
                 SSL = )

# Creation of Query

Weekly_Overall_Detail <- dbGetQuery(con, str_interp("with man as

( 
select 
policy_number
,vin
,veh_fixed_id 
,term_num
,decision_reason
,row_number() OVER (partition by  policy_number,vin,veh_fixed_id,term_num ORDER BY processed_date desc) AS order_id
from  edhprod_data_lake.rsp_man_bulk_risk_hive_tb
)

select    
    r.policy_number as PolicyNumber
   ,r.vehicle_seq_number as VehicleSeqNumber
   ,r.vin as Vin
   ,r.initial_transfer_code as InitialTransferCode
   ,to_date(r.rsp_transfer_date) as RspTransferDate
   ,to_date(r.period_start) as PeriodStart
   ,to_date(r.policy_expiry_date) as PolicyExpiryDate
   ,premium_ceded as PremiumCeded
   ,r.risk_ft_premium as RiskFTPremium
   ,r.province_code as Province
   ,r.grid_rated_ind as GridRatedInd
   ,r.company_code as Company
   ,r.decision_source as DecisionSource
   ,m.decision_reason as DecisionReason
   ,r.pool_decision as PoolDecision
   ,r.car_year as CarYear
   ,r.processed_date as ProcessedDate
   ,r.scheduler_date as SchedulerDate
  from edhprod_edw.rsp_pool_impala_tb r   
   left join man m 
   on r.policy_number=m.policy_number
   and r.vin=m.vin
   and r.vehicle_fixed_id=m.veh_fixed_id and
   r.term_number=m.term_num
where 
upper(r.pool_decision) in ('KEEP','CEDE')  and m.order_id=1

and to_date(r.rsp_transfer_date) <=to_date(r.scheduler_date)
and to_date(r.scheduler_date) < to_date(r.policy_expiry_date)
and r.scheduler_date=(select max(scheduler_date) from edhprod_edw.rsp_pool_impala_tb
where year(scheduler_date)=year(to_date(now())) and dayofweek(scheduler_date)=6)
"))

install.packages("lubridate")
library(lubridate)
**Weekly_Extract <- ymd(max(Weekly_Overall_Detail$scheduler_date))**
#Problem in the line above. Is there a way to take the date column of the SQL query and use it as a variable in #R?  
#Warning messages:
#1: In max(Weekly_Overall_Detail$scheduler_date) :
#  no non-missing arguments to max; returning -Inf
#2: All formats failed to parse. No formats found. 
# Writing  the file to R

library(stringr)
library(csv)

Excel_File <- function(x, Weekly_Extract){
  write.csv(x, file = str_interp("Weekly_Overall_Detail_${Weekly_Extract}.csv"), row.names = FALSE)
}

Excel_File(Weekly_Overall_Detail,Weekly_Extract)

#Saving the file to local
library(RCurl)

ftpUpload(str_interp("./Weekly_Overall_Detail_${Weekly_Extract}.csv"),
          str_interp(sprintf("File path is entered here/Weekly_Overall_Detail_${Weekly_Extract}.csv", .pw <- .rs.askForPassword("Please enter your windows password")))
          
          
)

It is hard to say what the problem is without seeing the data. Please run the following code and post the output of the dput() call here.

SampleData <- Weekly_Overall_Detail[1:10, c("PolicyNumber", "SchedulerDate")]
dput(SampleData)

Put a line with three back ticks just before and after the output you post, lie this
```
dput() output here
```

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.