SQLTeam.com | Weblogs | Forums

Powershell SQL query not properly exporting to CSV?

Hi there,

Context

I need to retrieve rows of data from a few columns within a table.

I need to automate this.

I've decided to use a batch file.

The command I've been running is.

osql -S .\sqlexpress -E -i "C:\Users\user.name\Desktop\eventdatetime24hr.sql" -o "C:\Users\user.name\Desktop\queryOut.csv"

It produces an output that looks like this:

As you can see, the output looks gross.

I need to feed it into another application, however, I can only do this is there is a separate cell for each column and row of data.

I don't know how to do this, can anyone please assist?

The columns are: eventdatetime, eventid, sNarrative

Thank you!

Hi

I have had lots of these issues in my past ...

I will look into it tomorrow

It's my bedtime .no access to my desktop

Any experts please help ..in the meantime

1 Like

If you're using PowerShell, you can SELECT the value and use Export-CSV cmdlet.

If PowerShell is just a wrapper around a batch file, I'd use "sqlcmd" like this: https://stackoverflow.com/questions/425379/how-to-export-data-as-csv-format-from-sql-server-using-sqlcmd

1 Like

Thanks everyone!

hi

i am also facing the same issues !!!

i tried a lot but could not find anything that would fix the issue
https://docs.microsoft.com/en-us/sql/tools/osql-utility?view=sql-server-2017