I am running an SQL (Oracle) statement like that
select * from table
where table_id in ('265&310', '266&320')
While running through TOAD, it consider & as some variable placeholder and it asks for its value. If it was for 1-2 place holders then I could have set it in TOAD but the in clause has like 200 of strings.
How to put this query?
I want to export the DATASET as SQL INSERT statement, so I can't use this in SQL-PLUS.
SET DEFINE OFF;
Will work to turn the prompting for variable off..
or
SET ESCAPE ON;
SELECT 'blah \& blah' AS DES FROM DUAL;
In TOAD, you can disable the prompt for substitution variables from the options dialog:
You need to uncheck:
View –> Toad Options –> Execute/Compile –> Prompt for Substitution variables.
You can escape the ampersand character by using concatenation, like this:
select * from table
where table_id in ('265' || '&' || '310', '266' || '&' || '320')
Related
I am trying to filter my result set to only return values which start with NIR_.
My SQL statement to do so is as follows
select * from run where name like %NIR_%
The result set also includes names like
NIRMeta_Invalid
NIRMeta_Position
I am not sure what I am doing wrong. I only need to select names which start with NIR_.
You need to escape the underscore in your LIKE pattern if you want it to be treated as a literal.
In SQL Server:
select * from run where name like 'NIR[_]%'
In MySQL and Oracle:
select * from run where name like 'NIR\_%'
If you want names that only start with NIR, then remove the first wildcard in the like pattern:
where name like 'NIR_%'
Note that _ is also a wildcard, so you probably want:
where name like 'NIR\_%'
You can use ESCAPE option to achieve this.
SELECT * FROM run WHERE name LIKE 'NIR#_%' ESCAPE '#'
Sample execution with the given data:
DECLARE #Run TABLE (name VARCHAR (100));
INSERT INTO #Run (name) VALUES
('NIR_MA'), ('NIR_RUN'), ('NIRMeta_Invalid'), ('NIRMeta_Position');
SELECT * FROM #Run WHERE name LIKE 'NIR#_%' ESCAPE '#'
Result:
name
-----
NIR_MA
NIR_RUN
I want to use the /*csv*/ to set the output to csv in oracle. But I need to change the delimiter and enclosing character.
I don't want to insert a bunch of "||" in my query to resolve this.
I'm trying to spool a sql query to a CSV file using a command prompt, however one column of data I'm returning contains comma's in the data and is causing the data to go into the next column. Is there a way to get around this? Ideally, I would like this particular column returned in the middle of query, not at the end.
I'm currently using the following commands:
set termout off;
set colsep ',';
set underline off;
set feedback off;
set pagesize 0;
set linesize 1500;
set trimspool on;
I know the set colsep ',' command is the issue however, haven't been able to figure out what to replace it with. Does anyone have any recommendations?
Also, right before my sql query, I'm using a select statement to pull the header information. Select 'a, b, c' from dual; Not sure if that makes a difference in the answer or not.
Two potential answers. Neither of them perfect. If you have commas in some of the results, but no pipes (|), you could switch to a pipe-delimited with
set colsep '|'
Most software that can read csv will do just fine with that format.
If that doesn't work for you, you can realize that to treat commas within a column, you'll need to wrap every data item in quotes:
"data 1","data 2","data,with,commas"
To do this, each separator will need to be "," so you can
set colsep '","'
This will not have quotation marks at the beginning and ending of each line, so you can then wrap every line in quotes with sed:
sed 's/^[^$]*$/"&"/'
You can modify your select query like
select REPLACE(column_name, ',',' ') column name from table_name
This will replace comma value from your column data with space.
You can modify your query that returns the result set by surrounding that column with double-quotes. Assuming b is the culprit:
select a
, '"' || trim(B) || '"' as b
, c
from your_table;
Proper syntax depends on your RDBMS version of course.
i have a table like
id name
10 bob
20 bill
i want to select only name column in output with double quotes
like select '"'||name||'"' from table
it is giving me the correct output but is there any other way without using concatenation ...
Thank you..
Using this you can get result with double quotes
' " ' + columnName + ' " '
Example
Query
SELECT '"'+Name+'"' , Age
FROM customer
Result
"Viranja" | 27
Create a virtual column that adds the quotes:
CREATE TABLE
....
quoted_name VARCHAR2 GENERATED ALWAYS AS ('"' || name || '"') VIRTUAL,
...
See here for more information:
http://www.oracle-base.com/articles/11g/virtual-columns-11gr1.php
this will check for any name with at least one double quote
select * from table
where name like '%"%'
If your intention is to be able to "export" the result into space or comma-delimited text file, use a view to "format" your data. You will need to this for your date columns as well.
There are two scenarios you would want to use double quotes in sql in my opinion.
Updating a string column which contains single multiple quotes in it. (you have to escape it)
updating blog contents in columns which you cant edit in "edit top 200 rows"
so, if you want to use double quotes follow this.
SET QUOTED_IDENTIFIER OFF
BEGIN
DECLARE #YourSqlStmt AS VarChar(5000) -- Declare a variable.
SET #YourSqlStmt = "ok"
PRINT #YourSqlStmt -- do your operations here
END
This saves time and you need not to escape single quotes in a existing string content of the column.
I need to find records containing html code such as ' ' But when I try to run the select * from table_name where column like ' %'
I got prompt asking for the value of nbsp. I guess the database thinks that nbsp is a parameter. I am wondering if the is an escape character so that I can tell the database that "&" is part of my query string. I tryed '\ ' but didn't work.
My environment is Oracle 9i with sqlplus client.
Thanks.
Have a look at this:
SQL Plus FAQ
e.g.
SET ESCAPE '\'
SELECT '\&abc' FROM dual;
Easier way:
SET DEFINE OFF
See:
SET DEFINE
The backslash should work, but I think you need to start your query with
SET ESCAPE ON
In PL/SQL, you would use:
BEGIN select <Column> from <Table_name> into <Variable> where <Column> LIKE '\ \%' ESCAPE '\'; END
/
Resources:
Wilcards in SQL on PSOUG.org
LIKE Condition in Oracle® Database SQL Language Reference 11g Release 2 (11.2)