Select /*csv*/ in oracle set delimeter and enclose character - sql

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.

Related

SQL Server query with symbol (')

How can I make a query to see if any record have the value (')?
I have tried this:
Select * from table where column LIKE '%'%' and other variants and still get sintax error.
And i have the same problem when I do a query like:
Select * from table where column == 'hello'world'
I have in the database a record stored with hello'world
I guess that bot questions have the same answer.
You just have to escape it by using the single quote twice (''), It will be considered as the single quote rather than the starting/ending of the string as follows:
Select * from table where column LIKE '%''%'
You can also use the QUOTED_IDENTIFIER:
According to documentation, When QUOTED_IDENTIFIER is OFF, Literals can
be delimited by either single or double quotation marks. If a literal
string is delimited by double quotation marks, the string can contain
embedded single quotation marks, such as apostrophes.
You can use it as follows:
SET QUOTED_IDENTIFIER OFF;
Select * from table where column LIKE "%'%"
SET QUOTED_IDENTIFIER ON;
' has to be doubled:
Select * from tab where col LIKE '%''%'

sas proc sql escape ' (apostrophe)

Is it possible to put an apostrophe into the label when using proc sql?
proc sql;
create table palim
as select palim label='palim's palim'
from palim;
quit;
This will not work, because sas thinks the apostrohe is the end of the label.
I did not manage to get it to work with escape sas community question
This questions is specifically for proc sql, since using double quotes will not work here.
Either switch to using double quote character " to quote your string literal so that the character used on the outside is not contained in the string.
label="palim's palim"
Or double up any embedded quote characters that match the character used on the outside.
label='palim''s palim'
You can use either single or double quote characters to quote string literals in SAS. The only reason they would not work in PROC SQL would be if you added the dquote=ansi option to the proc sql statement. Then values inside double quotes are interpreted as names instead of string literals.
Use % to escape single quote.
proc sql;
create table palim
as select palim label='palim%'s palim'
from palim;
quit;
Can also uso double single quote.
proc sql;
create table palim
as select palim label='palim''s palim'
from palim;
quit;
The escape clause works LIKE expressions.

Update oracle table and change one character with another

I have this table where one of the Fields contains values like this ¤1¤. It is in an Unicode database and nvarchar2 Fields.
I then want to switch the ¤ With an ? and Write this line:
update table1 set col1 = REPLACE(col1,'¤','?');
commit;
The col1 is not updated.
What am I doing wrong?
select ascii('¤') from dual;
Even though this states 164 as the result on a non-unicode database, it doesn't so on a unicode database. Hence the replace as asked for, will not work.
select chr(164 USING NCHAR_CS) from dual;
Using this states '¤' as the result.
Hence the following replace should work.
select replace(col1, chr(164 USING NCHAR_CS), chr(63)) from table1;
Sometime simple cut and paste may not get the character properly in your query so .. instead of writing the character in your query.. get the ascii value (you can use ascii or dump function in oracle to get the ascii value of the character) of the character and then use that in your replace as below
Your character seems to me ASCII value 164 ...
-- try as below
update table1 set col1 = replace(col1,chr(164),'?');
commit;

Spooling sql to a CSV when data contains commas

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.

Query Help - String in where clause has & character

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')