I'm using PowerShell to run a query that automatically creates a UTF8 text file. One of the requirements is that query the results are fixed-width and the results start at the first character. I have certain currency fields containing currency that are meeting the fixed-width requirement but the results do not start at the first character. I am trying to figure out how to use PS to a) make sure the results are a fixed width and b) start at the first character of the column.
cast(coalesce(SLCDPM.FBAL, '') as char(20)) as [BALANCE],
results dont start at character 1 of the field but is 20 characters long
however this
cast(coalesce(SLCDPM.FBAL, '') as varchar(20)) as [BALANCE]
OR
LTRIM(cast(coalesce(SLCDPM.FBAL, '') as varchar(20))) as [BALANCE]
results start at character 1 but fixed-width not correct.
Invoke-Sqlcmd -ServerInstance "ESTSVRSTD\MSSQLP901" -Database "C1PROD" -Query "SELECT distinct
cast(coalesce(ARECEIVABLE.FCUSTNO, ' ') as char(20)) as [CUSTNO],
cast(coalesce(CUSTOMER.fcompany, ' ') as char(255)) as [COMPANY],
cast(coalesce(CUSTOMER.FMSTREET, ' ') as char(240)) as [ADDRESS1],
cast(coalesce(CUSTOMER.FCITY, ' ') as char(50)) as [CITY],
cast(coalesce(CUSTOMER.fstate, ' ') as char(50)) as [STATE],
cast(coalesce(CUSTOMER.FZIP, ' ') as char(20)) as [ZIP],
cast(coalesce(CUSTOMER.FCRLIMIT, ' ') as char(20)) as [CRD_LIMIT],
replace(convert(varchar, FDLPAYDATE,101),'/','') as [LASTPAY],
cast(coalesce(CUSTOMER.FBAL, '') as char(40)) as [BALANCE],
cast(coalesce(CUSTOMER.FNPAYAMT, '') as char(20)) as [LASTAMT],
cast(coalesce(CUSTOMER.FANN_SALES, '') as char(20)) as [YTDSALES],
cast(coalesce('EAST SERVICE', ' ') as char(20)) as [BUSINESSUNIT]
FROM
CUSTOMER
right JOIN
ARECEIVABLE ON
CUSTOMER.fcustno = ARECEIVABLE.fcustno
LEFT JOIN
shmast ON ARECEIVABLE.fsono = shmast.fcsono AND ARECEIVABLE.fcustno = shmast.fcnumber
where FBAL > ' '
AND CUSTOMER.fcountry = 'United States of America'
ORDER BY CUSTNO" | ConvertTo-Csv -NoTypeInformation -Delimiter "`t" |
Select-Object -Skip 1 |
% { $_ -replace "`t`"", '' -replace '[\r?\n]', " " } | % {$_ -replace '"',""} | Out-File ("\\ESTSVRSTD\D\ARCUSTOMER.txt") -Force -Encoding ascii