Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Export in txt from sql (in a pivot way) Expand / Collapse
Author
Message
Posted Friday, May 23, 2014 8:17 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, June 20, 2014 4:00 AM
Points: 49, Visits: 84
Hello everybody,

I have to export the records of a table in a .txt file but in a strange way (in a column). For example I have this table with 3 fields:

create table prova
(cod varchar(1),
des1 varchar(2),
des2 Varchar (2)
)

insert into prova values (1, 'A1', 'A2')
insert into prova values (2, 'B1', 'B2')


I need to have the output in this way:

1 cod 1
1 des1 A1
1 des2 A2
8 S
1 cod 2
1 des1 B1
1 des2 B2
8 S


So, I have to put '1' at the begining of every field and '8 S' at the end of every record.
Anyboody helps me to do this?

Thank you very much.
Post #1574051
Posted Friday, May 23, 2014 8:42 AM This worked for the OP Answer marked as solution


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:21 PM
Points: 13,083, Visits: 11,918
antonela (5/23/2014)
Hello everybody,

I have to export the records of a table in a .txt file but in a strange way (in a column). For example I have this table with 3 fields:

create table prova
(cod varchar(1),
des1 varchar(2),
des2 Varchar (2)
)

insert into prova values (1, 'A1', 'A2')
insert into prova values (2, 'B1', 'B2')


I need to have the output in this way:

1 cod 1
1 des1 A1
1 des2 A2
8 S
1 cod 2
1 des1 B1
1 des2 B2
8 S


So, I have to put '1' at the begining of every field and '8 S' at the end of every record.
Anyboody helps me to do this?

Thank you very much.


It is hard to figure out exactly what you want for output here. This should at least get you started.

select d.Val
from prova
cross apply (values('1 cod ' + Cod),('1 des1 ' + des1),('1 des2 ' + des2),('8 S')) d(Val)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1574069
Posted Friday, May 23, 2014 3:04 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, June 20, 2014 4:00 AM
Points: 49, Visits: 84
Thank you very much.
Post #1574250
Posted Monday, May 26, 2014 7:47 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, June 20, 2014 4:00 AM
Points: 49, Visits: 84
Do you know how can I do the same in sql server 2005?
This functions properly only in sql server 2008 and 2012.
Post #1574526
Posted Monday, May 26, 2014 8:23 AM This worked for the OP Answer marked as solution


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:59 PM
Points: 3,342, Visits: 7,213
The alternative could be the official unpivot.
SELECT Val
FROM (SELECT CAST( '1 cod ' + CAST( cod AS varchar(10)) AS varchar(20)) col1,
CAST( '1 des1' + des1 AS varchar(20)) col2,
CAST( '1 des2' + des2 AS varchar(20)) col3,
CAST( '8 S' AS varchar(20)) col4
FROM #prova) p
UNPIVOT(Val FOR Cols IN (col1, col2, col3, col4))u

Or you could use UNION ALL for 2000 and previous versions.



Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1574533
Posted Monday, May 26, 2014 10:41 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:55 PM
Points: 36,749, Visits: 31,197
antonela (5/26/2014)
Do you know how can I do the same in sql server 2005?
This functions properly only in sql server 2008 and 2012.


Heh... this is why you should always pick the correct forum to post your questions on. Because you posted on 2012 forum, people just naturally assume that your question is based on 2012 instead of 2005. Posting on the correct forum means that you'll get a compatible answer instead of having to wait for someone to rehash your code for an earilier version.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1574566
Posted Tuesday, May 27, 2014 3:44 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, June 20, 2014 4:00 AM
Points: 49, Visits: 84
You're right.
Unfortunately when I wrote the first post I didn't know the customer still uses 2005.
Post #1574720
Posted Tuesday, May 27, 2014 3:44 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, June 20, 2014 4:00 AM
Points: 49, Visits: 84
Thank you very much
Post #1574721
Posted Tuesday, May 27, 2014 8:00 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:55 PM
Points: 36,749, Visits: 31,197
antonela (5/27/2014)
You're right.
Unfortunately when I wrote the first post I didn't know the customer still uses 2005.


Ah... got it. Thanks for the feedback.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1574794
Posted Friday, June 13, 2014 3:56 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, June 20, 2014 4:00 AM
Points: 49, Visits: 84
For me is very important to have the right order in the table.
For the first record I must to have the value of the first field of my original table, in the last record I must to have '8 S' that indicates me I have the fix value I put (but for the last record).
Is it possible to put another field (count) :1, 2, 3 etc?

val count
1 cod 1 1
1 des1 A1 2
1 des2 A2 3
8 S 4
1 cod 2 5
1 des1 B1 6
1 des2 B2 7
8 S 8


When I have many records I am not sure I have the right sorting.
I insert the result of the query in a table and when I call "select top 100 percent * from table" I don't have '8 S' on the last record.


Post #1580423
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse