Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL comma

Posted on 2014-11-18
10
256 Views
Last Modified: 2014-11-18
Hello,
I use below script to export data, (and save to csv through an exporter program,
the Reference field has commas in it,
when I export , the fields with commas become separated, , I need to keep the field together.  
Example  Reference :  100sd675,PMExvtyr

      SELECT
          CONVERT(varchar,InvoiceNumber) AS [InvoiceNumber],
            CONVERT(Varchar,Reference) as [Reference]
      FROM FinalizedOrders
      WHERE  ....
0
Comment
Question by:W.E.B
10 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40451147
easiest option is to make a different character instead of a comma for separating the fields say !

     SELECT
           CONVERT(varchar,InvoiceNumber) AS [InvoiceNumber],
            '!'+ CONVERT(Varchar,Reference) as [Reference]
       FROM FinalizedOrders
       WHERE  ....
0
 

Author Comment

by:W.E.B
ID: 40451152
I can't,
this is a client entry.

I found my own solution though,
'"' + Cast(Reference as varchar(100)) + '"' AS [Reference] ,

thanks
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40451156
I'm afraid the above is correct:  If you have a comma-separated text file, then the values in it cannot have commas in it, as it will cause the ETL process that consumes that file to interpret the row as having more columns than is currently mapped, and fail.

Your choices:
Change the delimeter to something else, such as pipe (|) delimited file, as pipes are hardly every used in values, except for columns that contain email text where some people's signature blocks have pipes in them, such as Dave Swift  | Super Dude | Green Co.
Use the REPLACE function to either remove the commas, or replace them with some other value...
SELECT
   REPLACE(CONVERT(varchar,InvoiceNumber), ',', '') AS [InvoiceNumber],
   REPLACE(CONVERT(Varchar,Reference), ',', '')  as [Reference]
FROM FinalizedOrders
WHERE  ....

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:W.E.B
ID: 40451236
I've requested that this question be deleted for the following reason:

Found own sloution
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40451237
Objection.  Spell out what 'Found own solutions' means here, so we can accept that as the answer, so other people with similar issues can learn from this question.
0
 

Author Comment

by:W.E.B
ID: 40451254
SELECT
          CONVERT(varchar,InvoiceNumber) AS [InvoiceNumber],
'"' + Cast(Reference as varchar(100)) + '"' AS [Reference]
       FROM FinalizedOrders
       WHERE  ....
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40451265
Objection? Jim he spelled out what his solution was already!  Enclose the string in "     ". Normal for any CSV that has commas in it.
0
 

Author Comment

by:W.E.B
ID: 40451342
So , my own solution (Comment before Jim Comment)
'"' + Cast(Reference as varchar(100)) + '"' AS [Reference]

Is the same as Jim?

Jim says,
Your choices:
    Change the delimeter to something else, such as pipe (|) delimited file, as pipes are hardly every used in values, except for columns that contain email text where some people's signature blocks have pipes in them, such as Dave Swift  | Super Dude | Green Co.

Use the REPLACE function to either remove the commas, or replace them with some other value...

?
0
 

Author Comment

by:W.E.B
ID: 40451361
I will award the points to Jim, for his effort and time.

Thank you Jim.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40451445
We must have cross-posted the last comment, and my objection to the close request.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question