How to insert a string which contains an &
I keep on forgetting this and coming back to it again! I think the best answer is a combination of the responses provided so far.
Firstly, & is the variable prefix in sqlplus/sqldeveloper, hence the problem - when it appears, it is expected to be part of a variable name.
SET DEFINE OFF will stop sqlplus interpreting & this way.
But what if you need to use sqlplus variables and literal & characters?
- You need SET DEFINE ON to make variables work
- And SET ESCAPE ON to escape uses of &.
e.g.
set define on
set escape on
define myvar=/forth
select 'back\\ \& &myvar' as swing from dual;
Produces:
old 1: select 'back\\ \& &myvar' from dual
new 1: select 'back\ & /forth' from dual
SWING
--------------
back\ & /forth
If you want to use a different escape character:
set define on
set escape '#'
define myvar=/forth
select 'back\ #& &myvar' as swing from dual;
When you set a specific escape character, you may see 'SP2-0272: escape character cannot be alphanumeric or whitespace'. This probably means you already have the escape character defined, and things get horribly self-referential. The clean way of avoiding this problem is to set escape off first:
set escape off
set escape '#'
How to insert a value that contains an apostrophe (single quote)?
Escape the apostrophe (i.e. double-up the single quote character) in your SQL:
INSERT INTO Person
(First, Last)
VALUES
('Joe', 'O''Brien')
/\
right here
The same applies to SELECT queries:
SELECT First, Last FROM Person WHERE Last = 'O''Brien'
The apostrophe, or single quote, is a special character in SQL that specifies the beginning and end of string data. This means that to use it as part of your literal string data you need to escape
the special character. With a single quote this is typically accomplished by doubling your quote. (Two single quote characters, not double-quote instead of a single quote.)
Note: You should only ever worry about this issue when you manually edit data via a raw SQL interface since writing queries outside of development and testing should be a rare occurrence. In code there are techniques and frameworks (depending on your stack) that take care of escaping special characters, SQL injection, etc.
Inserting characters in strings in python
Just keep it simple. Check to see if the position is greater than the length of the word then just print the word, else proceed with your logic:
C = input("Choose your charecter to insert. ")
P = int(input("Choose your character's position. "))
S = input("Choose your string. ")
if P > len(S):
print(S)
else:
st = S[:P] + C + S[P:]
print(st)
print(C, P, S)
How to insert a character in a string at a certain position?
int j = 123456;
String x = Integer.toString(j);
x = x.substring(0, 4) + "." + x.substring(4, x.length());
How to insert a character after every 2 characters in a string
Assume the string's length is always an even number,
>>> s = '12345678'
>>> t = iter(s)
>>> '-'.join(a+b for a,b in zip(t, t))
'12-34-56-78'
The t
can also be eliminated with
>>> '-'.join(a+b for a,b in zip(s[::2], s[1::2]))
'12-34-56-78'
The algorithm is to group the string into pairs, then join them with the -
character.
The code is written like this. Firstly, it is split into odd digits and even digits.
>>> s[::2], s[1::2]
('1357', '2468')
Then the zip
function is used to combine them into an iterable of tuples.
>>> list( zip(s[::2], s[1::2]) )
[('1', '2'), ('3', '4'), ('5', '6'), ('7', '8')]
But tuples aren't what we want. This should be a list of strings. This is the purpose of the list comprehension
>>> [a+b for a,b in zip(s[::2], s[1::2])]
['12', '34', '56', '78']
Finally we use str.join()
to combine the list.
>>> '-'.join(a+b for a,b in zip(s[::2], s[1::2]))
'12-34-56-78'
The first piece of code is the same idea, but consumes less memory if the string is long.
Inserting a string into a list without getting split into characters
To add to the end of the list:
list.append('foo')
To insert at the beginning:
list.insert(0, 'foo')
How to create SQLite query to insert a string that contains both and '?
The answer is to let the library do the quoting.
row = """Cristina O'Brien "Valenzuela" """
query = "INSERT INTO Actors (Actor) VALUES (?);"
conn.execute(query, (row,))
Insert variable values in the middle of a string
You can use string.Format
:
string template = "Hi We have these flights for you: {0}. Which one do you want";
string data = "A, B, C, D";
string message = string.Format(template, data);
You should load template
from your resource file and data
is your runtime values.
Be careful if you're translating to multiple languages, though: in some cases, you'll need different tokens (the {0}
) in different languages.
Related Topics
Rolling 90 Days Active Users in Bigquery, Improving Preformance (Dau/Mau/Wau)
How to Get Rid of "Error 1329: No Data - Zero Rows Fetched, Selected, or Processed"
Grant Select Permission on a View, But Not on Underlying Objects
How to Search All Columns in a Table
Difference Between Timestamps in Milliseconds in Oracle
How to Use Alias in Where Clause
Difference Between === Null and Isnull in Spark Datadrame
Hql: How to Perform an Inner Join on a Subquery
How to Create a Decimal Field in Access with Alter Table
Oracle- Split String Comma Delimited (String Contains Spaces and Consecutive Commas)
How to Use If/Else Statement to Update or Create New Xml Node Entry in SQL
How to Transform Vertical Data into Horizontal Data with SQL
Check If a Parameter Is Null or Empty in a Stored Procedure
Function with SQL Query Has No Destination for Result Data
How to Select Records Without Duplicate on Just One Field in SQL