Avatar of Sh M
Sh M
Flag for United States of America asked on

Sql/oracle query to handle comma and double quotes

Hi
I like to extract records from oracle db, by writing a awl query .
The query should check if a column has comma then put content of that column in a double quote else do not put it in double quote. This will be extracted to a comma delemited file, so all columns are separated by comma:

E.g:
A, gggg , "John , Silverado" , 12345

Thanks in advanced
Microsoft SQL ServerOracle Database

Avatar of undefined
Last Comment
Sh M

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
slightwv (䄆 Netminder)

Oracle is similar if you need it:
change CHARINDEX to INSTR and "+" to "||":

SELECT CASE 
   WHEN INSTR(your_value, ',') > 1 THEN '"' || your_value || '"' 
   ELSE your_value END as name_goes_here
FROM your_table

Open in new window

awking00

There is a slight difference. In sql server, the search is the first parameter and the field is the second.
Oracle -
where instr(field,',') > 0
SQL Server
where charindex(',', field) > 0
Jim Horn

awking00 is correct, and I edited my first comment to reflect this catch.  Thank you.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Jim Horn

Another option for you is if you're using this in an ETL application such as SSIS, there is the ability within SSIS to use string delimiters such as double-quote marks to make sure a comma in those columns doesn't result in an error when the downstream process consumes the file.
Vitor Montalvão

shmz, do you still need help with this question?
Sh M

ASKER
Thank you
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.