In SQLPlus, how do I add string that contains a special character to my database? The special character I am trying to use is 'é'.
I've tried Aim(atl+130) which is: Aimé but returns 'Aim?'
I copied your character and did this to find the value 233:
select dump('é') from dual;
So you should be able to do this and get it back (of course you can INSERT it too):
select 'Aim' || chr(233) from dual;
Related
could someone please help me to write a query to substitute hyphens in rails DB field with space?
For eg:
If I have a field called 'name' in a table User having a value 'asdc-sd bc', and want to remove special characters like '-' and replace it with space to match with a given name 'asdc sd bc'.
I tried using lower to convert the name to lowercase but am not able to find out how to substitute the hyphens with space. Please help!
You can use SQL REPLACE function to replace - with spaces(' ').
For example, if you are querying on name column of User model, you can write your query like below:
query_str = 'asdc sd bc'
User.where("REPLACE(name, '-', ' ') = ?", query_str)
I have a plain SQL query written by a trusted administrator that is to be run in a Rails (4.2) app. I am sanitizing it with ActiveRecord::Base.send(:sanitize_sql, ...) to allow user inputs to act as conditions, using the ? character for bind variables. The code has to allow arbitrary SQL, so I'm not interested in the arguments about why this is not the Rails way, etc.
The problem is that I can not include ? in a result field in the SQL without the underlying replace_bind_variables method replacing an intended literal ? in the result.
A simple query for example would be:
select 'http://www.google.com?q=' || res from some_table where a = ?;
To sanitize:
ActiveRecord::Base.send(:sanitize_sql, [sql, 'not me'], :some_table)
The sanitization fails because the ? in the URL gets replaced with the data intended for the condition, leading to the exception:
ActiveRecord::PreparedStatementInvalid: wrong number of bind variables (1 for 2)
The question is, does sanitize_sql or some variant allow literal ? characters to be included in a query so that they are not replaced? Is there some way of escaping them?
In the end I read through the ActiveRecord source and couldn't identify a way to handle this situation without a lot of code changes. There doesn't appear to be a way to escape the ? characters.
To resolve it for this one query I ended up using the SQL chr() function to generate a character that would pass the santization step untouched:
select 'http://www.google.com' || chr(63) || 'q=' || res from some_table where a = ?;
ASCII character 63 is ?.
Although not a perfect solution, I could at least get this one SQL query into the system without having to make massive code changes.
I'm using SPSS modeler and I have a variable that the software recognizes as numeric. So the missing values are $null$. I want that the missing values of the variable are selectionable with '', as character.
So I would: or trasform the format of the variable from numeric to character or change only the missing values from $null$ to ''.
How can I fix?
thanks in advance
The best way to select null values in a numeric field is to use the #NULL() function from the Blanks and Null section of the Expression Builder.
For example, if you wanted to keep only the null values so that you could inspect them, you might use a Select node. Leave the radio button set as Include. Press the Expression Builder (calculator) button. Change the filter in the drop menu on the left side from General Functions to show Blanks and Null (press B 2 or 3 times). Double-click on #NULL(ITEM). Go to the right side and double-click on your numeric field name. Put a Table node at the end and run it.
Using Select #NULL in IBM SPSS Modeler
Another way to view just the null rows is to enter the #NULL(varname) function into the "Highlight records where" section of the Table dialog box.
"Highlight records where" dialog
When you run the table, any row that is true for this condition will be shown in red.
If you really need the variable to be a string, then use a Compute node to create a copy of this field under a new name and use the to_string() function in the Conversion section of the Expression Builder to change the type of the variable. Now you will be able to use the the Select node to grab "" as the missing value. Or you could use the Filler node to replace the column, but then you would not be able to compare before and after.
The dialog examples shown in this answer use this sample stream that is installed with your IBM SPSS Modeler software:
C:\Program Files\IBM\SPSS\Modeler\18.0\Demos\streams\featureselection.str
The easiest way to do it it's using the Fill node with the configurations:
A) FIELD
B) Condition = #NULL(#FIELD)
C) Replace by = ' '
This node will replace all $null$ for ' ' at the same variable chosen in option a.
I don't think you can customize how you visualize $nulls. (I know it's possible in SQL db though)
So I'd suggest that you work with the numbers and when you want to visualize or export the results, then turn the field to a string one then replace nulls:
Filled node > to_string(#FIELD)
Filler node > blank and nulls > #FIELD = ''
I'm selecting an email address but I don't want to display the full email. Only the part before the '#'. How can I cut it. I know how to display only certain amount of characters or numbers. But how do I specify to display only till the '#' symbol.
Thank you.
Recent versions of Informix SQL have the CHARINDEX() function which can be used to isolate where the '#' symbol appears:
SELECT LEFT(email_addr, CHARINDEX('#', email_addr)-1)
CHARINDEX() will return 0 if not found, otherwise the ordinal position of the located string. My testing found that LEFT() doesn't complain about being passed 0 or -1, so it's safe to execute this as is, you don't have to verify that you get something back from CHARINDEX() first.
CREATE TEMP TABLE ex1
(
email_addr VARCHAR(60)
) WITH NO LOG;
INSERT INTO ex1 VALUES ('ret#example.com.au');
INSERT INTO ex1 VALUES ('emdee#gmail.com');
INSERT INTO ex1 VALUES ('unknown');
INSERT INTO ex1 VALUES (NULL);
INSERT INTO ex1 VALUES ('#bademail');
SELECT LEFT(email_addr, CHARINDEX('#', email_addr)-1) FROM ex1
... produces:
(expression)
ret
emdee
5 row(s) retrieved.
If you have an older version of Informix that doesn't support CHARINDEX(), you'll be forced to iterate through the string character by character, until you find the '#' symbol.
Suppose we have a textarea in which we put example string. Textbox contain :
Earth is revolving around the Sun.
But at the time of saving, I just pressed a enter key after "the sun". Now the statements in texbox ::
Earth is revolving around
the Sun
Now, in database where enter was pressed the \r is stored. Now i am trying to fetch the data but unable, because my query is of such type ::
SELECT * FROM "volume_factors" WHERE lower(volume_notes) like E'atest\\r\\n 100'
Actual data stored in database field
atest\r
100
Please suggest me to solve the issue.I have tried gsub to replace but no change.
search_text_array[1] = search_text_array[1].gsub('\\r\\n','\r\n')
Thanks in Advance !!!
Try this:
update volume_factors set volume_notes = regexp_replace(volume_notes, '\r\n', ' ');
That's to replace crlf with one space for data that is already in the database. You use postgresql's psql to do this.
To prevent new data containing crlf entering database, you should do it in the application. If you use ruby's gsub, do not use single quote, use double quote to recognize \n like this:
thestring.gsub("\n", " ")
Here we can replace \r\n by % to fetch the data.
Seaching Query will be like this ::
SELECT * FROM "volume_factors" WHERE lower(volume_notes) like E'atest% 100'
gsub function ::
search_text_array[1] = search_text_array[1].gsub('\\r\\n','%')