Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 script results saving CSV file but has spaces!

Author  Topic 

racinghart
Starting Member

49 Posts

Posted - 2007-02-05 : 19:10:02
so i wrote this script and its pretty sweet (tara helped me before on this one) but now, for some STRANGE reason and its been reported by users the following:

the script for example dumps "first name, last name"

when we save the results as a CSV it looks good in notepad...

"first name, last name"

but we kept getting some kind of stange issues when we tried to dump that CSV data into another program to use... so i opened the CSV file in command prompt to see what it really looks like and it is actually "f i r s t n a m e , l a s t n a m e" with all these unnecessary spaces! why is it saving the CSV with spaces in it when my script is nothing of the sort? it was working fine last week, no changes were made to the script but now the CSV's are rendering those weird spaces.

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-02-05 : 19:20:46
Is the datatype of your source column CHAR, VARCHAR, NCHAR, or NVARCHAR?


CODO ERGO SUM
Go to Top of Page

racinghart
Starting Member

49 Posts

Posted - 2007-02-05 : 19:28:25
here is the actual script as you may see i did not specify datatype for all the data column fields... should i?

select '"'+Replace(name,'~','","')+'"' as name,
'"'+Convert(char(10), strtdate, 101)+'"' as hiredate,
'"'+Left(SSN,3)+'-'+Substring(SSN,4,2)+'-'+Substring(SSN,6,4)+'"' as ssn,
'"'+Left(empid,6)+'"' as employeeid,
'"'+Convert(char(10), birthdate, 101)+'"' as birthday,
'"'+Left(phone,3)+'-'+Substring(phone,4,3)+'-'+Substring(phone,7,4)+'"' as phone,
'"'+Rtrim(addr1)+' '+Rtrim(addr2)+'"' as address,
'"'+Rtrim(city)+'"' as city,
'"'+Rtrim(state)+'"' as state,
'"'+Left(zip,5)+'"' as zip,
'"'+Rtrim(department)+'"' as department,
'"'+ CASE User5 when 'M' then '0'
when 'F' then '1'
when 'NULL' then ' '
end +'"'
from employee
where status = 'A' and DATEDIFF("dd",lastpaiddate,GETDATE()) <= '14'
order by name
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-02-05 : 19:33:52
Is the datatype of your source column CHAR, VARCHAR, NCHAR, or NVARCHAR?


CODO ERGO SUM
Go to Top of Page

racinghart
Starting Member

49 Posts

Posted - 2007-02-05 : 19:41:53
CHAR. I just checked it
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-02-05 : 19:43:24
looks like the file is created as a unicode file, and your app doesn't understand that it's unicode.


www.elsasoft.org
Go to Top of Page

racinghart
Starting Member

49 Posts

Posted - 2007-02-05 : 19:56:42
is there any way i can get it to not save the file as a "unicode" any line of code to make sure its clean without spaces
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-02-05 : 22:43:47
I have no idea what you are using to create the file. I assume it's bcp. if so, you can use the -c flag to specify and ansi file.


www.elsasoft.org
Go to Top of Page

racinghart
Starting Member

49 Posts

Posted - 2007-02-06 : 13:43:14
i run the script in query analyzer... then at the bottom where the results are, i select all and choose the Save File as... and export it as a file_name.csv file... then when i open the csv file in command prompt using "edit" mode just to look at what is inside... the results have spaces between each letter... but then when i open the same file in notepad in windows, it looks perfectly fine. not sure if that makes sense
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-02-06 : 13:54:54
Personally, I'd do this...wth some modifications

http://weblogs.sqlteam.com/brettk/archive/2005/04/13/4395.aspx

But if you look at what you are doing, you are creating them as separate columns. I would change that to make it all 1 concatenated column that is comma delimited...the file you must be opening must be fixed width

You should do something like

SELECT '"'+Col1+'"'
+',"'+Col2+'"'
+',"'+Col3+'"' AS SQL
FROM Table



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-02-06 : 13:57:42
then "edit" mode in the cmd prompt does not understand the file is unicode.

does it really matter though? most modern apps understand unicode. is something in your process really broken because of this?

if you care, you'll have to change your script to convert everything to char/varchar/text from nchar/nvarchar/ntext. again, I haven't seen your script so I can't help you with that. also, I doubt that it's really necessary to do this.


www.elsasoft.org
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-02-06 : 14:01:24
I'd just like to go on record that I helped with a small portion of the script. I would not have recommended exporting the data in this manner. Personally, I'd use bcp with the queryout option.

Tara Kizer
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-02-06 : 14:01:54
well, also, when you save the file in QA (bad form btw, non automated process...ect) What file format re you using?

There's ANSI, UNICODE and OEM (whatever that is)



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

racinghart
Starting Member

49 Posts

Posted - 2007-02-06 : 14:37:11
quote:
Originally posted by jezemine

then "edit" mode in the cmd prompt does not understand the file is unicode.

does it really matter though? most modern apps understand unicode. is something in your process really broken because of this?

if you care, you'll have to change your script to convert everything to char/varchar/text from nchar/nvarchar/ntext. again, I haven't seen your script so I can't help you with that. also, I doubt that it's really necessary to do this.


www.elsasoft.org




select '"'+Replace(name,'~','","')+'"' as name,
'"'+Convert(char(10), strtdate, 101)+'"' as hiredate,
'"'+Left(SSN,3)+'-'+Substring(SSN,4,2)+'-'+Substring(SSN,6,4)+'"' as ssn,
'"'+Left(empid,6)+'"' as employeeid,
'"'+Convert(char(10), birthdate, 101)+'"' as birthday,
'"'+Left(phone,3)+'-'+Substring(phone,4,3)+'-'+Substring(phone,7,4)+'"' as phone,
'"'+Rtrim(addr1)+' '+Rtrim(addr2)+'"' as address,
'"'+Rtrim(city)+'"' as city,
'"'+Rtrim(state)+'"' as state,
'"'+Left(zip,5)+'"' as zip,
'"'+Rtrim(department)+'"' as department,
'"'+ CASE User5 when 'M' then '0'
when 'F' then '1'
when 'NULL' then ' '
end +'"'
from employee
where status = 'A' and DATEDIFF("dd",lastpaiddate,GETDATE()) <= '14'
order by name
Go to Top of Page

racinghart
Starting Member

49 Posts

Posted - 2007-02-06 : 14:38:36
quote:
Originally posted by tkizer

I'd just like to go on record that I helped with a small portion of the script. I would not have recommended exporting the data in this manner. Personally, I'd use bcp with the queryout option.

Tara Kizer



tara... can you provide a but more information on how i can import the data correctly as a CSV as opposed to selecting the results and clicking Save File As CSV
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-02-06 : 14:41:23
With or without a header?

Did you read my link?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-02-06 : 14:42:57
First, you'll need to fix your query to just what you need. You certainly don't need all of those RTRIMs and quotes. Then you can do this:

bcp.exe "SELECT Column1, Column2, Column3 FROM DatabaseName.dbo.YourTable" queryout E:\SomeFile.csv -c -SyourServer -T -t, -r\r\n

bcp is a command line utility, so you run it from a cmd window.

I prefer to fully qualify my objects in the query rather than using more switches in the bcp command.

Tara Kizer
Go to Top of Page

racinghart
Starting Member

49 Posts

Posted - 2007-02-06 : 14:53:50
quote:
Originally posted by X002548

With or without a header?

Did you read my link?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam







i just caught up with the posts hehe... i am reading your link/page now...
Go to Top of Page

racinghart
Starting Member

49 Posts

Posted - 2007-02-06 : 16:31:11
quote:
Originally posted by jezemine

looks like the file is created as a unicode file, and your app doesn't understand that it's unicode.


www.elsasoft.org



taking what you said to consideration... i dumped the csv data and opened it with NOTEPAD and from that I chose SAVE FILE AS... and saved it over but made sure Encoding selection was on ANSI. I noticed it was being saved as UNICODE and that fixed it. The problem is, now we have to train the clerk an additional step of saving it as ANSI encoding after dumping the data to notepad. = / wonder if there is a sql code to just save the file as ANSI during the CSV file save. Thanks to everyone who helped, ive learned a great deal as usual but will put this concern aside for now.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-02-06 : 16:32:29
Just use bcp as mentioned in my previous post and you won't need to do this saving stuff. Plus you can automate bcp unlike the method you've chosen!

Tara Kizer
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-02-06 : 17:13:46
bcp? what's that?


www.elsasoft.org
Go to Top of Page
    Next Page

- Advertisement -