rails-sqlserver / tiny_tds

TinyTDS - Simple and fast FreeTDS bindings for Ruby using DB-Library.
Other
607 stars 189 forks source link

How to insert Date Type Data in a Select, Update, Delete... Statement?? #444

Closed bellothornus closed 4 years ago

bellothornus commented 4 years ago

Description

I want to insert a date type in my Select to make a query in my table, but if i write this:

results = dbh.execute("select * from IBEX352 where Date = '2019-09-27'") results.fields => ["Date", "Open", "High", "Low", "Close", "Adj_Close", "Volume"] results.each =>[{"Date"=>2019-09-27 00:00:00 +0200, "Open"=>"1", "High"=>"1", "Low"=>"1", "Close"=>"1", "Adj_Close"=>nil, "Volume"=>"1"}]

and it's okay, it gives me the values i want if i use the single quotation mark, but if i use a variable to search this...

results = dbh.execute("select * from IBEX352 where Date = #{Date}")

Which "Date" variable equals to = "2019-09-27"

results.fields =>Traceback (most recent call last): 3: from C:/Ruby25-x64/bin/irb.cmd:19:in <main>' 2: from (irb):23 1: from (irb):23:infields' TinyTds::Error (Operand type clash: datetime2 is incompatible with int)

and if i do that in SQL Server Management Studio, it gives me the same ERROR if i put the "Date" in double quotation marks, but works if i put the single quotation marks, so my question is: "Can i give my Select a variable who contains a single quotation marks? Because if i put a variable it forces to use the double quotation mark."

i dont know if i explained well the case..

bellothornus commented 4 years ago

Nevermind, i came up with an answer

if i put: require 'time' Date2=Time.parse(Date) puts " example text '#{Date2.year}-#{Date2.month}-#{Date2.day}' example text" will output this: => " example text '2019-9-27' example text " and now it works in my querys :D