Invalid Column Name ' ' on Concatenating Columns - stored-procedures

SET #QUERY = 'SELECT ID, CODE, (FIRST_NAME+" "+ISNULL(MIDDLE_NAME,'')+" "+SUR_NAME) AS NAME FROM [Common].[CM_Personnel_MF]
Executing above query shows below error:
Invalid column name ' '.
If I change the query to:
SET #QUERY = 'SELECT ID, CODE, (FIRST_NAME+' '+ISNULL(MIDDLE_NAME,' ')+' '+SUR_NAME) AS NAME FROM [Common].[CM_Personnel_MF]';
Getting Incorrect syntax as shown below
Incorrect syntax near '+ISNULL(MIDDLE_NAME,'.

You need to escape ' with '' (use double single quotes)
SET #QUERY = 'SELECT ID, CODE, (FIRST_NAME+'' ''+ISNULL(MIDDLE_NAME,'''')+'' ''+SUR_NAME) AS NAME FROM [Common].[CM_Personnel_MF]

Related

empty or null field check with postgres query in rails

I am trying to check two condition with case in posgtres query, in my table one field may have two values either will be empty or will be single quote('') so i tried this query but it didn't execute
CASE WHEN (items.item_code is NULL OR items.item_code = '') THEN
items.name::text ELSE items.item_code::text END as item_code
Error like:
PG::Error: ERROR: syntax error at or near ")" LINE 1: ...HEN
(items.item_code is NULL OR items.item_code = ) THEN ite..
you have problem with quotes in ruby, not in sql statement.
try changing to:
CASE
WHEN (coalesce(length(items.item_code),0) + length(items.item_code)) < 1
THEN items.name::text ELSE items.item_code::text
END as item_code
the code above does same condition check as yours, buta voids using quotes. I suppose your could should be smth like:
CASE WHEN (items.item_code is NULL OR items.item_code = \'\') THEN
items.name::text ELSE items.item_code::text END as item_code

Selecting all options from select then iterate through values to find selected value - Selenium Webdriver

I am having an issue extracting all the options in a drop down and then iterating through values to get selected value. Ruby code Below:
select = ##driver.find_element(:id, 'dropdown_7')
all_options = select.find_elements(:tag_name, 'option')
all_options.each do |i|
puts 'Value is: ' + i.attribute('Andorra')
i.click
HTML code:
<select id="dropdown_7" name="dropdown_7" class=" piereg_validate[required]"><option value="Afghanistan">Afghanistan</option></select>
Error Message: `+': no implicit conversion of nil into String (TypeError)
Not sure what this means apart from + = nil and no conversion of string?
The error is thrown because i.attribute('Andorra') returned nil which ruby failed to convert to a string. Here are a few examples that should get you what you want:
# print the name attribute
puts 'Name is: %s' % i.attribute('name')
# print the value attribute
puts 'Value is: %s' % i.attribute('value')
# print the text content
puts 'Text is: %s' % i.text

Ruby regex to extract string between single/double quotes that may include an escaped character

I am trying to write a regex that can pull a string value from a mysql string.
That is, if I have the following generated sql string and I want to be able to extract the first_name:
my_string = "SELECT * FROM users WHERE first_name = 'first name value'"
What I currently have appears to work for most cases:
result = /first_name = ['"](.*?)['"]/i.match my_string
However, the issue is when there is either a ' or " in the first_name, i.e.
result = "SELECT * FROM users WHERE first_name = 'first\"s name value'"
or
result = "SELECT * FROM users WHERE first_name = 'first\\'s name value'"
the returned result is only the value UP to the escaped character, so in these cases, the returned group would be "first". How can I fix it so that the entire first_name value gets returned?
Ruby 1.9+ Solution: Identically Named Named Groups
You seem to need to match strings inside single or double quotes and only match between the matching quotes.
Use the Ruby regex feature to use multiple named groups with the same name:
/first_name = (?:'(?<val>[^'\\]*(?:\\.[^'\\]*)*)'|"(?<val>[^"\\]*(?:\\.[^"\\]*)*"))/i
See the Rubular demo
The value in-between the quotes will be inside "val" group.
Here is an IDEONE Ruby demo:
my_string = "SELECT * FROM users WHERE first_name = 'first name value'"
my_string2 = "SELECT * FROM users WHERE first_name = 'first\"s name value'"
my_string3 = "SELECT * FROM users WHERE first_name = 'first\\'s name value'"
rx = /first_name = (?:'(?<val>[^'\\]*(?:\\.[^'\\]*)*)'|"(?<val>[^"\\]*(?:\\.[^"\\]*)*"))/i
puts rx.match my_string # => first_name = 'first name value'
puts rx.match my_string2 # => first_name = 'first"s name value'
puts rx.match my_string3 # => first_name = 'first\'s name value'
To get the "val" (demo):
rx.match(my_string)["val"] # => first name value
Ruby 1.8 Solution
Since named groups were introduced since Ruby 1.9 and you need it to work in Ruby 1.8, use a character class restricted with a negative lookahead solution.
/first_name = (['"])((?:(?!\1)[^\\])*(?:\\.(?:(?!\1)[^\\])*)*)\1/i
See the Rubular demo
The (['"]) matches and captures into Group 1 a ' or ". The (?:(?!\1)[^\\])* matches 0+ characters other than \ (due to [^\\]) and that is not " or ' (due to (?!\1)). The (?:\\.(?:(?!\1)[^\\])*)*) matches 0+ sequences of an escape sequences (see \\.) that is followed with 0+ characters other than ', " or \. The \1 backreference matches the corresponding closing quote.
See another Ruby demo:
my_string = "SELECT * FROM users WHERE first_name = 'first name value'"
my_string2 = "SELECT * FROM users WHERE first_name = 'first\"s name value'"
my_string3 = "SELECT * FROM users WHERE first_name = 'first\\'s name value'"
rx = /first_name = (['"])((?:(?!\1)[^\\])*(?:\\.(?:(?!\1)[^\\])*)*)\1/i
puts rx.match my_string # => first_name = 'first name value'
puts rx.match(my_string)[2] # => first name value
puts rx.match my_string2 # => first_name = 'first"s name value'
puts rx.match(my_string2)[2] # => first"s name value
puts rx.match my_string3 # => first_name = 'first\'s name value'
puts rx.match(my_string3)[2] # => first\'s name value
You could try this
/first_name = ['"](.*?)['"]\z/i
example here
I believe this regexp would fix it:
/first_name = ['"]((.*?)['"])*/i
Live example here.
I tested this out on Rubular and it seems to get the value that you're looking for. The only thing is that it also captures your escape chars which you could replace:
f_name_match = /first_name = \'(.+)\'/i.match(string).replace('\')

ruby: wrap each element of an array in additional quotes

I have a following string :
a = "001;Barbara;122"
I split in into array of strings:
names = a.split(";")
names = ["001", "Barbara", "122"]
What should I do to have each element wrapped additionally in '' quotes?
The result should be
names = ["'001'", "'Barbara'", "'122'"]
I know it sounds strange but I need it for database query in ruby on rails. For some reason I cannot access database record if my name is in "" quotes. I do have mk1==0006 in the database but rails does not want to access it somehow. However, it does access 1222.
sql = "SELECT mk1, mk2, pk1, pk2, pk3, value_string, value_number FROM infos WHERE mk1 in (0006) AND value_string ='männlich';"
recs = ClinicdbInfo.find_by_sql(sql)
=> []
sql = "SELECT mk1, mk2, pk1, pk2, pk3, value_string, value_number FROM infos WHERE mk1 in (1222) AND value_string ='männlich';"
recs = ClinicdbInfo.find_by_sql(sql)
=> [#<Info mk1: "1222", mk2: "", pk1: "Information allgemein", pk2: "Geschlecht", pk3: "Wert", value_string: "männlich", value_number: nil>]
So, I just need to wrap every element of names into additional ''-quotes.
names.map{ |e| "'" + e + "'" }
=> ["'001'", "'Barbara'", "'122'"]
or
names.map{ |e| "'#{e}'" }
=> ["'001'", "'Barbara'", "'122'"]
You should not concatenate parameters to sql string manually; you should instead pass parameters into find_by_sql method. Example:
sql = "SELECT mk1, mk2, pk1, pk2, pk3, value_string, value_number FROM infos WHERE mk1 in (?) AND value_string = ?"
recs = ClinicdbInfo.find_by_sql [sql, 1222, "männlich"]
This way the necessary type conversions and escaping to prevent against sql injection will be handled by Rails.
I agree with #jesenko that you should not construct your SQL queries and let AR do the type conversion and escape input against SQL injection attacts. However, there are other use cases when you'd want this. For example, when you want to insert an array of strings into your js. I prefer using the following syntax for those rare cases:
names.map &:inspect # => ["\"001\"", "\"Barbara\"", "\"122\""]
If you are print this in your views, you should mark it as html safe:
names.map(&:inspect).html_safe

matching string then returning results using regexp

Is there a way to match the following string so I get user name and 50?
hey "user name":/users/50
I may also have more than one instance of this in a string.
can you try the following
string = 'hey "user name":/users/50'
matches = string.scan /"(?<name>[A-Za-z ]+)":\/users\/(?<user_id>\d+)/
matches will be an array containing arrays with 2 elements where the first element is the name and the 2nd element is the user_id
>> matches # [['user name', 50]]

Resources