r shiny date range input to sql query

后端 未结 1 376
滥情空心
滥情空心 2020-12-10 22:03

I have a sql query below that works just fine.

 Select * from dbo.Employee where StartDate between \'08/01/2014\' and \'08/31/2014\' order by StartDate 
         


        
1条回答
  •  醉梦人生
    2020-12-10 22:26

    I use sub function to do that, by simply using regular expressions like so:

    my_date1 <- "08/01/2014"
    my_date2 <- "08/31/2014"
    
    my_query <- 'Select * from dbo.Employee where StartDate between DATE1 and DATE2 order by StartDate'
    my_query <- sub("DATE1",my_date1,my_query);my_query <- sub("DATE2",my_date2,my_query)
    # the result of your query is below
    noquote(my_query)
    
    # if you want the quotes for dates leave them there
    my_query <- 'Select * from dbo.Employee where StartDate between "DATE1" and "DATE2" order by StartDate'
    my_query <- sub("DATE1",my_date1,my_query);my_query <- sub("DATE2",my_date2,my_query)
    # the result of your query is below
    noquote(my_query)
    
    
    # Now sub the inputs into those variables like so
    my_query <- 'Select * from dbo.Employee where StartDate between DATE1 and DATE2 order by StartDate'
    my_query <- sub("DATE1",input$daterange[1],my_query);my_query <- sub("DATE2",input$daterange[2],my_query)
    noquote(my_query)
    

    0 讨论(0)
提交回复
热议问题