Jump to content
Will76021

Export From SQL to flate file

Recommended Posts

I need to export data from SQL to a Flat File with no Headers.

Invoke-Sqlcmd

$dbname = "Sendsuite Live"

$AttachmentPath = "c:\\exportdata\BeckerAccountDataNew.txt"

$QueryFmt= @"

SELECT

dbo.CharPad (s.PurchaseOrderNumber,10,'LEFT',' ')

,dbo.CharPad (s.SalesOrderNumber,10,'RIGHT','0')

,dbo.CharPad (U.UserName ,6,'LEFT',' ')

,dbo.CharPad (P.Reference2,6,'LEFT',' ')

,dbo.CharPad (P.Reference2,6,'LEFT',' ')

,dbo.CharPad (U.UserName,6,'LEFT',' ')

,dbo.CharPad ('',1,'LEFT',' ')

,dbo.CharPad ('MESS',6,'LEFT',' ')

,dbo.CharPad (U.DepartmentName,4,'LEFT',' ')

,dbo.CharPad (P.Reference1,4,'LEFT',' ')

,dbo.CharPad ('||',8,'LEFT',' ')

,dbo.CharPad ('USD',3,'LEFT',' ')

,dbo.CharPad (REPLACE(CONVERT(VARCHAR(10), s.ShipmentDate, 1), '/', ''),6,'LEFT',' ')

,dbo.CharPad (REPLACE(CONVERT(VARCHAR(10), s.ShipmentDate, 1), '/', ''),6,'LEFT',' ')

,dbo.CharPad ('||',6,'LEFT',' ')

,dbo.CharPad (case when s.NoOfPieces IS NULL then '1' else s.NoOfPieces end,9,'LEFT',' ')

,dbo.CharPad (case when s.NoOfPieces IS NULL then '1' else s.NoOfPieces end,9,'LEFT',' ')

,dbo.CharPad (s.TotalCost,11,'LEFT',' ')

,dbo.CharPad ('',1,'LEFT',' ')

,dbo.CharPad ('||',6,'LEFT',' ')

,dbo.CharPad ('||',4,'LEFT',' ')

,dbo.CharPad ('||',20,'LEFT',' ')

,dbo.CharPad (P.Reference4,20,'LEFT',' ')

,dbo.CharPad ('||',2,'LEFT',' ')

,dbo.CharPad ('||',2,'LEFT',' ')

,dbo.CharPad ('||',2,'LEFT',' ')

FROM Shipments as s

left JOIN [Record Identifiers] R on R.ShipmentID = s.ID

left join Packages P on P.ShipmentID = s.ID

left join Users U on U.id = s.ShipForUserID

where P.HistoryStateID <> 4 AND s.ShipmentDate = CONVERT(Date, GETDATE()-3)

"@

Invoke-Sqlcmd -ServerInstance Beware99 -Database $dbname -Query $QueryFmt |Export-Csv $AttachmentPath -NoTypeInformation

This will export it but in a csv and with Headers any help would be apricated.

Share this post


Link to post
Share on other sites

Look at the documentation for whatever you're using, you're using a command called Export-Csv.  Look at the options for it.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...