on ‎2016 Feb 27 3:50 PM
I have a cursor that adds a lot of fields (tab separated) from a select in a declared variable (declared as long varchar). Then I use xp_write_file to export it to a text file. My question is how I can quote all strings in the variable as I can do with ex. "output to"?
Request clarification before answering.
Rolle has asked in a comment:
This doesen't work:
UNLOAD select list(string(name,'\\x09'),char(09) order by column_number) from sa_describe_query('select * from #tmp') to 'c:\\temp\\test.txt' encoding 'windows-1252' delimited by '\\x09';
Everything is on the same row.
Well, this should unload the column headers, so it is expected to be only one row, isn't it?
Here's a sample with an unload from the system table sysusertype:
unload
select list('''' || name || '''', ',' order by column_number)
from sa_describe_query('select * from sys.sysusertype order by type_name')
to 'C:\\MyFile.txt' quotes off escapes off;
unload
select *
from sys.sysusertype
order by type_name
to 'C:\\MyFile.txt' quotes on append on;
Note the "manual" quoting of the first unload: I have used list with single quotes around the column names to build the following result (which is one single string value, not a sequence of strings):
'type_id','creator','domain_id','nulls','width','scale','type_name','default','check','base_type_str','extended_base_type_str'
Then I have used the QUOTES OFF and ESCAPES OFF clause to unload that as-is - the default QUOTES ON would double the single quotes and would insert a leading and trailing quote (as the result set is just one single value), as seen here:
'''type_id'',''creator'',''domain_id'',''nulls'',''width'',''scale'',''type_name'',''default'',''check'',''base_type_str'',''extended_base_type_str'''
ESCAPES OFF is necessary when using QUOTES OFF as otherwise the comma is replaced by its hexadecimal representation:
'type_id'\\x2c'creator'\\x2c'domain_id'\\x2c'nulls'\\x2c'width'\\x2c'scale'\\x2c'type_name'\\x2c'default'\\x2c'check'\\x2c'base_type_str'\\x2c'extended_base_type_str'
Finally, with QUOTES OFF ESCAPES OFF and the second UNLOAD appended it should give something like:
'type_id','creator','domain_id','nulls','width','scale','type_name','default','check','base_type_str','extended_base_type_str' 1,'type_id',1,'smallint','smallint',2,0,2,,,'ut',24,1,'SYS','ISYSUSERTYPE','type_id',0,0,, 2,'creator',21,'unsigned int','unsigned int',4,0,4,,,'ut',24,2,'SYS','ISYSUSERTYPE','creator',0,0,, 3,'domain_id',1,'smallint','smallint',2,0,2,,,'ut',24,3,'SYS','ISYSUSERTYPE','domain_id',0,0,,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
IMHO, that's not really strange but just different formatting. "0.175" and ".175" are the same decimal values so no data is lost.
FWIW, DBISQL itself displays the same value diffently on my box with German-locale:
select cast(0.175 as numeric)
return "0,175" in DBISQL (note the decimal comma) and ".175" in dbisqlc.
That being said, I do not know how to influence OUTPUT and/or UNLOAD to use a different format for decimal data. Of course you are free to choose your own format preference by converting the data to string within the query yourself.
Why would you expect "0,175"? That's not a valid numerical value within SQL, as SQL expects the point as decimal delimiter. You would not be able to load such data back into SQL Anywhere...
If you need to export data with German formatting rules (i.e. decimal comma, point as thousand separator), confine that old FAQ and my answer there:
With UNLOAD, I don't think so - unless you would explicitly cast decimal data to string and then replace the decimal point with a comma within the select list, such as
select replace(cast(myNumber as varchar), '.', ','), ...
Frankly, for more than a few columns, a function containing that logic (like the cited one) would be way more comprehensible IMHO.
AFAIK, as stated, no, as UNLOAD is primarily meant to unload data that can be re-loaded later.
What application do you use to import the unloaded data? Tools like Excel can usually be configured to recognize decimal point vs. decimal comma and should accept missing zeroes before the decimal separator without problems. (In contrast, the stored function I had built as mentioned in one of my comments above was used with Word as that does not have such import options by default.)
| User | Count |
|---|---|
| 12 | |
| 9 | |
| 7 | |
| 5 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.