Export data to CSV from SQL using batch file (My Workaround)

For this trick you will need to download sed (streams editor) for win32 more about sed here

To generate data from SQL you can use SQLCMD, for example below:

SQLCMD -S SERVER_NAME\SERVER_INSTANCE -d DB_NAME -U DB_USER -P DB_PASSWORD -Q "SET NOCOUNT ON;SELECT * FROM DATABASE ORDER BY COLUMN" -s "," -o "D:\YOUR_DIRECTORY\OUTPUT\OUTPUT_%AWSDT%_temp.csv"

In case you don’t know.

"SELECT * FROM DATABASE ORDER BY COLUMN"

is the query that we can to get the record from

SET NOCOUNT ON

is to omit message “xx rows affected” at the end

-s ","

to use comma “,” as column separator

and here’s where the magic happened.

sed -r "s/ +\t/\t/g" D:\YOUR_DIRECTORY\OUTPUT\OUTPUT_%AWSDT%_temp.csv | sed -r "s/\t +/\t/g" | sed -r "s/(^ +| +$)//g" | sed 2d > D:\YOUR_DIRECTORY\OUTPUT\OUTPUT_%AWSDT%.csv

This will clean up all the long spaces, remove the header separator
“————-,————–”
and it will gives the the clean look CSV file.

Below are the full sample:

@echo off
D:
CD D:\YOUR_DIRECTORY\

echo.
echo.Copy current format to temporary
reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f
echo.
echo.Modify date format
reg add "HKCU\Control Panel\International" /v sShortDate /d "dd/MM/yyy" /f
@REM reg query "HKCU\Control Panel\International" /v sShortDate
set LogDate=%date%

set yyyy=

set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))

if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100

set CurDate=%mm%/%dd%/%yyyy%
set dayCnt=%3

if "%dayCnt%"=="" set dayCnt=3

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31
set /A dd=31 + %dd%
goto CHKDAY

:SET30
set /A dd=30 + %dd%
goto CHKDAY

:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29

:SET28
set /A dd=28 + %dd%
goto CHKDAY

:SET29
set /A dd=29 + %dd%
goto CHKDAY

:DONE
if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

REM Set IIS and AWS date variables
set IISDT=%yyyy:~2,2%%mm%%dd%
set AWSDT=%yyyy%%mm%%dd%
rem set AWSDT=20150323

SQLCMD -S SERVER_NAME\SERVER_INSTANCE -d DB_NAME -U DB_USER -P DB_PASSWORD -Q "SET NOCOUNT ON;SELECT * FROM DATABASE ORDER BY COLUMN" -s "," -o "D:\YOUR_DIRECTORY\OUTPUT\OUTPUT_%AWSDT%_temp.csv"

sed -r "s/ +\t/\t/g" D:\YOUR_DIRECTORY\OUTPUT\OUTPUT_%AWSDT%_temp.csv | sed -r "s/\t +/\t/g" | sed -r "s/(^ +| +$)//g" | sed 2d > D:\YOUR_DIRECTORY\OUTPUT\OUTPUT_%AWSDT%.csv

del D:\YOUR_DIRECTORY\OUTPUT\OUTPUT_%AWSDT%_temp.csv

I hope you enjoy the tricks any questions let me know in comment section below.