Solved

SQL comma

Posted on 2014-11-18
10
261 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
[X]
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
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 66

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 66

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 66

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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

690 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