I saw this line in an oracle script
SELECT COUNT(*) INTO version1 FROM &1..xxdt WHERE version = 3310;
I don't understand the &1.. part. I think xxdt is the name of the table, so what is the &1.. thing in front of it?
The &1 prompts for a user-entered value. Note how the entered value mytable is substituted for &1 below:
SQL> SELECT COUNT(*) FROM &1 WHERE col1 = 12;
Enter value for 1: mytable
old 1: SELECT COUNT(*) FROM &1 WHERE col1 = 12
new 1: SELECT COUNT(*) FROM mytable WHERE col1 = 12
COUNT(*)
----------
0
The dot (.) appends every non-space character that follows the dot to the entered value. Note how the value table after the dot is appended to the entered my:
SQL> SELECT COUNT(*) FROM &1.table WHERE COL1 = 12;
Enter value for 1: my
old 1: SELECT COUNT(*) FROM &1.table WHERE COL1 = 12
new 1: SELECT COUNT(*) FROM mytable WHERE COL1 = 12
COUNT(*)
----------
0
The two dots in &1..xxdt aren't a special operator. The first dot means to append; the second dot is literal. It looks like the &1 in your example is used to prompt for a schema/owner name. Note below how I've entered ed and &1..mytable is transformed into ed.mytable:
SQL> SELECT COUNT(*) FROM &1..mytable WHERE COL1 = 12;
Enter value for 1: ed
old 1: SELECT COUNT(*) FROM &1..mytable WHERE COL1 = 12
new 1: SELECT COUNT(*) FROM ed.mytable WHERE COL1 = 12
COUNT(*)
----------
0
Addendum: Great suggestion from David Aldridge to include a quick explanation of SET DEFINE, which goes hand-in-hand with variable substitution. Here goes...
The substitutions above are done by SQLPlus, and its behavior can be controlled using SET DEFINE:
SET DEFINE ON will allow the substitution and use the defined substitution character. This is normally the SQLPlus default, and was the case when I ran the queries above.
SET DEFINE <char> sets the substitution character. The ampersand (&) is the usual default. SQLPlus will only accept non-alphanumeric, non-space characters for the substitution character. Note that I've never had to change this value in over a decade of using Oracle.
SET DEFINE OFF will stop substitution. Use this if you need to have an actual literal ampersand in your query or proc, because SQLPlus will treat the ampersand as a substitution character no matter where you put it, including in a string.
I believe the ampersand is used for substitution variables. See http://www.oracle-base.com/articles/misc/literals-substitution-variables-and-bind-variables.php
Substitution Variables
Substitution variables are a feature of the SQL*Plus tool. They have
nothing to do with the way SQL is processed by the database server.
When a substitution variable is used in a statement, SQL*Plus requests
an input value and rewrites the statement to include it. The rewritten
statement is passed to the database. As a result, the database server
knows nothing of the substitution variable. The following example
illustrates this by repeating the previous test, this time using
substitution variables.
If SET CONCAT is a period (.) and you want to append a period immediately after a substitution variable then use two periods together. For example:
define mycity = Melbourne
spool &mycity..log
is the same as:
spool Melbourne.log
https://blogs.oracle.com/opal/entry/sqlplus_101_substitution_varia#9_7
Another Example:
Create a file run.sql with below queries in vi editor:
vi run.sql
select 'mv &1..log &2._' ||to_char(sysdate,'DD-MON-YYYY-HH24-MI') || '.log' from dual;
:wq
Now run:
sqlplus scott/tiger #run.sql listener renamelistener
Code Break Down:
(&1) -first parameter "listener"
(&2) -second parameter "renamelistener"
(.log) -is a substituted of &1
(.) -where the single dot(.) appends non-space characters with a passing parameter value
Thanks!
Related
How do I remove commas between a string of characters? In all posts. Search and Replace?
I have something like this: xfields column
...||ilo_ocen|870,664||kraj|...
I want to remove commas between "||ilo_ocen|" and "||kraj|" in all posts.
Thank you in advance for your help! :)
Usually, we replace it with something else. As you said that you want to "remove" comma, you'd replace it with an empty string.
This is Oracle database example (as you didn't state which database you actually use); I believe other databases have a similar function. Sample data in lines #1 - 4; query you might need begins at line #6.
SQL> with test (xfields) as
2 -- sample data
3 (select '||ilo_ocen|870,664||kraj|' from dual union all
4 select 'Little,foot has a big nose' from dual)
5 -- REPLACE!
6 select replace(xfields, ',', '') result
7 from test;
RESULT
--------------------------
||ilo_ocen|870664||kraj|
Littlefoot has a big nose
SQL>
I am trying to query Keyword 100% using Like command.
LIKE (‘%100%%’)
But the command is querying for all keywords with 100 which is not what I
want
Use Escape Character.
Try:
Select * from MyTable m where m.Column1 like '%100\%%' escape '\'
Escape Character can be set as per your convenience.
In the above query, replace MyTable with your table name and Column1 with your Column Name.
You could also take advantage of SQL Server's LIKE operator's regex syntax, and use [%] to represent a literal percent:
SELECT *
FROM yourTable
WHERE col LIKE '%100[%]%';
Demo
I prefer this method to the accepted answer because it makes more explicit the intention to represent a literal character, and it avoids the possible need for an ESCAPE clause.
line1 SELECT 'A'
line2 SELECT CONCAT(a,b);
line3 SELECT 'B'
Trying this out in SQL Server 2008 and found that line1 will not be queried, due to the batch failing on line 2. If SQL Server is a procedural programming then line1 should've been successfully queried right?
The reason for the observed behaviour is that you see a compile-time error, not a run-time one. Here are some examples that might shed some light on differences between them.
When database engine encounters a compile-time error, the entire batch isn't executed at all, and your code comprises a single batch. That's why you don't see any results. However, if you would separate these statements with go (in SSMS, sqlcmd or any other client that recognises it) only the second statement will be skipped.
SELECT 'A'
Is valid in T-SQL, and it will quite literally display A as the result with no column header. But that query does NOT create a reference called a that holds the value of A.
SELECT CONCAT(a,b);
In SQL 2008 this won't work because that function simply isn't available. Note also that this row terminates with a semi-colon. Neither a nor b exist however as the only preceding line of code does not create any persistent reference.
SELECT a + b;
In SQL 2008 this might work (if both a and b existed and were strings)
SELECT 'B'
Is valid in T-SQL, and it will quite literally display B as the result with no column header. But that query does NOT create a reference called b that holds the value of B.
in brief
SELECT 'A' does not give that value of A any name to refer to later.
the concatenation is in the wrong order and terminates with ;
SELECT 'B' does not give that value of B any name to refer to later.
T-SQL does allow the following:
DECLARE #a AS VARCHAR(10)
DECLARE #b AS VARCHAR(10)
SET #a = 'A'
SET #B = 'B'
SELECT #a + #b
;
Another approach:
select a + b
from (select 'A' as a, 'B' as b) as derived
Here the columns of the derived table are given an alias of a and b, which can be referenced in the outer select clause to perform the concatenation.
I have an SQL SELECT query that's grabbing some data from my database. I need to replace a certain word that contains a semicolon in my SELECT query. Exactly this:
REPLACE(Table.Field,'"','') AS Field1
The error I'm getting reads
Unclosed quotation mark after the character string '"'.
So I think the semicolon is terminating the query. How can I escape that semicolon?
I tried backslashes and using double quotes.
Some sample data and expected output, as requested
Sample data
Field
"Hello"
"Goodbye"
Expected output
Field1
Hello
Goodbye
Full Query
SELECT REPLACE(Table.Name,';','') AS Name,
SUM(Table.Quantity) AS Quantity,
SUM(Table.Price*Table.Quantity) AS Price
FROM Table
GROUP BY Name
The ; symbol doesn't terminate the query and it should not be escaped, if it is part of the string literal (the text enclosed in single quotes ').
Here is a complete example that demonstrates that it works fine in SSMS:
CREATE TABLE #TempTable (Name varchar(50));
INSERT INTO #TempTable (Name) VALUES('Field');
INSERT INTO #TempTable (Name) VALUES('"Hello"');
INSERT INTO #TempTable (Name) VALUES('"Goodbye"');
SELECT
Name
,REPLACE(Name,'"','') AS ReplacedName
FROM #TempTable;
DROP TABLE #TempTable;
This is the result set:
Name ReplacedName
---- ------------
Field Field
"Hello" Hello
"Goodbye" Goodbye
You didn't provide all details of how you construct and execute your query, so I have a guess. It looks like you are:
building the text of the query dynamically
use some web-based tools/languages/technologies for that
web-based text processing tool/language that you use parses the text of your SQL query as if it was HTML and interferes with the result. For one thing, it changes " to the " symbol.
during all this processing you end up with unmatched ' symbol in the text of your SQL. It could come from the user input that you concatenate to your query of from a value stored in your database.
it has nothing to do with the ; symbol. Your error message clearly states that the matching quotation mark (which is ') is missing after the " symbol.
To understand what is going on you should print out the text of the actual SQL query that is sent to the server. Once you have it, it should become obvious what went wrong. I don't think that the Full Query that you put in the question is the real query that you are trying to run. It has syntax error. So, get the real thing first.
This works fine for me
declare #a as nvarchar(50) = '"Hello"'
select REPLACE(#a,'"','') AS Field1
declare #b as nvarchar(50) = '"Goodbye"'
select REPLACE(#b,'"','') AS Field1
Error message says unclosed quotation mark ?
Do you have single quotes in few of your fields ?
In that case you can replace them first as below
REPLACE(Table.Field,'''','') AS Field1
Let me know you need more help with this.
Source
"
the double quote sign "
I think there is no where that this parameter is known as a special phrase that refers to " and cause you error message.
In SQL Server there is just a function like QUOTENAME ( 'character_string' [ , 'quote_character' ] ) that used like this: -Just for ' or " or []-
SELECT QUOTENAME('Sample', '"') --> result is `"Sample"`
SELECT QUOTENAME('Sam"ple', '"') --> result is `"Sam""ple"`
In SQL Server identifiers can be delimited by ", When SET QUOTED_IDENTIFIER is ON -for following the ISO rules-. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers. Literals can be delimited by either single or double quotation marks.
I suggest you using SET QUOTED_IDENTIFIER OFF that make sure, that you've not identifier between " in your query.
Note:
When a table is created, the QUOTED IDENTIFIER option is always stored as ON in the table's metadata even if the option is set to OFF when the table is created.
If you are using a SQL string I suggest this syntax:
REPLACE(Table.Field, CHAR(34), '') As Field1
or
REPLACE(REPLACE(Table.Field, ';', '.'), '".', '') As Field1
So I need to insert some values into a vertica database (via vsql), which may contain quotes and all sorts of special characters. But vertica does not seem to understand character escaping. For example:
rpt=> select "asdasda\"asdasdad" from some_table limit 1;
rpt"> ";
ERROR: syntax error at or near "" from some_table limit 1;
"" at character 26
LINE 1: select "asdasda\"asdasdad" from some_table limit 1;
This is not the insert statement, but you should get the idea.
Well, first off I should have used single quotes. Escape sequences used to work in earlier versions (before 4.0 I believe), but now they are off by default. If you do not want to tweak database config parameters you have two options.
Use E' syntax:
select E'somethin\' here' from v_catalog.dual_p;
Or double the quotes that need to be escaped:
select 'somethin'' here' from v_catalog.dual_p;