In the table tblmap, I have columns mapelec, mappaper and mapplan. In these columns I have the words TRUE or FALSE. I am new to MySQL and have these T-SQL statements I am trying to convert to MYSQL statements:
IIf([tblmap].[mapelec],'Digital Maps'
IIf([tblmap].[mappaper],'Paper Maps','Other Maps')) AS MapType
To convert to MySQL I am guessing this is what it means:
CASE WHEN tblmap.mapelec = 'TRUE' THEN 'Digital Maps' WHEN tblmap.mappaper = 'TRUE' THEN 'Paper Maps' ELSE 'Other Maps' END AS MapType
Next:
IIf(([tblMap]![MapPlan]=True),'Plan View','') AS MapPlan
CASE WHEN tblmap.mapelec = 'FALSE' THEN 'Plan View' END AS MapPlan
Then this
IIf([tblmap].[mapelec],1)
IIf([tblmap].[mappaper],2,3)
CASE WHEN tblmap.mapelec = 'TRUE' THEN '1' END,
CASE WHEN tblmap.mappaper = 'TRUE' THEN '2' ELSE '3' END
I am making the assumption that [tblmap].[mapelec] is some kind of shorthand and means if the data in tblmap.mapelec evaluates as TRUE then do something. Can someone tell me if I am converting these correctly?
I was able to figure this out using my assumptions in my post.
IIf([tblmap].[mapelec],'Digital Maps'
IIf([tblmap].[mappaper],'Paper Maps','Other Maps')) AS MapType
Translated to
CASE
WHEN tblmap.mapelec = 'TRUE' THEN 'Digital Maps'
WHEN tblmap.mappaper = 'TRUE' THEN 'Paper Maps'
ELSE 'Other Maps'
END AS MapType
I made all my IIF statements follow this format and I eventually returned the same results as the original T-SQL statements.
Related
I've been requested to migrate a project made with VBA in an Access DB to VB.Net with SQL Server 2008 R2. I've read in other threads that the function IIF does not come as a built-in function in such SQL Server version, creating it won't help it too, because the '=' operator can't be used to express a boolean in the first argument, and the problem using CASE is that some IIF's comes with more than one expression to evaluate inside.
In short words, this produces an error, "the = operator is not defined"
Iif(Value1 = Value2, 'Some return', 'Another return')
And an IIF expression like the following can't be translated to a CASE expression.
Iif((Value1 = Value2) And (Value3 = Value4), 'Value to return if true', 'Value to return if false')
I'm currently creating stored procedures and taking advantage of programming structures to overcome this problem, but it seems an inefficient workaround since there are lots of nested IIF's.
My question is, is there a more efficient way to do it?, or should i stick doing it with stored procedures?
Thanks in advance.
Both of your IIF calls can be converted to CASE expressions:
IIF(Value1 = Value2, 'Some return', 'Another return')
IIF((Value1 = Value2) AND (Value3 = Value4), 'Value to return if true', 'Value to return if false')
can be rewritten as
CASE WHEN Value1 = Value2 THEN 'Some return' ELSE 'Another return' END
and
CASE WHEN (Value1 = Value2) AND (Value3 = Value4)
THEN 'Value to return if true'
ELSE 'Value to return if false'
END
This question already has answers here:
ActiveRecord OR query Hash notation
(2 answers)
Closed 7 years ago.
I am trying to specify the index view in my controller with a where or clause. Right now, the code is only returning the condition before the ||, if it finds that.
My code:
def index
#trades = Trade.where(grower_id: session[:user_id]) ||
Trade.where(consumer_id: session[:user_id])
end
I've tried:
#trades = Trade.where(grower_id: session[:user_id]).or(Trade.where(consumer_id: session[:user_id]))
and:
#trades = Trade.where(grower_id: session[:user_id]) ||=(Trade.where(consumer_id: session[:user_id]))
I also checked out this response, but those suggestions aren't working.
Any help would be greatly appreciated!
Active Record Query does not support or clause in Rails 4. But, it's coming in Rails 5. If you want to use or clause in your >= Rails 4.2 application for Active Record queries, you can use the where-or gem. Then, you can have your Active Record query like this:
#trades = Trade.where(grower_id: session[:user_id]).or(Trade.where(consumer_id: session[:user_id]))
To explain the behaviour of this code:
def index
#trades = Trade.where(grower_id: session[:user_id]) ||
Trade.where(consumer_id: session[:user_id])
end
|| (logical or operator) will look at the value of the first expression, if it's present i.e. if it's not nil or false, then will return the value. If not, only then it goes to the next expression to evaluate. That's why you are seeing that behavior which is expected. See this post for some more details on how logical or operator (||) works.
Now, to fix your problem, you can re-write your query using SQL:
def index
#trades = Trade.where('trades.grower_id = ? OR trades.consumer_id = ?', session[:user_id], session[:user_id])
end
As mu is too short pointed in the comment section, to avoid repetition in your code, you can also write the query this way:
def index
#trades = Trade.where('trades.grower_id = :user_id OR trades.consumer_id = :user_id', user_id: session[:user_id])
end
I have a ruby script which queries the database, extracts some info and publishes the result. The row in the code snippet below is a record(I'm looping through the rows). I have a flag Cancelled [Which is of datatype bit(1)] in the table.
Irrespective of the flag value, I am always publishing Cancelled = 'No'. Basically the conditional statement is always returning false even though that's not the case. Can someone tell me what I'm doing wrong?
db = Mysql2::Client.new(parameters....)
query_str = "Some query"
row = db.query(query_str).first
Cancelled = 'No'
if row['Cancelled'] == true
Cancelled = 'Yes'
end
I'm new to this so please excuse me if this is a silly question.
Is it possible to use a CASE statement with a List as the result?
WHERE
Service.Closed_Flag = 'False'
AND Service.Board_name = CASE
WHEN #SORT = 'True'
THEN (#BOARD)
ELSE Service.Board_name
END
#SORT is Boolean
#BOARD is a Drop-down List with option to allow multiple picks
The above code works fine unless I select more than one option for the #BOARD parameter resulting in a list.
I was hoping I could use IN within the CASE statement, THEN IN(#BOARD)?
SOLUTION:
I found a solution to my problem shortly after posting, always seems to happen to me. I can't answer my question until another 7 hours have passed to here is my solution:
CASE returns a scalar value only.
I adjusted my statement to the following which solved my problem:
WHERE
Service.Closed_Flag = 'False'
AND ((#SORT = 'True' AND Service.Board_name IN (#BOARD))
OR
(#SORT = 'False' AND Service.Board_name = Service.Board_name))
I found a solution to my problem shortly after posting, always seems to happen to me.
CASE returns a scalar value only.
I adjusted my statement to the following which solved my problem:
WHERE
Service.Closed_Flag = 'False'
AND (
(#SORT = 'True' AND Service.Board_name IN (#BOARD))
OR
(#SORT = 'False' AND Service.Board_name = Service.Board_name)
)
In Rails 3.0.9 (Ruby 1.9.2, MySQL) I have a method that is supposed to find users using two fields, type(string) and flag(boolean). The string part of the query works fine, but not the boolean part.
here's the code:
def find_users(type)
#private_users = User.where('type =? and flag != ?', org_type, true)
end
to try to figure out what's going on, put this code:
#users.each do |f|
puts "oh helllllllllllllllllllllllllllo #{f.user_name}"
puts "oh helllllllllllllllllllllllllllo #{f.flag}"
puts "oh helllllllllllllllllllllllllllo #{f.type}"
end
The flag field is blank/null for most of these and those are the ones I'm trying to pick up.
If I change the expression to ('type =? and flag = ?', type, true), it correctly finds the ones where 1 is the value of the flag.
I've tried these things to no avail:
User.where('type =? and flag = ?', org_type, false)
User.where('type =? and flag = ?', org_type, "")
User.where('type =? and flag = ?', org_type, nil)
This probably an easy question for someone, so I hoping some knows the answer. Thank you!
Try
User.where(:type => org_type, flag: nil)
Should result in the SQL query
SELECT "users".* FROM "users" WHERE "users"."type" = 'some_type' AND "users"."flag" IS NULL
The key being that ActiveRecord will use the IS NULL operator for your flag column.
If a nullable column is NULL, you can't use normal comparison operators. You need to use the IS NULL / IS NOT NULL operator.
You can also try the NULL-safe equals (<=>) operator (which is a new one to me too)