Solved

SQL comma

Posted on 2014-11-18
10
248 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:Wass_QA
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:Wass_QA
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
 

Author Comment

by:Wass_QA
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Wass_QA
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:Wass_QA
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:Wass_QA
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now