Camillia
asked on
Can BCP command place data in specific positions in text file?
We have a stored proc that does this:
1. inserts data into a temp table
Is this doable?
1. inserts data into a temp table
insert into whatever table
select col1, col2, col3, .... from table1
2. creates a text fileselect
@FileName = '\\some-path-and-name-' + replace(replace(replace(convert(nvarchar(19),getdate(),120),'-',''),':',''),' ','') + '.txt'
3. Writes data to this file with pipe delimitedSET @bcpCommand = 'bcp ' +
'"SELECT * FROM whatevertable " queryout "'
SET @bcpCommand = @bcpCommand + @FileName + '" -T -c -t "|"'
EXEC master..xp_cmdshell @bcpCommand
I need to place the data in specific positions in the text file. For example, col1 goes to position 10 in the text file, col2 goes to position 14 and so on.Is this doable?
ASKER
No, you misunderstood my question.
As it is now...the BCP populates the text file with col1 | col2 | col3 |
I want Col1 to go to position 10 in the text file. Col2 go to position 14. Col3 position 25 and so now. No more pipe delimited. I want to be able to say...col1 goes to x position in the text file. Col5 goes to position 105 in the file and so on.
As it is now...the BCP populates the text file with col1 | col2 | col3 |
I want Col1 to go to position 10 in the text file. Col2 go to position 14. Col3 position 25 and so now. No more pipe delimited. I want to be able to say...col1 goes to x position in the text file. Col5 goes to position 105 in the file and so on.
Are you trying to do some kind of mail letters/mailmerge? personalized text files?
ASKER
EDI flat files that a customer wants and the data has to be in specific locations.
ASKER
I think I can have one column...insert the cols (each piece of data) into this one column and with spaces between each piece of data. Then BCP this one column into the text file.
For example:
Insert into whatevertable
select col1 + <some space> + col2 + <10 more spaces to get me to the correct position> + and so on
For example:
Insert into whatevertable
select col1 + <some space> + col2 + <10 more spaces to get me to the correct position> + and so on
I think what Hain is getting at is to read the data and then output it again. When outputting by adjusting the order of the select query, you are adjusting the order the data will be written.
You can do this without the database by reading one row at a time, perhaps placing it into an array, then rewriting the line in the new order but I think it would be easier to create a new file that way you keep the original. In other words, read line one into an array. Write the line with the new order array(5), array(0), array(10) new line and repeat.
You can do this without the database by reading one row at a time, perhaps placing it into an array, then rewriting the line in the new order but I think it would be easier to create a new file that way you keep the original. In other words, read line one into an array. Write the line with the new order array(5), array(0), array(10) new line and repeat.
ASKER
>>I think what Hain is getting at is to read the data and then output it again. When outputting by adjusting the order of the select query, you are adjusting the order the data will be written.
Order is not the issue. Space between each col is what I'm trying to solve for.
I think what I posted above should work. Not an ideal solution but that might work. https://www.experts-exchange.com/questions/29201868/Can-BCP-command-place-data-in-specific-positions-in-text-file.html#a43203507
Order is not the issue. Space between each col is what I'm trying to solve for.
I think what I posted above should work. Not an ideal solution but that might work. https://www.experts-exchange.com/questions/29201868/Can-BCP-command-place-data-in-specific-positions-in-text-file.html#a43203507
-t "|"
change this to
-t " "
to make it space
or better
-t "\t"
make it TAB character...
if you really need NOTHING, then you can use "select COELESCE(col1,col2,...) from ..."
instead of "select * from ..."
now, as I said before,
dont use "select *" use "select col2, col3, col1, col5, col24,..."
change this to
-t " "
to make it space
or better
-t "\t"
make it TAB character...
if you really need NOTHING, then you can use "select COELESCE(col1,col2,...) from ..."
instead of "select * from ..."
now, as I said before,
dont use "select *" use "select col2, col3, col1, col5, col24,..."
ASKER
Thanks, Hain. That won't work either. I think I need to do what I mentioned. It's not one space between each col data. The space can be 2 or 10 or 20.
https://www.experts-exchange.com/questions/29201868/Can-BCP-command-place-data-in-specific-positions-in-text-file.html#a43203507
https://www.experts-exchange.com/questions/29201868/Can-BCP-command-place-data-in-specific-positions-in-text-file.html#a43203507
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, sligghtwv. I hadn't thought of that. I'll try an example and see how it works.
ASKER
Thanks, Hain. I'll run a test tomorrow and see how this works.
it is basically, LPAD and RPAD which is missing from SQL server...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Let me take a look. Thanks
>>you can use CONCAT/LEFT/RIGHT/REPLICAT E
If they need right side padding, sure, use three functions when one will do:
left(concat('HainKurt', REPLICATE(' ',20)),20)
or
cast('HainKurt' as char(20))
If they need right side padding, sure, use three functions when one will do:
left(concat('HainKurt', REPLICATE(' ',20)),20)
or
cast('HainKurt' as char(20))
ASKER
I'll try tomorrow
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ha, no :). I'll try this and post back.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, Hain.
ASKER
I'll close this question and work on it. I'll post back if I need help.
SELECT * FROM whatevertable
>>>
SELECT ...col1,..., col2,... FROM whatevertable