Question : SQL 2005 Formatting help for Flat File

I need to return a 2 digit number into this field:

MONTH(DATEADD(m , -( @otherlong - 1 ) , Visit)) AS [Fiscal Period],

Right now, in my dataset I am returning a 1 digit number for periods 1 - 9. Instead of 1, I would need back 01. Essentially, I need the "0" in front of the digit.

I also need help on this field:

SUM(pvp.totalfee) AS [Amount],

This needs to be the LEFT 22 characters and if its not 22 characters, i need it space filled. If its $1159.76 for example, I need it to pull into my file as:

'1159.76               '

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
SET NOCOUNT ON

/********* Determine the beginning of the fiscal year ***************************/
DECLARE @otherlong INT
SELECT
   @otherlong = ISNULL(otherlong , 1)
FROM
   Medlists
WHERE
   tablename = 'fiscalyear'
/******************************************************************************/
SELECT
   '01' AS EntityCode,
   RIGHT(YEAR(DATEADD(m , -( @otherlong - 1 ) , Visit)) , 2) AS [Fiscal Year],
   MONTH(DATEADD(m , -( @otherlong - 1 ) , Visit)) AS [Fiscal Period],
   'GL' AS [Subsystem],
   'JRNENT' AS [Source Code],
   LEFT(ISNULL(doc.Ledger , '') + SPACE(10) , 10) AS [Dept No],
   LEFT(LEFT(ISNULL(fac.Ledger , '') , 4) + LEFT(ISNULL(fin.Ledger , '') , 4) + SPACE(10) , 10) AS [SubAccount No],
   SUM(pvp.totalfee) AS [Amount],
   'C' AS [Debit/Credit],
   'Centricity Entry' + SPACE(34) AS [Description],
   SPACE(6) AS [Proj Id],
   SPACE(16) AS [Filler]
FROM
   PatientVisit pv 
INNER JOIN DoctorFacility comp ON pv.CompanyId = comp.DoctorFacilityId 
INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId 
INNER JOIN DoctorFacility doc ON pv.DoctorId = doc.DoctorFacilityId 
LEFT OUTER JOIN Medlists fin ON pv.FinancialClassMId = fin.MedListsId 
INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 
INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId 
INNER JOIN Batch b ON pvp.BatchID = b.BatchID 
INNER JOIN PatientVisitProcsAgg pvpa ON pvp.PatientVisitProcsID = pvpa.PatientVisitProcsID 
LEFT OUTER JOIN Procedures p ON pvp.ProceduresID = p.ProceduresID 
LEFT JOIN MedLists Procs ON p.DepartmentMId = procs.MedListsId
WHERE
   b.entry >= ISNULL(NULL , '1/1/1900') AND
   b.entry < DATEADD(DAY , 1 , ISNULL(NULL , '1/1/3000')) AND
   --Filter on Facility
(
  (
    NULL IS NOT NULL AND
    pv.FacilityID IN ( NULL )
  ) OR
  ( NULL IS NULL )
) AND
   --Filter on Company
(
  (
    NULL IS NOT NULL AND
    pv.CompanyID IN ( NULL )
  ) OR
  ( NULL IS NULL )
) AND
   --Filter on Financial Class
(
  (
    NULL IS NOT NULL AND
    pv.FinancialClassMID IN ( NULL )
  ) OR
  ( NULL IS NULL )
)
GROUP BY
   pv.TicketNumber,
   pv.Visit,
   doc.Ledger,
   fac.Ledger,
   fin.Ledger
HAVING
   SUM(pvp.TotalFee) <> 0
ORDER BY
   pv.TicketNumber

Answer : SQL 2005 Formatting help for Flat File

LEFT( convert(varchar, DATEADD(m , -( @otherlong - 1 ) , Visit), 101), 2) AS [Fiscal Period]
...
LEFT( CONVERT(varchar, SUM(pvp.totalfee)) + SPACE(22), 22)
Random Solutions  
 
programming4us programming4us