I am running a job in MS SQL Server that outputs a text file with white space in between columns. What I'd like to do is specify a specific character sequence between each column as a delimiter.
For example, I'd like the output to look like:
Apple%%%red%%%fruit
banana%%%yellow%%%fruit
onion%%%White%%%veggie
In this example, %%% is the delimiter.
How can I do this?
Assuming that you are using the output file of the job step, and the output file is currently structured something like this:
---- --------
row1 somedata
row2 somedata
You could just concatenate the columns using '+' and fit the percent signs in as appropriate. So the job step definition would contain:
select column1 + '%%%' + column2 from table1;
And the output would look like:
---------------
row1%%%somedata
row2%%%somedata
This assumes that you are OK with concatenating each row of results into a single column. You will need to cast/convert non-character column values for this to work.
My guess is that you are looking for the T-SQL equivalent of Oracle's P/SQL command "set colsep" command. This command lets you alter the delimter of the output. TO make it semicolon, for example, you would call:
set colsep ";"
But in SQL Server... I see the way to do it.
Use the "bcp" utility and you can specify the delimiter and write to your file. He are instructions:
http://msdn.microsoft.com/en-us/library/ms162802.aspx
Look at the -t option to change the separator.
Related
I am doing a
select field1
from tablename
where field1 like '%xyz zrt bla bla trew%'
field1 is a clob column and between the 'xyz zrt bla bla trew' there might be new line characters like chr(10) or chr(13). So it might be 'xyz\r\n\rzrt bla bla\n\n trew' etc
These are being converted to one (or more spaces). So any spaces between words can be true spaces or one or more of those new line characters.
How do I take this into account in my LIKE?
I am using Oracle but if possible I would like to use something that works for SQL Server, etc.
The lazy solution (relying on regular expressions, which may or may not kill performance - which may or may not matter) would be something like this:
select field1
from tablename
where regexp_like(field1, 'xyz\s+zrt\s+bla\s+bla\s+trew')
For Oracle you can use INSTR(field1, chr(10)) > 0
For SQL Server you can use field1 LIKE '%' + CHAR(10) + '%'
or CHARINDEX(CHAR(10), field1) > 0
You can replace the \n and \r with spaces first then trim the spaces and then compare. That would always work and perhaps fast enough.
However for proper speeding up the search process you can store the trimming results in a designated column "field1_trim".
Depends on your needs - storing the trimming results into a temporary table might be enough and more balanced between space/speed solution.
For example: save the result of the following query into a temporary table and then run your query on it
select
regexp_replace('[[:space:]]+', chr(32))
field1_trim,
<some unique row id to map to original table>
from table1;
I have a requirement where I have a table REPLACE_Table. This table would have 2 columns: one would be Original_string and the other would be Replacement_String.
I have a cursor running on Item_master table. For each record, in the Item_description column, I need to scan for the Replace_Table/Original_string and replace it with Replace_Table/replacement_string.
For Example, if my Replace_Table has these 2 rows:
Original_string Replacement_String
--------------------------------------
LO ##
WO ()
If my first Item_Description is 'HELLO WORLD', then I should get the result as 'HEL## ()RLD'.
I cannot use recursive Replace function in SQL because I do not know the number of records in my REPLACE_Table. I cannot use XLATE because it is not character to character replacement.
Only solution I have in mind is to read the REPLACE_Table in a loop and keep replacing Item_Description column value using the REPLACE in SQL.
Is there any other good solution?
Ok, so you're dealing with outputting XML and you're concerned about special characters...
Personally, I'd look at using the CDATA section for any data which might contain special characters...
<name><![CDATA[Mike & Son's Auto]]></name>
Is handled by an XML parser just like
<name>Mike & Son's Auto</name>
would be.
Also consider looking at whatever tools you might be using for web services. Scott Klement's excellent open source HTTP API includes an http_EscapeXml() procedure already.
Failing that, consider using the XMLTEXT() function built into Db2 for i
myText = 'Mike & Son''s Auto';
exec SQL
values (XMLSERIALIZE(XMLTEXT(:myText)
as varchar(50)
excluding XMLDECLARATION
)) into :myXmlText;
Although XMLTEXT() only converts & and < from what I can see...
i have a table:
id | detail
1 | ddsffdfdf ;df, deef,"dgfgf",/dfdf/
when I did: insert into details values(1,'ddsffdfdf ;df, deef'); => got inserted properly
When I copied that inserted value from database to a file,the file had: 1 ddsffdfdf ;df, deef
Then I loaded the whole csv file to pgsql database,with values in the format: 1 ddsffdfdf ;df, deef
ERROR: invalid input syntax for integer: "1 ddsffdfdf ;df, deef is obtained. How to solve the problem?
CSVs need a delimiter that Postgres will recognize to break the text into respective fields. Your delimiter is a space, which is insufficient. Your CSV file should look more like:
1,"ddsffdfdf df, deef"
And your SQL should look like:
COPY details FROM 'filename' WITH CSV;
The WITH CSV is important because it tells Postgres to use a comma as the delimiter and parses your values based on that. Because your second field contains a comma, you want to enclose its value in quotes so that its comma is not mistaken for a delimiter.
To look at a good example of a properly formatted CSV file, you can output your current table:
COPY details TO '/your/filename.csv' WITH CSV;
I am using command line Hive. For example hive -e "SELECT * FROM my_db.my_table;"
It is currently returning what looks like tab separated values. Is it possible to specify which delimiter it should use? For example, can I make it return pipe separated values?
what i am done in my case, i fired a query like below.
INSERT OVERWRITE LOCAL DIRECTORY '/home/Desktop/test3'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
select * from stud_02
other solution would be
hive -e 'select * from stud_01 limit 10' | sed 's/[[:space:]]\+/,/g' >> /home/Desktop/test.csv
I am using the following command to output the result of an SQL query to a text file:
$sqlite3 my_db.sqlite "SELECT text FROM message;" > out.txt
This gives me output like this:
text for entry 1
text for entry 2
Unfortunately, this breaks down when the text contains a newline:
text for entry 1
text for
entry 2
How can I specify an output delimiter (which I know doesn't exist in the text) for SQLite to use when outputting the data so I can more easily parse the result? E.g.:
text for entry 1
=%=
text for
entry 2
=%=
text for entry 3
Try -separator option for this.
$sqlite3 -separator '=%=' my_db.sqlite "SELECT text FROM message;" > out.txt
Update 1
I quess this is because of '-list' default option. In order to turn this option off you need to change current mode.
This is a list of modes
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
-list Query results will be displayed with the separator (|, by
default) character between each field value. The default.
-separator separator
Set output field separator. Default is '|'.
Found this info here
I had the same question and there is a simpler solution. I found this at https://sqlite.org/cli.html :
.separator COL ?ROW? Change the column and row separators
For example:
sqlite> .separator | ,
sqlite> select * from example_table;
1|3,1|4,1|15,1|21,1|33,2|13,2|16,2|32,
Or with no column separator:
sqlite> .separator '' ,
sqlite> select * from example_table;
13,14,115,121,133,213,216,232,
Or, to answer the specific question posed above, this is all that is needed:
sqlite> .separator '' \r\n=%=\r\n
sqlite> select * from message;
text for entry 1
=%=
text for
entry 2
=%=
text for entry 3
=%=
In order to seperate columns, you would have to work with group_concat and a seperator.
Query evolution:
SELECT text FROM messages;
SELECT GROUP_CONCAT(text, "=%=") FROM messages;
SELECT GROUP_CONCAT(text, "\r\n=%=\r\n") FROM messages;
// to get rid of the concat comma, use replace OR change seperator
SELECT REPLACE(GROUP_CONCAT(text, "\r\n=%="), ',', '\r\n') FROM messages;
SQLFiddle
Alternative: Sqlite to CSV export (with custom seperator), then work with that.