in below qury remove /r/n and clean:
drop table #UpperTable10


===============================


// SalaryClasses.Resources.SC_129
private string _query = "\r\nDeclare @YearMonth int = #YearMonth;\r\nDeclare @SerialPay int = #SerialPay;\r\nWITH temp as\r\n(\r\nselect InsBaseTypeCode,Prc,Oprc,Cprc, ROW_NUMBER() OVER(Partition by InsBaseDetailTypes.InsBaseTypeCode  ORDER BY date desc) \r\nas rw from RuleSalaries as RuleSalaries\r\ninner join \r\nInsBaseDetailTypes as InsBaseDetailTypes on RuleSalaries.RuleSalaryTitleCode=InsBaseDetailTypes.RuleSalaryTitleCode\r\nwhere RuleSalaries.RuleSalaryTitleCode in (120,121,122,123,126)\r\n)\r\n\t\r\nSELECT\r\nYearMonth = YearMonth,\r\nMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n\tMastPaysView_EmpYear=Convert(nvarchar(8), (RIGHT(CONVERT(nvarchar(max),EYear+100),2)+'/'+RIGHT(CONVERT(nvarchar(max),EMonth+100),2)+'/'+'00')),\r\n\tEmpBaseDate=CASE WHEN LEN(EmpBaseDate)=8 THEN SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 1, 4)\r\n\t+'/'\r\n\t+  SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 5, 2)  \r\n\t+'/'\r\n\t+  SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 7, 2)  else EmpBaseDate end,\r\n    DeductOver30=DeductOver30,\r\n    SumDeductIncludeSanavatGhabl=0,\r\n    MzoodhengamkosooratbazneshastegiEmpBaz=MzoodhengamkosooratbazneshastegiEmpBaz,\r\n    AghsaatEnteghalHaghbime=AghsaatEnteghalHaghbime,\r\n    EmpBazSanavatGhbal=0,\r\n    OrgBazSanavatghabl=0,\r\n\tEmptype2 = Emptype2,\r\n\tJozBBand11=JozBBand11,\r\n\tTafavotTatbigh2=0,\r\n\tTafavotTatbigh3=0,\r\n    MostakhdemID=MostakhdemID,\r\n    SandoghChangeDate=SandoghChangeDate,\r\n\tSanavatGhairRasmi=' ',\r\n\tMastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n    Hokm = Hokm,\r\n    Marriage = Marriage,\r\n    Reshteh = Reshteh,\r\n    Madrak = Madrak,\r\n    Major = Major,\r\n    Gender = Gender,\r\n\tEmptype = Emptype,\r\n    SazPost = SazPost,\r\n    IsarStatus = IsarStatus,\r\n    IsarRelStatus = IsarRelStatus,\r\n    Rasteh = Rasteh,\r\n    Date = Date,\r\n    Employees_Name = Employees_Name,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    BirthDate =CASE WHEN LEN(BirthDate)=8 THEN SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 1, 4)\r\n\t+'/'\r\n\t+  SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 5, 2)  \r\n\t+'/'\r\n\t+  SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 7, 2)  else BirthDate end,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n\tPhoneNum=PhoneNum,\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp309=SayerEmp309,\r\n\tSayerEmp310=SayerEmp310,\r\n    SayerEmp313=SayerEmp313,\r\n\tPay51 = Pay51,\r\n    Pay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n    PayNegahdasht=0,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = SumPay,\r\n    Emp_Baz = Emp_Baz,\r\n    Org_Baz = Org_Baz,\r\n    MSumPay = MSumPay,\r\n    MEmpBaz = MEmpBaz,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95 = Code95,\r\n\tCode97 = Code97,\r\n    Code96 = Code96,\r\n    Code27 = Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp =SayerEmp,\r\n    SumKasrBaz = Mogarrari+Emp_Baz+Org_Baz+SayerOrg+SayerEmp+MEmpBaz+MOrgBaz-SayerEmp307+SayerEmp309+SayerEmp310+SayerEmp313,\r\n\tTrialDate = TrialDate,\r\n\tMastPaysView_InsTypeCode=MastPaysView_InsTypeCode\r\n\t\tINTO #UpperTable10\r\n\tFROM (\r\nSELECT\r\nYearMonth = YearMonth,\r\nMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n\tMastPaysView_EmpYear_Month= CASE WHEN (SELECT CHARINDEX('-',MastPaysView_EmpYear_Month ) AS a)- (SELECT CHARINDEX('\\',MastPaysView_EmpYear_Month ) AS a) =2 THEN  REPLACE(MastPaysView_EmpYear_Month, '\\', '0')\r\n\t\t\t\t\t\t\t\t\t WHEN (SELECT CHARINDEX('-',MastPaysView_EmpYear_Month ) AS a)- (SELECT CHARINDEX('\\',MastPaysView_EmpYear_Month ) AS a) =3 THEN  REPLACE(MastPaysView_EmpYear_Month, '\\', '')ELSE '00000' END,\r\n\tEmpBaseDate=CASE WHEN LEN(EmpBaseDate)=8 THEN REPLACE(EmpBaseDate,'/','')+'01' ELSE EmpBaseDate END\t,\r\n    DeductOver30=DeductOver30,\r\n    SumDeductIncludeSanavatGhabl=0,\r\n    MzoodhengamkosooratbazneshastegiEmpBaz=MzoodhengamkosooratbazneshastegiEmpBaz,\r\n    AghsaatEnteghalHaghbime=AghsaatEnteghalHaghbime,\r\n    EmpBazSanavatGhbal=0,\r\n    OrgBazSanavatghabl=0,\r\n\tJozBBand11=JozBBand11,\r\n\tTafavotTatbigh2=0,\r\n\tTafavotTatbigh3=0,\r\n    MostakhdemID=MostakhdemID,\r\n    SandoghChangeDate=SandoghChangeDate,\r\n\tSanavatGhairRasmi=' ',\r\n\tMastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n    Hokm = Hokm,\r\n\tEmptype2 = Emptype2,\r\n    Marriage = Marriage,\r\n    Reshteh = Reshteh,\r\n    Madrak = Madrak,\r\n    Major = Major,\r\n    Gender = Gender,\r\n\tEmptype = Emptype,\r\n    SazPost = SazPost,\r\n    IsarStatus = IsarStatus,\r\n    IsarRelStatus = IsarRelStatus,\r\n    Rasteh = Rasteh,\r\n    Date = Date,\r\n    Employees_Name = Employees_Name,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    BirthDate =CASE WHEN LEN(BirthDate)=8 THEN REPLACE(BirthDate,'/','')+'01' ELSE BirthDate END\t,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n\tPhoneNum=PhoneNum,\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp309=SayerEmp309,\r\n\tSayerEmp310=SayerEmp310,\r\n    SayerEmp313=SayerEmp313,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = SumPay,\r\n    Emp_Baz = Emp_Baz,\r\n    Org_Baz = Org_Baz,\r\n    MSumPay = MSumPay,\r\n    MEmpBaz = MEmpBaz,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95 = Code95,\r\n\tCode97 = Code97,\r\n    Code96 = Code96,\r\n    Code27 = Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp =SayerEmp,\r\n    SumKasrBaz = SumKasrBaz,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n\tMastPaysView_InsTypeCode=MastPaysView_InsTypeCode,\r\n\tTrialDate = TrialDate\r\n\r\n\tFROM\r\n\t\t(\r\nSELECT\r\nYearMonth = YearMonth,\r\nMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n\tMastPaysView_EmpYear_Month=CASE WHEN (SELECT CHARINDEX('\\',MastPaysView_EmpYear ) AS a)=4 AND (SELECT CHARINDEX('/',MastPaysView_EmpYear ) AS a)=2 THEN REPLACE(MastPaysView_EmpYear, '/', '0') \r\n\t\t\t\t\t\t\t\t\tWHEN (SELECT CHARINDEX('\\',MastPaysView_EmpYear ) AS a)=5 AND (SELECT CHARINDEX('/',MastPaysView_EmpYear ) AS a)=3 THEN REPLACE(MastPaysView_EmpYear, '/', '0')  \r\n\t\t\t\t\t\t\t\t\tWHEN (SELECT CHARINDEX('\\',MastPaysView_EmpYear ) AS a)=6 AND (SELECT CHARINDEX('/',MastPaysView_EmpYear ) AS a)=3 THEN REPLACE(MastPaysView_EmpYear, '/', '') \r\n\t\t\t\t\t\t\t\t\tWHEN (SELECT CHARINDEX('\\',MastPaysView_EmpYear ) AS a)=5 AND (SELECT CHARINDEX('/',MastPaysView_EmpYear ) AS a)=2 THEN REPLACE(MastPaysView_EmpYear, '/', '') ELSE'checkshavad' END,\r\n    DeductOver30=DeductOver30,\r\n\tMastPaysView_InsTypeCode=MastPaysView_InsTypeCode,\r\n    SumDeductIncludeSanavatGhabl=0,\r\n    MzoodhengamkosooratbazneshastegiEmpBaz=MzoodhengamkosooratbazneshastegiEmpBaz,\r\n    AghsaatEnteghalHaghbime=AghsaatEnteghalHaghbime,\r\n    EmpBazSanavatGhbal=0,\r\n    OrgBazSanavatghabl=0,\r\n\tJozBBand11=JozBBand11,\r\n\tTafavotTatbigh2=0,\r\n\tTafavotTatbigh3=0,\r\n\tEmptype2 = Emptype2,\r\n    MostakhdemID=CASE WHEN MostakhdemID<=1000000 OR (SUBSTRING(CONVERT(NVARCHAR(20), MostakhdemID),1,1) = SUBSTRING(CONVERT(NVARCHAR(20), MostakhdemID),6,1) AND SUBSTRING(CONVERT(NVARCHAR(20), MostakhdemID),1,1) = SUBSTRING(CONVERT(NVARCHAR(20), MostakhdemID),3,1) AND \r\n\t\t\t\t\t\t   SUBSTRING(CONVERT(NVARCHAR(20), MostakhdemID),1,1) = SUBSTRING(CONVERT(NVARCHAR(20), MostakhdemID),4,1) AND SUBSTRING(CONVERT(NVARCHAR(20), MostakhdemID),1,1) = SUBSTRING(CONVERT(NVARCHAR(20), MostakhdemID),5,1))\r\n\t\t\t\t\t\t   OR MostakhdemID=9999999999 OR MostakhdemID=999999999 OR MostakhdemID=99999999 OR MostakhdemID=9999999 \r\n\t\t\t\t\t\t   OR MostakhdemID=8888888888 OR MostakhdemID=888888888 OR MostakhdemID=88888888 OR MostakhdemID=8888888 \r\n\t\t\t\t\t\t   OR MostakhdemID=7777777777 OR MostakhdemID=777777777 OR MostakhdemID=77777777 OR MostakhdemID=7777777 \r\n\t\t\t\t\t\t   OR MostakhdemID=6666666666 OR MostakhdemID=666666666 OR MostakhdemID=66666666 OR MostakhdemID=6666666 \r\n\t\t\t\t\t\t   OR MostakhdemID=5555555555 OR MostakhdemID=555555555 OR MostakhdemID=55555555 OR MostakhdemID=5555555 \r\n\t\t\t\t\t\t   OR MostakhdemID=4444444444 OR MostakhdemID=444444444 OR MostakhdemID=44444444 OR MostakhdemID=4444444 \r\n\t\t\t\t\t\t   OR MostakhdemID=3333333333 OR MostakhdemID=333333333 OR MostakhdemID=33333333 OR MostakhdemID=3333333 \r\n\t\t\t\t\t\t   OR MostakhdemID=2222222222 OR MostakhdemID=222222222 OR MostakhdemID=22222222 OR MostakhdemID=2222222 \r\n\t\t\t\t\t\t   OR MostakhdemID=1111111111 OR MostakhdemID=111111111 OR MostakhdemID=11111111 OR MostakhdemID=1111111 \r\n\t\t\t\t\t\t   OR MostakhdemID=123456789 OR MostakhdemID=12345678 OR MostakhdemID=1234567 \r\n\t\t\t\t\t\t   THEN\tEmployees_NationalCode ELSE MostakhdemID END,\r\n    SandoghChangeDate=SandoghChangeDate,\r\n\tSanavatGhairRasmi=' ',\r\n\tMastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n    Hokm = Hokm,\r\n    Marriage = Marriage,\r\n    Reshteh = Reshteh,\r\n    Madrak = Madrak,\r\n    Major = Major,\r\n    Gender = Gender,\r\n\tEmptype = Emptype,\r\n    SazPost = SazPost,\r\n    IsarStatus = IsarStatus,\r\n    IsarRelStatus = IsarRelStatus,\r\n    Rasteh = Rasteh,\r\n    Date = Date,\r\n    Employees_Name = Employees_Name,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    BirthDate =CASE WHEN SUBSTRING(BirthDate,1,2)='13' AND SUBSTRING(BirthDate,3,2)='13' THEN SUBSTRING(BirthDate,3,8) ELSE BirthDate END,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n    EmpBaseDate =CASE WHEN SUBSTRING(EmpBaseDate,1,2)='13' AND SUBSTRING(EmpBaseDate,3,2)='13' THEN SUBSTRING(EmpBaseDate,3,8) ELSE EmpBaseDate END,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n\tPhoneNum=PhoneNum,\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = SumPay,\r\n    Emp_Baz = Emp_Baz,\r\n    Org_Baz = Org_Baz,\r\n    MSumPay = MSumPay,\r\n    MEmpBaz = CASE WHEN (Pay1>0) THEN  round(((MSumPay)*tempPrc) /100,0)\r\n\t\t\t\t  ELSE round(((MSumPay-Mogarrari)*tempPrc) /100,0)\r\n\t\t\t\t  END ,\r\n    MOrgBaz =  CASE WHEN Pay1>0 THEN  round(((MSumPay)*tempCPrc) /100,0)\r\n\t\t\t\t  ELSE round(((MSumPay-Mogarrari)*tempCPrc) /100,0)\r\n\t\t\t\t  end,\r\n    Code95 = Code95,\r\n\tCode97 = Code97,\r\n    Code96 = Code96,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp309=SayerEmp309,\r\n\tSayerEmp310=SayerEmp310,\r\n    SayerEmp313=SayerEmp313,\r\n    Code27 = Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp =SayerEmp,\r\n    SumKasrBaz = SumKasrBaz,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n\tTrialDate = TrialDate\r\n\tFrom\r\n\t\t(\r\nSELECT\r\nYearMonth = YearMonth,\r\n\tMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n    DeductOver30=(-1)*SayerEmp307,\r\n    SumDeductIncludeSanavatGhabl=0,\r\n\tMastPaysView_InsTypeCode=MastPaysView_InsTypeCode,\r\n    MzoodhengamkosooratbazneshastegiEmpBaz=SayerEmp309+SayerEmp313,\r\n    AghsaatEnteghalHaghbime=SayerEmp310,\r\n    EmpBazSanavatGhbal=0,\r\n    OrgBazSanavatghabl=0,\r\n\tJozBBand11=JozBBand11,\r\n\tTafavotTatbigh2=0,\r\n\tTafavotTatbigh3=0,\r\n    MostakhdemID=MostakhdemID,\r\n\tEmptype2 = Emptype2,\r\n    SandoghChangeDate=case when Emptype2=3 then EmpBaseDate else N' ' end,\r\n\tSanavatGhairRasmi=' ',\r\n\tMastPaysView_InsDateUp30=case when SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_InsDateUp30), 1, 4)!='0' or SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_InsDateUp30), 1, 4) is null then(\r\n\tSUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_InsDateUp30), 1, 4)\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_InsDateUp30), 5, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_InsDateUp30), 5, 2) else SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_InsDateUp30), 5, 2) end\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_InsDateUp30), 7, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_InsDateUp30), 7, 2) else SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_InsDateUp30), 7, 2) end) else ' ' end,\r\n    Hokm = Hokm,\r\n    Marriage = Marriage,\r\n    Reshteh = Reshteh,\r\n    Madrak = CASE WHEN Madrak=N'' OR Madrak=N' ' OR Madrak IS NULL THEN N'ليسانس' ELSE Madrak END ,\r\n    Major = Major,\r\n    Gender = Gender,\r\n\tEmptype = Emptype,\r\n    SazPost = SazPost,\r\n    IsarStatus = CASE WHEN Emp_Baz=0 AND MastPaysView_InsTypeCode IN (24,26) THEN N'رزمنده با حداقل 24 ماه حضور در جبهه' ELSE IsarStatus END\t,\r\n    IsarRelStatus = IsarRelStatus,\r\n    Rasteh = Rasteh,\r\n    Date = case when Date<14000000 and Date>13000000 then Date-13000000 else Date end,\r\n    Employees_Name = Employees_Name,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    BirthDate =case when SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 1, 4)!='0' or SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 1, 4) is null then(\r\n\tSUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 1, 4)\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 5, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 5, 2) else SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 5, 2) end\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 7, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 7, 2) else SUBSTRING(CONVERT(NVARCHAR(20), BirthDate), 7, 2) end ) else ' ' end,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n\tDayEmpBaseDate=DayEmpBaseDate,\r\n\tDayYearMonth=DayYearMonth,\r\n    MastPaysView_EmpYear =CASE when DayEmpBaseDate!=0 THEN CONVERT(NVARCHAR(20), (DayYearMonth - DayEmpBaseDate)/365)+'/'+  CONVERT(NVARCHAR(20),((DayYearMonth - DayEmpBaseDate)%365)/30) +'\\'+  CONVERT(NVARCHAR(20),((DayYearMonth - DayEmpBaseDate)%365)%30)+'-'\r\n\t\t\t\t\t  WHEN Emptype2=3 then N' '\r\n\t\t\t\t\t  ELSE N' ' END,\r\n    EmpBaseDate = case when Emptype2=3 then N' ' else EmpBaseDate end,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = case when SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpDate2), 1, 4)!='0' or SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpDate2), 1, 4) is null then(\r\n\tSUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpDate2), 1, 4)\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpDate2), 5, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpDate2), 5, 2) else SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpDate2), 5, 2) end\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpDate2), 7, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpDate2), 7, 2) else SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpDate2), 7, 2) end ) else ' ' end,\r\n    MastPaysView_ChgDate = case when SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_ChgDate), 1, 4)!='0' or SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_ChgDate), 1, 4) is null then(\r\n\tSUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_ChgDate), 1, 4)\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_ChgDate), 5, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_ChgDate), 5, 2) else SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_ChgDate), 5, 2) end\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_ChgDate), 7, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_ChgDate), 7, 2) else SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_ChgDate), 7, 2) end ) else ' ' end,   \r\n    ExemptDate = case when SUBSTRING(CONVERT(NVARCHAR(20), ExemptDate), 1, 4)!='0' or SUBSTRING(CONVERT(NVARCHAR(20), ExemptDate), 1, 4) is null then(\r\n\tSUBSTRING(CONVERT(NVARCHAR(20), ExemptDate), 1, 4)\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), ExemptDate), 5, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), ExemptDate), 5, 2) else SUBSTRING(CONVERT(NVARCHAR(20), ExemptDate), 5, 2) end\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), ExemptDate), 7, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), ExemptDate), 7, 2) else SUBSTRING(CONVERT(NVARCHAR(20), ExemptDate), 7, 2) end ) else ' ' end,\r\n    HokmDate = case when SUBSTRING(CONVERT(NVARCHAR(20), HokmDate), 1, 4)!='0' or SUBSTRING(CONVERT(NVARCHAR(20), HokmDate), 1, 4) is null then(\r\n\tSUBSTRING(CONVERT(NVARCHAR(20), HokmDate), 1, 4)\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), HokmDate), 5, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), HokmDate), 5, 2) else SUBSTRING(CONVERT(NVARCHAR(20), HokmDate), 5, 2) end\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), HokmDate), 7, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), HokmDate), 7, 2) else SUBSTRING(CONVERT(NVARCHAR(20), HokmDate), 7, 2) end) else ' ' end,\r\n    MadrakEmp = MadrakEmp,   \r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n\tPhoneNum=N'09121111111',\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = SumPayWithOutMogharrari,\r\n    Emp_Baz = Emp_Baz,\r\n    Org_Baz = Org_Baz,\r\n    MSumPay = MSumPay,\r\n    MEmpBaz = MEmpBaz,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95 = Code95,\r\n\tCode97 = Code97,\r\n    Code96 = Code96,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp309=SayerEmp309,\r\n\tSayerEmp310=SayerEmp310,\r\n    SayerEmp313=SayerEmp313,\r\n    Code27 = Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp =SayerEmp,\r\n    SumKasrBaz = SumKasrBaz,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n    tempPrc=tempPrc,\r\n\ttempCPrc=tempCPrc,\r\n\tTrialDate = case when SUBSTRING(CONVERT(NVARCHAR(20), TrialDate), 1, 4)!='0' or SUBSTRING(CONVERT(NVARCHAR(20), TrialDate), 1, 4) is null then(\r\n\tSUBSTRING(CONVERT(NVARCHAR(20), TrialDate), 1, 4)\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), TrialDate), 5, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), TrialDate), 5, 2) else SUBSTRING(CONVERT(NVARCHAR(20), TrialDate), 5, 2) end\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), TrialDate), 7, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), TrialDate), 7, 2) else SUBSTRING(CONVERT(NVARCHAR(20), TrialDate), 7, 2) end)else ' ' end\r\n\tFrom\r\n\t\t(\r\nSELECT\r\nYearMonth = YearMonth,\r\nMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n\tMastPaysView_InsTypeCode=MastPaysView_InsTypeCode,\r\n    Hokm = Hokm,\r\n    Marriage = Marriage,\r\n    Reshteh = Reshteh,\r\n    Madrak = Madrak,\r\n    Major = Major,\r\n    Gender = Gender,\r\n\tMastPaysView_InsDateUp30,\r\n\tEmptype = Emptype,\r\n\tEmptype2=Emptype2,\r\n    SazPost = SazPost,\r\n    IsarStatus = IsarStatus,\r\n    IsarRelStatus = IsarRelStatus,\r\n    Rasteh = Rasteh,\r\n    Date = CASE WHEN Emp_Baz=0 AND TempCprc=13.5 AND (MastPaysView_InsTypeCode=4 OR MastPaysView_InsTypeCode=19 OR MastPaysView_InsTypeCode=28) AND  MastPaysView_InsDateUp30=0 THEN CONVERT(varchar(10), @YearMonth) +'01' ELSE Date end,\r\n    Employees_Name = Employees_Name,\r\n\tMostakhdemID=MostakhdemID,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    BirthDate = BirthDate,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n    DayYearMonth=case when SUBSTRING(CONVERT(NVARCHAR(20), @YearMonth), 1, 4)!='0' or SUBSTRING(CONVERT(NVARCHAR(20), @YearMonth), 1, 4) is null then(\r\n\tCONVERT(BIGINT,SUBSTRING(CONVERT(NVARCHAR(20), @YearMonth), 1, 4))*365\r\n\t+\r\n\tcase when CONVERT(NVARCHAR(20),(SUBSTRING(CONVERT(NVARCHAR(20), @YearMonth), 5, 2)))<10 then  CONVERT(bigint,SUBSTRING(CONVERT(NVARCHAR(20), @YearMonth), 5, 2))*30 else CONVERT(BIGINT,SUBSTRING(CONVERT(NVARCHAR(20), @YearMonth), 5, 2))*30 end\r\n\t+1\r\n\t) else 0 end,\r\n\tDayEmpBaseDate=case when SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 1, 4)!='0' or SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 1, 4) is null then(\r\n\tCONVERT(BIGINT,SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 1, 4))*365\r\n\t+\r\n\tcase when CONVERT(NVARCHAR(20),(SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 5, 2)))<10 then  CONVERT(bigint,SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 5, 2))*30 else CONVERT(BIGINT,SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 5, 2))*30 end\r\n\t+\r\n\tcase when CONVERT(NVARCHAR(20),(SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 7, 2)))<10 then  CONVERT(BIGINT,SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 7, 2)) else CONVERT(BIGINT,SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 7, 2))\r\n\tEND \r\n\t) else 0 end,\r\n    EmpBaseDate = case when SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 1, 4)!='0' or SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 1, 4) is null then(\r\n\tSUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 1, 4)\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 5, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 5, 2) else SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 5, 2) end\r\n\t+'/'\r\n\t+case when CONVERT(int,(SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 7, 2)))<10 then  SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 7, 2) else SUBSTRING(CONVERT(NVARCHAR(20), EmpBaseDate), 7, 2) end ) else ' ' end,\r\n    MastPaysView_IsarCode = case WHEN Emp_Baz=0 and TempCprc=22.5 and IsarEmp=0 then 2 else IsarEmp end,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_EmpYear = MastPaysView_EmpYear,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,   \r\n    ExemptDate = ExemptDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    IsarEmp = IsarEmp,   \r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = SumPayWithOutMogharrari,\r\n    Emp_Baz = CASE WHEN SumPayWithOutMogharrari=0 AND (Emp_Baz<=0 OR Org_Baz<0) THEN 0 \r\n\t\t\t\t   WHEN SumPayWithOutMogharrari=0 AND (Emp_Baz<0 OR Org_Baz<=0) THEN 0\r\n\t\t\t\t   WHEN\tMastPaysView_Wrktim=2 THEN ROUND((SumPay)*2*(tempPrc/100),0)\r\n\t\t\t\t   WHEN\tMastPaysView_Wrktim=3 THEN ROUND((SumPay)*2*(tempPrc/100),0)\r\n\t\t\t\t   WHEN\tMastPaysView_Wrktim=4 THEN ROUND(((SumPay)*(tempPrc/100)+((SumPayWithOutMogharrari/3)-(cast (Mogarrari AS FLOAT)/(cast (4 AS FLOAT)/cast (3 AS FLOAT))))*(0.225)),0)\r\n\t\t\t\t   WHEN\tMastPaysView_Wrktim=5 THEN ROUND((SumPay)*(cast (4 AS FLOAT)/cast (3 AS FLOAT))*(tempPrc/100),0)\r\n\t\t\t\t   ELSE Emp_Baz END,\r\n    Org_Baz = CASE WHEN SumPayWithOutMogharrari=0 AND (Emp_Baz<=0 OR Org_Baz<0) THEN 0\r\n\t\t\t\t   WHEN SumPayWithOutMogharrari=0 AND (Emp_Baz<0 OR Org_Baz<=0) THEN 0\r\n\t\t\t\t   WHEN\tMastPaysView_Wrktim=2 THEN ROUND(((SumPay)*2*(tempCPrc/100)),0)\r\n\t\t\t\t   WHEN\tMastPaysView_Wrktim=3 THEN ROUND(((SumPay)*2*(tempCPrc/100)),0)\r\n\t\t\t\t   WHEN\tMastPaysView_Wrktim=4 THEN ROUND((SumPay)*(tempCPrc/100),0)\r\n\t\t\t\t   WHEN\tMastPaysView_Wrktim=5 THEN ROUND(((SumPay)*(cast (4 AS FLOAT)/cast (3 AS FLOAT))*(tempCPrc/100)),0)\r\n\t\t\t\t   ELSE Org_Baz END, \r\n\tMSumPay=CASE WHEN ABS(MSumPay)<10 AND ABS(MSumPay)>0 THEN 0 ELSE MSumPay end   ,\r\n    MEmpBaz = MEmpBaz,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95 = Code95,\r\n\tCode97 = Code97,\r\n    Code96 = Code96,\r\n\tJozBBand11=JozBBand11,\r\n    Code27 = Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp = SayerEmp,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp309=SayerEmp309,\r\n\tSayerEmp310=SayerEmp310,\r\n    SayerEmp313=SayerEmp313,\r\n\tSayerEmp308=SayerEmp308,\r\n\tSayerEmp328=SayerEmp328,\r\n    SumKasrBaz = SumKasrBaz,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n\tTrialDate,\r\n    tempPrc=tempPrc,\r\n\ttempCPrc=tempCPrc,\r\n\tSumPayWithOutMogharrari=SumPayWithOutMogharrari\r\n\tFrom\r\n\t\t(\r\nSELECT\r\nMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n Hokm = Hokm,\r\n    Marriage = Marriage,\r\n    Reshteh = Reshteh,\r\n    Madrak = Madrak,\r\n    Major = Major,\r\n    Gender = Gender,\r\n\tEmptype = Emptype,\r\n\tEmptype2=Emptype2,\r\n    SazPost = SazPost,\r\n    IsarStatus = IsarStatus,\r\n    IsarRelStatus = IsarRelStatus,\r\n    Rasteh = Rasteh,\r\n    YearMonth = YearMonth,\r\n    Date = Date,\r\n    Employees_Name = Employees_Name,\r\n\tMostakhdemID=MostakhdemID,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    BirthDate = BirthDate,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n    EmpBaseDate = EmpBaseDate,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_EmpYear = CONVERT(NVARCHAR(20), MastPaysView_EmpYear),\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,\r\n    ExemptDate = ExemptDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    IsarEmp = IsarEmp,\r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = SumPay,\r\n    Emp_Baz = Emp_Baz,\r\n    Org_Baz = Org_Baz,\r\n    MSumPay = MSumPay,\r\n    MEmpBaz = CASE WHEN SumPayWithOutMogharrari=0 AND (Emp_Baz<=0 OR Org_Baz<0) THEN  round(((MSumPay-mogarrari)*tempPrc) /100,0)+Emp_Baz\r\n\t\t\t\t  WHEN SumPayWithOutMogharrari=0 AND (Emp_Baz<0 OR Org_Baz<=0) THEN  round(((MSumPay-mogarrari)*tempPrc) /100,0)+Emp_Baz\r\n\t\t\t\t  ELSE round(((MSumPay)*tempPrc) /100,0)\r\n\t\t\t\t  END ,\r\n    MOrgBaz = CASE WHEN SumPayWithOutMogharrari=0 AND (Emp_Baz<=0 OR Org_Baz<0) THEN  round(((MSumPay-mogarrari)*tempCPrc) /100,0)+Org_Baz\r\n\t\t\t\t  WHEN SumPayWithOutMogharrari=0 AND (Emp_Baz<0 OR Org_Baz<=0)  THEN  round(((MSumPay-mogarrari)* tempCPrc) /100,0)+Org_Baz\r\n\t\t\t\t  ELSE round(((MSumPay)*tempCPrc) /100,0)\r\n\t\t\t\t  END  ,\r\n    Code95 = Code95,\r\n\tCode97 = Code97,\r\n    Code96 = Code96,\r\n\tJozBBand11=JozBBand11,\r\n    Code27 = Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp = SayerEmp,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp308=SayerEmp308,\r\n\tSayerEmp309=SayerEmp309,\r\n    SayerEmp313=SayerEmp313,\r\n\tSayerEmp310=SayerEmp310,\r\n\tSayerEmp328=SayerEmp328,\r\n    SumKasrBaz = SumKasrBaz,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n    MastPaysView_Wrktim = MastPaysView_Wrktim,\r\n    MastPaysView_InsTypeCode = MastPaysView_InsTypeCode,\r\n\tMastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n\tTrialDate,\r\n\ttempPrc=tempPrc,\r\n\ttempCPrc=tempCPrc,\r\n\tSumPayWithOutMogharrari=SumPayWithOutMogharrari\r\n\tFrom\r\n\t\t(\r\nSELECT\r\nMastPaysView_RegionCode=MastPaysView_RegionCode,\r\nHokm = Hokm,\r\n    Marriage = Marriage,\r\n    Reshteh = Reshteh,\r\n    Madrak = Madrak,\r\n    Major = Major,\r\n    Gender = Gender,\r\n\tEmptype = Emptype,\r\n\tEmptype2=Emptype2,\r\n    SazPost = SazPost,\r\n    IsarStatus = IsarStatus,\r\n    IsarRelStatus = IsarRelStatus,\r\n    Rasteh = Rasteh,\r\n    YearMonth = YearMonth,\r\n    Date = Date,\r\n    Employees_Name = Employees_Name,\r\n\tMostakhdemID=MostakhdemID,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    BirthDate = BirthDate,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n    EmpBaseDate = EmpBaseDate,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_EmpYear =case when MastPaysView_EmpYear<100000 and MastPaysView_EmpYear> 1 then replicate('0',1)+cast(MastPaysView_EmpYear as NVARCHAR(20))    else cast(MastPaysView_EmpYear as NVARCHAR(20)) end,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate, \r\n    ExemptDate = ExemptDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    IsarEmp = IsarEmp,  \r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = SumPay,\r\n    Emp_Baz = Emp_Baz,\r\n    Org_Baz = Org_Baz,\r\n    MSumPay = CASE WHEN MastPaysView_Wrktim=3 THEN MSumPay*2\r\n                   WHEN MastPaysView_Wrktim=5 THEN Round(MSumPay*(cast (4 AS FLOAT)/cast (3 AS FLOAT)),0)\r\n                   ELSE MSumPay\r\n                   End,\r\n    MEmpBaz =MEmpBaz,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95 = Code95,\r\n\tCode97 = Code97,\r\n    Code96 = Code96,\r\n\tJozBBand11=JozBBand11,\r\n    Code27 = Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp = SayerEmp,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp308=SayerEmp308,\r\n\tSayerEmp309=SayerEmp309,\r\n    SayerEmp313=SayerEmp313,\r\n\tSayerEmp310=SayerEmp310,\r\n\tSayerEmp328=SayerEmp328,\r\n    SumKasrBaz = SumKasrBaz,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n    MastPaysView_Wrktim = MastPaysView_Wrktim,\r\n\tMastPaysView_InsTypeCode = MastPaysView_InsTypeCode,\r\n\tMastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n\tTrialDate,\r\n\ttempPrc=tempPrc,\r\n\ttempCPrc=tempCPrc,\r\n\tSumPayWithOutMogharrari=SumPayWithOutMogharrari\r\n\tFrom\r\n\t\t(\r\nSELECT\r\nMastPaysView_RegionCode=MastPaysView_RegionCode,\r\nHokm = Hokm,\r\n    Marriage = Marriage,\r\n    Reshteh = Reshteh,\r\n    Madrak = Madrak,\r\n    Major = Major,\r\n    Gender = Gender,\r\n\tEmptype = Emptype,\r\n\tEmptype2=Emptype2,\r\n    SazPost = SazPost,\r\n    IsarStatus = IsarStatus,\r\n    IsarRelStatus = IsarRelStatus,\r\n    Rasteh = Rasteh,\r\n    YearMonth = YearMonth,\r\n    Date = Date,\r\n    Employees_Name = Employees_Name,\r\n\tMostakhdemID=MostakhdemID,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    BirthDate = BirthDate,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n    EmpBaseDate = EmpBaseDate,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_EmpYear = CASE WHEN (MastPaysView_Sazpost=14565 OR MastPaysView_Sazpost=13699 OR MastPaysView_Sazpost=13700 OR MastPaysView_Sazpost=13701 OR MastPaysView_Sazpost=5892 OR MastPaysView_Sazpost=147914 OR MastPaysView_Sazpost=10000844) AND EYear=0 THEN 1 else  EYear+EMonth+'00' end,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,\r\n    ExemptDate = ExemptDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    IsarEmp = IsarEmp,\r\n    InsType = InsType,\r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = SumPay,\r\n    Emp_Baz =Emp_Baz,\r\n    Org_Baz =Org_Baz,\t\r\n    MSumPay = CASE WHEN SumPayWithOutMogharrari=0 AND (Emp_Baz<=0 OR Org_Baz<0) THEN Pay800+PayBackValUnder100-SayerEmp\r\n\t\t\t\t   WHEN SumPayWithOutMogharrari=0 AND (Emp_Baz<0 OR Org_Baz<=0) THEN Pay800+PayBackValUnder100-SayerEmp\r\n\t\t\t\t   ELSE Pay800+PayBackValUnder100 END ,\r\n    MEmpBaz = MEmpBaz,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95 = Code95,\r\n\tCode97 = Code97,\r\n    Code96 = Code96,\r\n\tJozBBand11=JozBBand11,\r\n    Code27 = Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp = SayerEmp,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp308=SayerEmp308,\r\n\tSayerEmp309=SayerEmp309,\r\n    SayerEmp313=SayerEmp313,\r\n\tSayerEmp310=SayerEmp310,\r\n\tSayerEmp328=SayerEmp328,\r\n    SumKasrBaz = SumKasrBaz,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n    MastPaysView_Wrktim = MastPaysView_Wrktim,\r\n\t MastPaysView_InsTypeCode = MastPaysView_InsTypeCode,\r\n\t MastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n\tTrialDate,\r\n\ttempPrc=tempPrc,\r\n\ttempCPrc=tempCPrc,\r\n\tSumPayWithOutMogharrari=SumPayWithOutMogharrari\r\n\tFrom\r\n\t\t(\r\nSELECT\r\nMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n    Hokm = Hokm,\r\n    Marriage = Marriage,\r\n    Reshteh = N'دبيري',\r\n    Madrak = Madrak,\r\n    Major = Major,\r\n    Gender = Gender,\r\n\tEmptype = Emptype,\r\n\tEmptype2=Emptype2,\r\n    SazPost = SazPost,\r\n    IsarStatus = IsarStatus,\r\n    IsarRelStatus = IsarRelStatus,\r\n    Rasteh = Rasteh,\r\n    YearMonth = YearMonth,\r\n    Date = Date,\r\n    Employees_Name = Employees_Name,\r\n\tMostakhdemID=MostakhdemID,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    BirthDate = BirthDate,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = Major,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n    EmpBaseDate = EmpBaseDate,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_EmpYear = MastPaysView_EmpYear,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,\r\n    ExemptDate = ExemptDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    IsarEmp = IsarEmp,\r\n    InsType = InsType,  \r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = SumPay,\r\n        Emp_Baz = CASE WHEN YearInsDateup30=@YearMonth AND  DaysOfInsDateUp30>=1  THEN ROUND((ROUND(((SumPay)*9)/100,0)/30)*(DaysOfInsDateUp30),0)\r\n\tELSE ROUND(((SumPay)*tempPrc)/100,0) END,\r\n    Org_Baz = Round(((SumPay)*tempCPrc)/100,0),\r\n    MSumPay =  MSumPay,\t\t   \r\n    MEmpBaz =MEmpBaz,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95=Code95,\r\n\tCode97=Code97,\r\n    Code96=Code96,\r\n\tJozBBand11=JozBBand11,\r\n    Code27=Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp = SayerEmp,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp308=SayerEmp308,\r\n\tSayerEmp309=SayerEmp309,\r\n    SayerEmp313=SayerEmp313,\r\n\tSayerEmp310=SayerEmp310,\r\n\tSayerEmp328=SayerEmp328,\r\n    SumKasrBaz = SumKasrBaz,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n    MastPaysView_Wrktim = MastPaysView_Wrktim,\r\n\tTrialDate,\r\n\t MastPaysView_InsTypeCode = MastPaysView_InsTypeCode,\r\n\t MastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n\ttempPrc= tempPrc ,\r\n\ttempCPrc=tempCPrc,\r\n\tPay800=Pay800,\r\n\tPayBackValUnder100=PayBackValUnder100,\r\n\tSumPayWithOutMogharrari=SumPayWithOutMogharrari\r\n\tFrom\r\n\t\t(\r\n\tselect \r\n\t\t*\r\n\tfrom(\r\n\tSELECT\r\n\tMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n\tYearInsDateup30=CAST(SUBSTRING(CONVERT(NVARCHAR(20),MastPaysView_InsDateUp30),1,6) AS INT),\r\n    DaysOfInsDateUp30=(CAST(SUBSTRING(CONVERT(NVARCHAR(20),MastPaysView_InsDateUp30),7,8) AS int)-1),\r\n\tHokm = IsNull((Select Description from SalaryHokmTypes where SalaryHokmTypeCode=OuterTable.MastPaysView_HokmTypeCode),' '),\r\n    Marriage =case when  MastPaysView_MarriageCode=5 and Payaaelemandi!=0  then N'متاهل' \r\n\t\t\t\t   when  MastPaysView_MarriageCode=5 and Payaaelemandi=0  then N'مجرد' \r\n                   when  MastPaysView_MarriageCode=4   then N'معيل' else\r\n\t\t\t\tIsNull((Select Description from Marriages where MarriageCode=OuterTable.MastPaysView_MarriageCode),' ')end,\r\n    Reshteh = IsNull((Select Description from JobBranchs where JobBranchCode=OuterTable.MastPaysView_Rshteh),' '),\r\n    Madrak =case WHEN  OuterTable.MadrakEmp=8 then N'سيکل' \r\n\t\t\t     WHEN  OuterTable.MadrakEmp=7 then N'ابتدايي' \r\n                 WHEN  OuterTable.MadrakEmp IN(0,1) then N'بيسواد' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=2 then N'خواندن ونوشتن' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp IN (3,4,5) THEN N'نهضت سوادآموزي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=7 then N'بيسواد' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp IN(18,19,20,21) then N'دكترا' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=25 THEN N'معادل دكترا' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=26 then N'دوره اول سطح' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=27 then N'دوره دوم سطح' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=28 then N'خارج' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=29 then N'اجتهاد' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=30 then N'درجه 1هنرمندي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=31 then N'درجه 2هنرمندي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=32 then N'درجه 3هنرمندي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=33 then N'درجه 4هنرمندي'\r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=34 then N'درجه 5هنرمندي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=35 then N'درجه 6هنرمندي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=36 then N'درجه 7هنرمندي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp IN( 50,51,52,53,54,55) then N'ابتدايي'  \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=56 then N'اول دبيرستان'\r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=57 then N'دوم دبيرستان' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=58 then N'سوم دبيرستان' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=59 then N'چهارم دبيرستان' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=60 then N'پنجم دبيرستان' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=61 then N'اول راهنمائي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=62 then N'دوم راهنمائي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=66 then N'اول دبيرستان'\r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=67 then N'دوم دبيرستان' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=68 then N'سوم دبيرستان' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp IN(69,70) then N'دوره دوم تعليمات عمومي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=71 then N'اول دبيرستان' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=72 then N'دوم دبيرستان' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=73 then N'سوم دبيرستان'\r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=74 then N'دكترا' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=75 then N'کارشناسي' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=76 then N'کارشناسي ارشد' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=77 then N'کارداني' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=78 then N'تحصيلات قديم' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=90 then N'ديپلم افتخاري' \r\n\t\t\t\t WHEN  OuterTable.MadrakEmp=91 then N'فوق ديپلم'\r\n\tELSE IsNull((Select Description from Edus where EduCode=OuterTable.MadrakEmp),N'ليسانس') end ,\r\n    Major = IsNull((Select Description from EduSubs where EduSubCode=OuterTable.MastPaysView_EduSubCode and OuterTable.MastPaysView_EduSubCode!=0 ),N'رياضي'),\r\n    Gender = case OuterTable.Employees_Gender\r\n\twhen 0 then N'نامشخص'\r\n\twhen 1 then N'مرد'\r\n\twhen 2 then N'زن'\r\n\telse ' '\r\n\tend,\r\n    Emptype = IsNull((Select Description from EmpTypes where EmpTypeCode=OuterTable.ServiceType),' '),\r\n\tEmptype2 = IsNull((Select EmpTypeCode from EmpTypes where EmpTypeCode=OuterTable.ServiceType),' '),\r\n    SazPost = IsNull((Select Description from SazPosts where SazPostCode=OuterTable.MastPaysView_Sazpost),N'دبير'),\r\n    IsarStatus = case when OuterTable.IsarEmp IN (3,9,21,22,23,24,25) then N' '\r\n\t\t\t\t\t  when OuterTable.IsarEmp IN (1,5) then N'آزاده'\r\n\t\t\t\t\t  when OuterTable.IsarEmp IN (2,10) then N'جانباز'\r\n\t\t\t\t\t  when OuterTable.IsarEmp IN (4) then N'رزمنده'\r\n\t\t\t\t\t  when OuterTable.IsarEmp IN (6) then N'مفقودالاثر'\r\n\t\t\t\t\t  when OuterTable.IsarEmp IN (7,16,20) then N'جانباز - رزمنده - آزاده'\r\n\t\t\t\t\t  when OuterTable.IsarEmp IN (8) then N'شهيد'\r\n\t\t\t\t\t  when OuterTable.IsarEmp IN (11,14,18) then N'جانباز-آزاده'\r\n\t\t\t\t\t  when OuterTable.IsarEmp IN (13,17) then N'جانباز - رزمنده'\r\n\t\t\t\t\t  when OuterTable.IsarEmp IN (15,19) then N'رزمنده - آزاده'\r\n\r\n\tELSE \r\n\t\t\t  IsNull((Select Description from Isars where IsarCode=OuterTable.IsarEmp),' ') end,\r\n  IsarRelStatus =case when OuterTable.MastPaysView_IsarRelationCode IN (120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,141,142,143,144,145,146,162,164) then N' '\r\n\t\t\t\t\t  when OuterTable.MastPaysView_IsarRelationCode IN (147,148,149,150,151,152,153,154,155,156,157,158,159,160,163) then N'فرزند جانباز'\r\n\t\t\t\t\t  when OuterTable.MastPaysView_IsarRelationCode IN (135,136,137,140,165) then N'فرزند'\r\n\t\t\t\t\t  when OuterTable.MastPaysView_IsarRelationCode IN (101,161) then N'فرزند شهيد'\r\n\t\t\t\t\t  when OuterTable.MastPaysView_IsarRelationCode IN (107,108,109,110) then N'پدر شهيد'\r\n\t\t\t\t\t  when OuterTable.MastPaysView_IsarRelationCode IN (117,118,119,102) then N'همسر شهيد'\r\n\t\t\t\t\t  when OuterTable.MastPaysView_IsarRelationCode IN (103,104,105,106) then N'مادر شهيد'\r\n\t\t\t\t\t  when OuterTable.MastPaysView_IsarRelationCode IN (115) then N'برادر شهيد'\r\n\t\t\t\t\t  when OuterTable.MastPaysView_IsarRelationCode IN (116) then N'خواهر شهيد'\r\n\r\n\tELSE N' ' end,\r\n    Rasteh = case OuterTable.MastPaysView_Rasteh\r\n\twhen 0 then N''\r\n\twhen 1 then N'آموزشي وفرهنگي'\r\n\twhen 2 then N'اداري ومالي'\r\n\twhen 3 then N'امورواجتماعي'\r\n\twhen 4 then N'بهداشتي ودرماني'\r\n\twhen 5 then N'خدمات'\r\n\twhen 6 then N'کشاورزي'\r\n\twhen 7 then N'فني ومهندسي'\r\n\twhen 8 then N'فنآوري اطلاعات '\r\n\twhen 9 then N'هنرمندان'\r\n\twhen 88 then N'ساير'\r\n\twhen 100 then N'غيرقابل قبول'\r\n\telse ' '\r\n\tend,\r\n    YearMonth = YearMonth,\r\n    Date = OuterTable.Date,\r\n    Employees_Name = Employees_Name,\r\n\tMostakhdemID=MostakhdemID,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    BirthDate = BirthDate,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_MarriageCode = MastPaysView_MarriageCode,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n    EmpBaseDate = EmpBaseDate,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_EmpYear = MastPaysView_EmpYear,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,\r\n    ExemptDate = ExemptDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    IsarEmp = IsarEmp,\r\n    InsType = InsType,    \r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = case WHEN\tMastPaysView_Wrktim IN (2,3) THEN SumPay-(Mogarrari/2)\r\n\t\t\t\t  WHEN\tMastPaysView_Wrktim IN (4,5) THEN ROUND(SumPay-(cast (Mogarrari AS FLOAT)/(cast (4 AS FLOAT)/cast (3 AS FLOAT))),0)\r\n\t\t\t  ELSE SumPay-Mogarrari\r\n\t\t\t  END,\r\n    Emp_Baz = Emp_Baz,\r\n    Org_Baz = Org_Baz,\r\n    MSumPay =  MSumPay,\r\n    MEmpBaz =MEmpBaz,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95=Code95,\r\n\tCode97=Code97,\r\n    Code96=Code96,\r\n\tJozBBand11=JozBBand11,\r\n    Code27=Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp = SayerEmp+SayerEmp324+SayerEmp328+SayerEmp308,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp308=SayerEmp308,\r\n\tSayerEmp309=SayerEmp309,\r\n    SayerEmp313=SayerEmp313,\r\n\tSayerEmp310=SayerEmp310,\r\n\tSayerEmp328=SayerEmp328,\r\n\tPayaaelemandi=Payaaelemandi,\r\n    SumKasrBaz = SumKasrBaz,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n    MastPaysView_Wrktim = MastPaysView_Wrktim,\r\n\tTrialDate,\r\n\t MastPaysView_InsTypeCode = MastPaysView_InsTypeCode,\r\n\t MastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n\ttempPrc =temp.Prc,\r\n\ttempCPrc=temp.CPrc,\r\n\tPay800=Pay800,\r\n\tSumPayWithOutMogharrari=SumPayWithOutMogharrari,\r\n    MastPaysView_HokmTypeCode = MastPaysView_HokmTypeCode,\r\n\tPayBackValUnder100=PayBackValUnder100\r\nFrom\r\n    (\r\nSELECT\r\nMastPaysView_RegionCode,\r\n    BirthDate,\r\n    Date,\r\n    EmpBaseDate,\r\n    Employees_Birthplace,\r\n    Employees_FatherName,\r\n    Employees_Gender,\r\n    Employees_IdNo,\r\n    Employees_IssuePlace,\r\n    Employees_LastName,\r\n    Employees_Name,\r\n\tMostakhdemID,\r\n    Employees_NationalCode,\r\n    EmptypePosition,\r\n    ExemptDate,\r\n    HokmDate,\r\n    InsType,\r\n    IsarEmp,\r\n    MadrakEmp,\r\n    MastPaysView_ChgDate,\r\n    MastPaysView_ChildNo,\r\n    MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode,\r\n    MastPaysView_EmpDate2,\r\n    MastPaysView_EmpID,\r\n    MastPaysView_EmptypeCode_,\r\n    MastPaysView_EmpYear,\r\n    MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode,\r\n    MastPaysView_MarriageCode,\r\n    MastPaysView_Rasteh,\r\n    MastPaysView_Rshteh,\r\n    MastPaysView_Salcnd,\r\n    MastPaysView_Sazpost,\r\n    MastPaysView_Tbgh,\r\n    Military,\r\n    Rotbeh,\r\n    ServiceType,\r\n    YearMonth,\r\nPayBackValUnder100=Sum(PayBackValUnder100),\r\n\tPay800=SUM(Pay800),\r\n    Emp_Baz = Sum(Emp_Baz),\r\n    MEmpBaz = Sum(MEmpBaz),\r\n    Mogarrari = Sum(Mogarrari),\r\n    MOrgBaz = Sum(MOrgBaz),\r\n    Code95=Sum(Code95),\r\n\tCode97=Sum(Code97),\r\n    Code96=Sum(Code96),\r\n\tJozBBand11=Sum(JozBBand11),\r\n    Code27=Sum(Code27),\r\n    MSumPay = Sum(MSumPay),\r\n    Org_Baz = Sum(Org_Baz),\r\n    Pay1 = Sum(Pay1),\r\n    Pay10 = Sum(Pay10),\r\n    Pay11 = Sum(Pay11),\r\n    Pay12 = Sum(Pay12),\r\n    Pay13 = Sum(Pay13),\r\n\tPay51 = Sum(Pay51),\r\n\tPay18Rotbehbandi=Sum(Pay18Rotbehbandi),\r\n\tPay19Tarmim=sum(Pay19Tarmim),\r\n\tPay46 =SUM(Pay46),\r\n    Pay14 = Sum(Pay14),\r\n    Pay2 = Sum(Pay2),\r\n    Pay3 = Sum(Pay3),\r\n    Pay4 = Sum(Pay4),\r\n    Pay5 = Sum(Pay5),\r\n    Pay6 = Sum(Pay6),\r\n    Pay7 = Sum(Pay7),\r\n    Pay8 = Sum(Pay8),\r\n    Pay9 = Sum(Pay9),\r\n    Sayer = Sum(Sayer),\r\n    SayerEmp = Sum(SayerEmp),\r\n\tSayerEmp307=SUM(SayerEmp307),\r\n\tSayerEmp308=SUM(SayerEmp308),\r\n\tSayerEmp309=SUM(SayerEmp309),\r\n    SayerEmp313=SUM(SayerEmp313),\r\n\tSayerEmp310=SUM(SayerEmp310),\r\n\tSayerEmp328=Sum(SayerEmp328),\r\n\tPayaaelemandi=sum(Payaaelemandi),\r\n\tSayerEmp324=Sum(SayerEmp324),\r\n    SayerOrg = Sum(SayerOrg),\r\n    SumKasrBaz = Sum(SumKasrBaz),\r\n    SumPay = Sum(SumPay),\r\n\tSumPayWithOutMogharrari = Sum(SumPay),\r\n\tEYear,\r\n\tEMonth,\r\n    MastPaysView_Wrktim = MastPaysView_Wrktim,\r\n\t MastPaysView_InsTypeCode = MastPaysView_InsTypeCode,\r\n\t MastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n    MastPaysView_HokmTypeCode = MastPaysView_HokmTypeCode,\r\n\tTrialDate\r\nFrom\r\n    (\r\nSELECT\r\nMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n    BirthDate = BirthDate,\r\n    Date = Date,\r\n    EmpBaseDate = EmpBaseDate,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Name = Employees_Name,\r\n\tMostakhdemID=MostakhdemID,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    EmptypePosition = EmptypePosition,\r\n    ExemptDate = ExemptDate,\r\n    HokmDate = HokmDate,\r\n    InsType = InsType,\r\n    IsarEmp = IsarEmp,\r\n    MadrakEmp = MadrakEmp,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EmptypeCode_ = MastPaysView_EmptypeCode,\r\n    MastPaysView_EmpYear = MastPaysView_EmpYear,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_MarriageCode = MastPaysView_MarriageCode,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    Military = Military,\r\n    Rotbeh = Rotbeh,\r\n    ServiceType = ServiceType,\r\n    YearMonth = YearMonth,\r\n    Emp_Baz = Emp_Baz,\r\n    MEmpBaz = MEmpBaz,\r\n    Mogarrari = Mogarrari,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95=Code95,\r\n\tCode97=Code97,\r\n    Code96=Code96,\r\n\tJozBBand11=JozBBand11,\r\n    Code27=Code27,\r\n    MSumPay = MSumPay,\r\n\tPay800=Pay800,\r\nPayBackValUnder100=PayBackValUnder100,\r\n    Org_Baz = Org_Baz,\r\n    Pay1 = Pay1,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Sayer = Sayer,\r\n    SayerEmp = SayerEmp,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp308=SayerEmp308,\r\n\tSayerEmp309=SayerEmp309,\r\n    SayerEmp313=SayerEmp313,\r\n\tSayerEmp310=SayerEmp310,\r\n\tSayerEmp328=SayerEmp328,\r\n\tPayaaelemandi=Payaaelemandi,\r\n\tSayerEmp324=SayerEmp324,\r\n    SayerOrg = SayerOrg,\r\n    SumKasrBaz = SumKasrBaz,\r\n    SumPay = SumPay,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n    MastPaysView_Wrktim = MastPaysView_Wrktim,\r\n\t MastPaysView_InsTypeCode = MastPaysView_InsTypeCode,\r\n\t MastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n    MastPaysView_HokmTypeCode = MastPaysView_HokmTypeCode,\r\n\tTrialDate\r\nFrom\r\n    (\r\nSELECT\r\nMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n    Emp_Baz = Emp_Baz,\r\n    Employees_BirthDate = Employees_BirthDate,\r\n    Employees_Birthplace = Employees_Birthplace,\r\n    Employees_EmpID = Employees_EmpID,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Name = Employees_Name,\r\n\tMostakhdemID=MostakhdemID,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EmptypeCode = MastPaysView_EmptypeCode,\r\n    MastPaysView_HokmTypeCode = MastPaysView_HokmTypeCode,\r\n    MastPaysView_InsTypeCode = MastPaysView_InsTypeCode,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_MarriageCode = MastPaysView_MarriageCode,\r\n    MastPaysView_MilitaryStatusCode = MastPaysView_MilitaryStatusCode,\r\n    MastPaysView_Mostkhdm = MastPaysView_Mostkhdm,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n\tMastPaysView_Salcnd =\tMastPaysView_Wrktim,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_SerialPay = MastPaysView_SerialPay,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_WifeJobCode = MastPaysView_WifeJobCode,\r\n    MastPaysView_Wrktim = MastPaysView_Wrktim,\r\n    MastPaysView_YearMonth = MastPaysView_YearMonth,\r\n    MEmpBaz = MEmpBaz,\r\n    Mogarrari = Mogarrari,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95=Code95,\r\n\tCode97=Code97,\r\n    Code96=Code96,\r\n\tJozBBand11=JozBBand11,\r\n    Code27=Code27,\r\n\tPay800=Pay800,\r\n    PayBackValUnder100=PayBackValUnder100,\r\n    MSumPay = Pay800+PayBackValUnder100,\r\n    Org_Baz = Org_Baz,\r\n    Pay1 = Pay1,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n\tPay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n\tPay46 = Pay46,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Regions_OrganCode = Regions_OrganCode,\r\n    Sayer = Sayer,\r\n    SayerEmp = SayerEmp,\r\n\tSayerEmp307=SayerEmp307,\r\n\tSayerEmp308=SayerEmp308,\r\n\tSayerEmp309=SayerEmp309,\r\n    SayerEmp313=SayerEmp313,\r\n\tSayerEmp310=SayerEmp310,\r\n\tSayerEmp328 = SayerEmp328,\r\n\tPayaaelemandi=Payaaelemandi,\r\n\tSayerEmp324 = SayerEmp324*(-1),\r\n    SayerOrg = SayerOrg,\r\n    SumCurrentValues_BackDocVal = SumCurrentValues_BackDocVal,\r\n    SumCurrentValues_CodePayID = SumCurrentValues_CodePayID,\r\n    SumCurrentValues_DocVal = SumCurrentValues_DocVal,\r\n    SumKasrBaz = SumKasrBaz,\r\n    SumPay = SumPay,\r\n    EmptypePosition = EmptypePosition,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,\r\n    MastPaysView_EmpDate = MastPaysView_EmpDate,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_EmpYear = MastPaysView_EmpYear,\r\n    Military = Military,\r\n    MastPaysView_Grop = MastPaysView_Grop,\r\n    Date = Date,\r\n    YearMonth = YearMonth,\r\n    BirthDate = BirthDate,\r\n    Rotbeh = Rotbeh,\r\n    ExemptDate = ExemptDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    IsarEmp = IsarEmp,\r\n    EmpBaseDate = EmpBaseDate,\r\n    ServiceType = ServiceType,\r\n    InsType = InsType,\r\n    MogharariAddi = MogharariAddi,\r\n    MogharariJanbaz = MogharariJanbaz,\r\n    Pay14 = Pay14,\r\n\tEYear=EYear,\r\n\tEMonth=EMonth,\r\n\tMastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n\tTrialDate\r\nFrom\r\n    (\r\nSelect\r\n    *\r\nFrom\r\n    (\r\nSelect\r\n    *,\r\n    Sayer = SumPay-Pay1-Pay2-Pay3-Pay4-Pay5-Pay6-Pay7-Pay8-Pay9-Pay10-Pay11-Pay12-Pay51-Pay18Rotbehbandi-Pay19Tarmim-Pay46-Pay800-Code95-Code96-Code97,\r\n    SumKasrBaz = 0\r\nFrom\r\n    (\r\nSelect\r\n    *,\r\n    Mogarrari = MogharariAddi+MogharariJanbaz,\r\n    SumPay =Pay90 + Pay1+Pay2+Pay3+Pay4+Pay5+Pay6+Pay7+Pay8+Pay9+Pay10+Pay11+Pay12+Pay13+Pay51+Pay18Rotbehbandi+Pay19Tarmim+Code95+Code96+Code97+Code27+Pay46+Pay49,\r\n    Pay14 = Pay4+Pay5+Pay6 +Pay49\r\nFrom\r\n    (\r\nSelect\r\n    *,\r\nJozBBand11=0,\r\nPay90 = case\r\nwhen  (SumCurrentValues_CodePayID = 90) then SumCurrentValues_DocVal\r\nelse 0\r\nend,\r\n    MogharariAddi = case\r\n\twhen  ((SumCurrentValues_CodePayID = 910)or(SumCurrentValues_CodePayID = 911)or(SumCurrentValues_CodePayID = 995)or(SumCurrentValues_CodePayID = 942)) then (SumCurrentValues_DocVal+SumCurrentValues_BackDocVal)*(-1)\r\n\telse 0\r\n\tend,\r\n\tPay800 = case\r\nwhen(SumCurrentValues_CodePayID = 801 or SumCurrentValues_CodePayID = 802 or SumCurrentValues_CodePayID = 803 or\r\n\t SumCurrentValues_CodePayID = 804 or SumCurrentValues_CodePayID = 810 or --SumCurrentValues_CodePayID = 813 or\r\n\t SumCurrentValues_CodePayID = 821 or SumCurrentValues_CodePayID = 822 or SumCurrentValues_CodePayID = 824 or SumCurrentValues_CodePayID = 846 or\r\n\t SumCurrentValues_CodePayID = 849 or --SumCurrentValues_CodePayID = 850 or\r\n     SumCurrentValues_CodePayID = 825 or SumCurrentValues_CodePayID = 847 or SumCurrentValues_CodePayID = 855 or SumCurrentValues_CodePayID = 845 or\r\n     SumCurrentValues_CodePayID = 851 or SumCurrentValues_CodePayID = 857 or SumCurrentValues_CodePayID = 861 or SumCurrentValues_CodePayID = 863 or\r\n   \t SumCurrentValues_CodePayID = 866 or SumCurrentValues_CodePayID = 814 or SumCurrentValues_CodePayID = 868 or\r\n   \t SumCurrentValues_CodePayID = 886 or SumCurrentValues_CodePayID = 867 or SumCurrentValues_CodePayID = 887 or\r\n     SumCurrentValues_CodePayID = 888 OR SumCurrentValues_CodePayID = 877 OR SumCurrentValues_CodePayID = 848 or\r\n   \t SumCurrentValues_CodePayID = 891 or SumCurrentValues_CodePayID = 892 or SumCurrentValues_CodePayID = 893 or\r\n\t SumCurrentValues_CodePayID = 894 or SumCurrentValues_CodePayID = 895 or SumCurrentValues_CodePayID = 896 or \r\n     SumCurrentValues_CodePayID = 818 or SumCurrentValues_CodePayID = 819 ) \r\n\t then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n\tPayBackValUnder100 = case\r\nwhen(SumCurrentValues_CodePayID = 1  or  \r\n\t SumCurrentValues_CodePayID = 2  or  \r\n\t SumCurrentValues_CodePayID = 3  or \r\n     SumCurrentValues_CodePayID = 4  or \r\n     SumCurrentValues_CodePayID = 5  or \r\n   \t SumCurrentValues_CodePayID = 6  or \r\n   \t SumCurrentValues_CodePayID = 10 or \r\n   \t --SumCurrentValues_CodePayID = 13 or \r\n\t SumCurrentValues_CodePayID = 14 or  \r\n     SumCurrentValues_CodePayID = 16 or  \r\n\t SumCurrentValues_CodePayID = 18 or \r\n\t SumCurrentValues_CodePayID = 19 or \r\n\t SumCurrentValues_CodePayID = 21 or  \r\n\t SumCurrentValues_CodePayID = 22 or \r\n     SumCurrentValues_CodePayID = 24 or \r\n     SumCurrentValues_CodePayID = 25 or \r\n   \t SumCurrentValues_CodePayID = 26 or \r\n   \t SumCurrentValues_CodePayID = 30 or \r\n\t SumCurrentValues_CodePayID = 45 or \r\n     SumCurrentValues_CodePayID = 47 or \r\n\t SumCurrentValues_CodePayID = 46 or \r\n   \t SumCurrentValues_CodePayID = 48 or \r\n\t SumCurrentValues_CodePayID = 49 or\r\n     --SumCurrentValues_CodePayID = 50 or \r\n\t SumCurrentValues_CodePayID = 51 or \r\n\t SumCurrentValues_CodePayID = 53 or  \r\n\t SumCurrentValues_CodePayID = 54 or \r\n     SumCurrentValues_CodePayID = 57 or \r\n     SumCurrentValues_CodePayID = 59 or \r\n   \t SumCurrentValues_CodePayID = 60 or \r\n   \t SumCurrentValues_CodePayID = 61 or \r\n   \t SumCurrentValues_CodePayID = 62 or \r\n\t SumCurrentValues_CodePayID = 63 or\r\n     SumCurrentValues_CodePayID = 65 or\r\n     SumCurrentValues_CodePayID = 66 or\r\n     SumCurrentValues_CodePayID = 67 or\r\n     SumCurrentValues_CodePayID = 76 or\r\n     SumCurrentValues_CodePayID = 77 or\r\n     SumCurrentValues_CodePayID = 89 or\r\n     SumCurrentValues_CodePayID = 91 or\r\n\t SumCurrentValues_CodePayID = 92 or\r\n     SumCurrentValues_CodePayID = 95 or\r\n     SumCurrentValues_CodePayID = 96 \r\n   )\r\n\t then SumCurrentValues_BackDocVal\r\n\telse 0\r\n\tend,\r\n     MOrgBaz =0, \r\n    Pay1 = case\r\n\twhen  (SumCurrentValues_CodePayID = 1 or SumCurrentValues_CodePayID = 48 or SumCurrentValues_CodePayID = 86    ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    Org_Baz = 0,\r\n\tIsarEmp=MastPaysView_IsarCode,\r\n    Pay6 = case\r\n\twhen  (SumCurrentValues_CodePayID = 93 or SumCurrentValues_CodePayID = 893) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    Military = case\r\n\twhen  (Employees_Gender = 1 and MastPaysView_MilitaryStatusCode=1) then N'پايان خدمت'\r\n\twhen  (Employees_Gender = 1 and MastPaysView_MilitaryStatusCode not in (1,2,3,4,6,7,8,9,10,11)) then N'پايان خدمت'\r\n\twhen  (Employees_Gender = 1 and MastPaysView_MilitaryStatusCode in (2,3,4,6,7,8,9,10,11)) then N'معافيت'\r\n\twhen  (Employees_Gender != 1 ) then N' '\r\n\telse N' '\r\n\tend,\r\n    MogharariJanbaz = case\r\n\twhen  (SumCurrentValues_CodePayID = 912) then (SumCurrentValues_DocVal+SumCurrentValues_BackDocVal)\r\n\telse 0\r\n\tend,\r\n    SayerOrg = 0,\r\n    Date = CASE WHEN (MastPaysView_InsTypeCode=4 OR MastPaysView_InsTypeCode=19 OR MastPaysView_InsTypeCode=28) AND  MastPaysView_InsDateUp30=0 THEN CONVERT(varchar(10), @YearMonth) +'01' ELSE MastPaysView_InsDateUp30 end,\r\n    YearMonth = @YearMonth,\r\n    MadrakEmp = case\r\n\twhen  (MastPaysView_EduCode > 18) then 16\r\n\telse MastPaysView_EduCode\r\n\tend,\r\n    ExemptDate = case\r\n\twhen  (MastPaysView_InsTypeCode = 4 OR MastPaysView_InsTypeCode = 28) then ((MastPaysView_YearMonth)*100)+1\r\n\telse Null\r\n\tend,\r\n    MEmpBaz = 0,\r\n    SayerEmp = case\r\n\twhen  SumCurrentValues_CodePayID=902 or SumCurrentValues_CodePayID=150 then (ABS(SumCurrentValues_DocVal)+ABS(SumCurrentValues_BackDocVal))\r\n\telse 0\r\n\tend,\r\n\tPayaaelemandi = case -- just for marriage\r\n\twhen SumCurrentValues_CodePayID=12 or SumCurrentValues_CodePayID=11 then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n\tSayerEmp307 = case\r\n\twhen SumCurrentValues_CodePayID=307 then (ABS(SumCurrentValues_DocVal)+ABS(SumCurrentValues_BackDocVal))\r\n\telse 0\r\n\tend,\r\n\tSayerEmp308 = case\r\n\twhen SumCurrentValues_CodePayID=308 then (ABS(SumCurrentValues_DocVal)+ABS(SumCurrentValues_BackDocVal))\r\n\telse 0\r\n\tend,\r\n\tSayerEmp309 = case\r\n\twhen SumCurrentValues_CodePayID=309 then (ABS(SumCurrentValues_DocVal)+ABS(SumCurrentValues_BackDocVal))\r\n\telse 0\r\n\tend,\r\n    SayerEmp313 = case\r\n\twhen SumCurrentValues_CodePayID=313 then (ABS(SumCurrentValues_DocVal)+ABS(SumCurrentValues_BackDocVal))\r\n\telse 0\r\n\tend,\r\n\tSayerEmp310 = case\r\n\twhen SumCurrentValues_CodePayID=310 then (ABS(SumCurrentValues_DocVal)+ABS(SumCurrentValues_BackDocVal))\r\n\telse 0\r\n\tend,\r\n\tSayerEmp328 = case\r\n\twhen SumCurrentValues_CodePayID=328 then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n\tSayerEmp324 = case\r\n\twhen SumCurrentValues_CodePayID=324  then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    Pay9 = case\r\n\twhen  (SumCurrentValues_CodePayID = 24 or SumCurrentValues_CodePayID = 94  ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    Pay2 = case\r\n\twhen  (SumCurrentValues_CodePayID = 21 or SumCurrentValues_CodePayID = 25 or SumCurrentValues_CodePayID = 57     or SumCurrentValues_CodePayID = 65     ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    ServiceType = case\r\n\twhen  (MastPaysView_EmptypeCode = 60 or MastPaysView_EmptypeCode = 14 or MastPaysView_EmptypeCode = 15 or MastPaysView_EmptypeCode = 16 or MastPaysView_EmptypeCode = 17 or MastPaysView_EmptypeCode = 19 ) then 1\r\n\telse MastPaysView_EmptypeCode\r\n\tend,\r\n    Pay5 = case\r\n\twhen  (SumCurrentValues_CodePayID = 92  ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    Pay4 = case\r\n\twhen  (SumCurrentValues_CodePayID = 3) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    BirthDate = Employees_BirthDate,\r\n    EmptypePosition = case\r\n\twhen  (MastPaysView_Wrktim = 0) then N'خالي'\r\n    when  (MastPaysView_Wrktim = 1) then N'شاغل'\r\n    when  (MastPaysView_Wrktim in(2,3,4,5)) then N'نيمه وقت تا سه سال'\r\n\telse '  '\r\n\tend,\r\n    EmpBaseDate = \r\n\tcase \r\n\t\t WHEN (MastPaysView_EmpDate <= 0) then MastPaysView_EmpDate2\r\n\t     WHEN (MastPaysView_EmpDate <= 0) AND MastPaysView_EmpDate2<=0 then MastPaysView_TrialDate\r\n\t\t WHEN (MastPaysView_EmpDate <= 0) AND MastPaysView_EmpDate2<=0 AND MastPaysView_TrialDate<=0 then MastPaysView_CrtDate\r\n\telse MastPaysView_EmpDate\r\n\tend,\r\n    Pay3 = case\r\n\twhen  (SumCurrentValues_CodePayID = 2 or SumCurrentValues_CodePayID = 88   ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    InsType = MastPaysView_InsTypeCode,\r\n    Emp_Baz = 0,\r\n    Pay8 = case\r\n\twhen  (Regions_OrganCode <> 310) and (SumCurrentValues_CodePayID = 87 or SumCurrentValues_CodePayID = 77  ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    Rotbeh = case\r\n\twhen MastPaysView_Rank=0 then N' '\r\n\twhen MastPaysView_Rank=1 then N'مقدماتي'\r\n\twhen MastPaysView_Rank=2 then N'پايه'\r\n\twhen MastPaysView_Rank=3 then N'ارشد'\r\n\twhen MastPaysView_Rank=4 then N'خبره'\r\n\twhen MastPaysView_Rank=5 then N'عالي'\r\n\telse ''\r\n\tend,\r\n    Pay12 = case\r\n\twhen  ( SumCurrentValues_CodePayID = 62 or SumCurrentValues_CodePayID = 66  or SumCurrentValues_CodePayID = 67    ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    Pay13 = case\r\n\twhen  (--SumCurrentValues_CodePayID = 13  or\r\nSumCurrentValues_CodePayID = 45 or SumCurrentValues_CodePayID = 47   ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n\tPay18Rotbehbandi = case\r\n\twhen  (SumCurrentValues_CodePayID = 18    ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n\tPay19Tarmim = case\r\n\twhen  (SumCurrentValues_CodePayID = 19    ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n\tPay51 = case\r\n\twhen  (SumCurrentValues_CodePayID = 51    ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n\tPay46 = case\r\n\twhen  (SumCurrentValues_CodePayID = 46    ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n\tPay49 = case\r\n\twhen  (SumCurrentValues_CodePayID = 49 --or SumCurrentValues_CodePayID = 50  \r\n) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    Pay10 = case\r\n\twhen  (SumCurrentValues_CodePayID=4)    then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    Pay11 = case\r\n\twhen  (SumCurrentValues_CodePayID = 10  or SumCurrentValues_CodePayID = 22   or SumCurrentValues_CodePayID = 63  or   SumCurrentValues_CodePayID = 61  ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n    Pay7 = case\r\n\twhen  (SumCurrentValues_CodePayID = 55 or SumCurrentValues_CodePayID = 91  ) then SumCurrentValues_DocVal\r\n\telse 0\r\n\tend,\r\n\r\n    Code95= case\r\n    when  (SumCurrentValues_CodePayID = 95) then SumCurrentValues_DocVal\r\n    else 0\r\n    end,\r\n\tCode97= 0,\r\n    Code96 = case\r\n    when  (SumCurrentValues_CodePayID = 96) then SumCurrentValues_DocVal\r\n    else 0\r\n    end,\r\n    Code27= case\r\n\twhen  (SumCurrentValues_CodePayID = 14) then SumCurrentValues_DocVal\r\n    else 0\r\n    end,\r\n       HokmDate = (select top 1 d.Efct from HokmDocs as d where  d.EmpID=Employees_EmpID AND d.YearMonth<=@YearMonth AND d.Efct<=((@YearMonth*100)+31) AND d.Stepno=0 order by d.YearMonth desc, d.SerialPay desc, d.serial desc),\r\n\tEYear=CASE\r\n    WHEN (MastPaysView_EmpYear/100)<10 THEN '0'+CONVERT(varchar(10), MastPaysView_EmpYear/100) \r\n\tELSE CONVERT(varchar(10), MastPaysView_EmpYear/100) \r\n\tEND,\r\n\tEMonth=CASE\r\n    WHEN (MastPaysView_EmpYear%100)>=0 AND (MastPaysView_EmpYear%100)<10 THEN '0'+CONVERT(varchar(10), MastPaysView_EmpYear%100) \r\n\tELSE CONVERT(varchar(10), MastPaysView_EmpYear%100) \r\n\tEND\r\nFrom\r\n    (\r\nSelect\r\n    MastPaysView_InsDateUp30=IsNull(MastPaysView.InsDateUp30,0),\r\n    Employees_EmpID=IsNull(Employees.EmpID,0),\r\n    Employees_LastName=IsNull(Employees.LastName,N' '),\r\n    Employees_Name=IsNull(Employees.Name,N' '),\r\n\tMostakhdemID=IsNull(MastPaysView.Mostkhdm,0),\r\n    Employees_FatherName=IsNull(Employees.FatherName,N' '),\r\n    Employees_Birthplace=IsNull(Employees.Birthplace,N' '),\r\n    Employees_IssuePlace=IsNull(Employees.IssuePlace,N' '),\r\n    Employees_BirthDate=IsNull(Employees.BirthDate,0),\r\n    Employees_IdNo=IsNull(Employees.IdNo,N' '),\r\n    Employees_NationalCode=IsNull(Employees.NationalCode,0),\r\n\tMastPaysView_Rank=IsNull(MastPaysView.Rank,0),\r\n    Employees_Gender=IsNull(Employees.Gender,0),\r\n    MastPaysView_YearMonth=MastPaysView.YearMonth,\r\n    MastPaysView_SerialPay=MastPaysView.SerialPay,\r\n    MastPaysView_InsTypeCode=MastPaysView.InsTypeCode,\r\n    MastPaysView_Salcnd=MastPaysView.Salcnd,\r\n    MastPaysView_Wrktim=MastPaysView.Wrktim,\r\n    MastPaysView_RegionCode=MastPaysView.RegionCode,\r\n    MastPaysView_EmpID=MastPaysView.EmpID,\r\n    MastPaysView_EduCode=MastPaysView.EduCode,\r\n    MastPaysView_EduSubCode=MastPaysView.EduSubCode,\r\n    MastPaysView_MilitaryStatusCode=MastPaysView.MilitaryStatusCode,\r\n    MastPaysView_MarriageCode=MastPaysView.MarriageCode,\r\n    MastPaysView_WifeJobCode=MastPaysView.WifeJobCode,\r\n    MastPaysView_IsarCode=MastPaysView.IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView.IsarRelationCode,\r\n    MastPaysView_Mostkhdm=MastPaysView.Mostkhdm,\r\n    MastPaysView_EmptypeCode=MastPaysView.EmptypeCode,\r\n    MastPaysView_Rasteh=MastPaysView.Rasteh,\r\n    MastPaysView_Rshteh=MastPaysView.Rshteh,\r\n    MastPaysView_Tbgh=MastPaysView.Tbgh,\r\n    MastPaysView_Sazpost=MastPaysView.Sazpost,\r\n    MastPaysView_HokmTypeCode=MastPaysView.HokmTypeCode,\r\n    MastPaysView_ChildNo=MastPaysView.ChildNo,\r\n    MastPaysView_EmpYear=MastPaysView.EmpYear,\r\n    MastPaysView_EmpDate=ISNULL(MastPaysView.EmpDate,0),\r\n\tMastPaysView_TrialDate=ISNULL(MastPaysView.TrialDate,0),\r\n    MastPaysView_EmpDate2=ISNULL(MastPaysView.EmpDate2,0),\r\n    MastPaysView_ChgDate=ISNULL(MastPaysView.ChgDate,0),\r\n    MastPaysView_Grop=MastPaysView.Grop,\r\n    Regions_OrganCode=IsNull(Regions.OrganCode,0),\r\n    SumCurrentValues_CodePayID=IsNull(SumCurrentValues.CodePayID,0),\r\n    SumCurrentValues_DocVal=IsNull(SumCurrentValues.DocVal,0),\r\n    SumCurrentValues_BackDocVal=IsNull(SumCurrentValues.BackDocVal,0),\r\n    MastPaysView_CrtDate=MastPaysView.CrtDate,\r\n\tTrialDate = MastPaysView.TrialDate\r\nFrom\r\n    \r\nMastPaysView as MastPaysView \r\nLeft Outer Join\r\nSumCurrentValues as SumCurrentValues  on MastPaysView.RegionCode = SumCurrentValues.RegionCode and MastPaysView.YearMonth = SumCurrentValues.YearMonth and MastPaysView.SerialPay = SumCurrentValues.SerialPay and MastPaysView.EmpID = SumCurrentValues.EmpID\r\nLeft Outer Join\r\nEmployees as Employees  on MastPaysView.EmpID = Employees.EmpID and SumCurrentValues.EmpID = Employees.EmpID\r\nLeft Outer Join\r\nRegions as Regions  on MastPaysView.RegionCode = Regions.RegionCode\r\n\r\n) as Table0\r\n\r\n) as Table1\r\n\r\n) as Table2\r\n\r\n) as ConditionTable\r\nWhere\r\n    MastPaysView_YearMonth = @YearMonth and\r\nMastPaysView_SerialPay = @SerialPay\r\n\r\n) as UpperLevelTable\r\n\r\n) as SourceTable\r\n\r\n) as InnerTable\r\nGroup BY\r\nMastPaysView_RegionCode,\r\n    MastPaysView_Wrktim,\r\n    EYear,\r\n    EMonth,\r\n    BirthDate,\r\n    Date,\r\n    EmpBaseDate,\r\n    Employees_Birthplace,\r\n    Employees_FatherName,\r\n    Employees_Gender,\r\n    Employees_IdNo,\r\n    Employees_IssuePlace,\r\n    Employees_LastName,\r\n    Employees_Name,\r\n\tMostakhdemID,\r\n    Employees_NationalCode,\r\n    EmptypePosition,\r\n    ExemptDate,\r\n    HokmDate,\r\n    InsType,\r\n    IsarEmp,\r\n    MadrakEmp,\r\n    MastPaysView_ChgDate,\r\n    MastPaysView_ChildNo,\r\n    MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode,\r\n    MastPaysView_EmpDate2,\r\n    MastPaysView_EmpID,\r\n    MastPaysView_EmptypeCode_,\r\n    MastPaysView_EmpYear,\r\n    MastPaysView_HokmTypeCode,\r\n    MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode,\r\n    MastPaysView_MarriageCode,\r\n    MastPaysView_Rasteh,\r\n    MastPaysView_Rshteh,\r\n    MastPaysView_Salcnd,\r\n    MastPaysView_Sazpost,\r\n    MastPaysView_Tbgh,\r\n    Military,\r\n    Rotbeh,\r\n    ServiceType,\r\n    YearMonth, \r\n\tMastPaysView_InsTypeCode,\r\n\tMastPaysView_InsDateUp30,\r\n\tTrialDate\r\n) as OuterTable\r\nleft join temp as temp \r\non OuterTable.InsType= temp.InsBaseTypeCode where temp.rw=1\r\n ) as Outerable2\r\n) as UpperTable\r\n) as UpperTable3\r\n) as UpperTable4\r\n) as UpperTable5\r\n) as UpperTable6\r\n) as UpperTable7\r\nWhere\r\n    Mogarrari+Emp_Baz+Org_Baz+SayerOrg+SayerEmp+MEmpBaz+MOrgBaz<> 0 AND UpperTable7.ServiceType<>8\r\n) AS UpperTable8\r\n\t) AS UpperTable9\r\n\t) AS UpperTable10\t\r\n\t--) AS UpperTable11 \r\n\r\n\tSELECT\r\n\tYearMonth = YearMonth,\r\n\tMastPaysView_RegionCode=MastPaysView_RegionCode,\r\n\tMastPaysView_EmpYear=CASE  WHEN Emptype2!= 3 and LEN(CONVERT(NVARCHAR(20), MastPaysView_EmpYear))=3 THEN '00/0'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),1,1)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),2,2)\t\r\n\t\t\t\t\t\t\t   WHEN Emptype2!= 3 and LEN(CONVERT(NVARCHAR(20), MastPaysView_EmpYear))=4 THEN '00/' +SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),1,2)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),3,2)\r\n\t\t\t\t\t\t\t   WHEN Emptype2!= 3 and LEN(CONVERT(NVARCHAR(20), MastPaysView_EmpYear))=5 THEN '0'\t  +SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),1,1)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),2,2)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),4,2)\r\n\t\t\t\t\t\t\t   WHEN Emptype2!= 3 and LEN(CONVERT(NVARCHAR(20), MastPaysView_EmpYear))=6 THEN\t\t   SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),1,2)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),3,2)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),4,2)\r\n\t\t\t\t\t\t\t  WHEN Emptype2= 3 THEN ' '\r\n\t\t\t\t\t\t\t  ELSE CONVERT(NVARCHAR(20), MastPaysView_EmpYear) END,\t\t \r\n\tEmpBaseDate=EmpBaseDate,\r\n    DeductOver30=DeductOver30,\r\n    SumDeductIncludeSanavatGhabl=0,\r\n    MzoodhengamkosooratbazneshastegiEmpBaz=MzoodhengamkosooratbazneshastegiEmpBaz,\r\n    AghsaatEnteghalHaghbime=AghsaatEnteghalHaghbime,\r\n    EmpBazSanavatGhbal=0,\r\n    OrgBazSanavatghabl=0,\r\n\tJozBBand11=JozBBand11,\r\n\tTafavotTatbigh2=0,\r\n\tTafavotTatbigh3=0,\r\n    MostakhdemID=MostakhdemID,\r\n    SandoghChangeDate=SandoghChangeDate,\r\n\tSanavatGhairRasmi=CASE WHEN Emptype2= 3 and LEN(CONVERT(NVARCHAR(20), MastPaysView_EmpYear))=3 THEN '00/0'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),1,1)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),2,2)\t\r\n\t\t\t\t\t\t   WHEN Emptype2= 3 and LEN(CONVERT(NVARCHAR(20), MastPaysView_EmpYear))=4 THEN '00/' +SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),1,2)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),3,2)\r\n\t\t\t\t\t       WHEN Emptype2= 3 and LEN(CONVERT(NVARCHAR(20), MastPaysView_EmpYear))=5 THEN '0'\t  +SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),1,1)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),2,2)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),4,2)\r\n\t\t\t\t     \t   WHEN Emptype2= 3 and LEN(CONVERT(NVARCHAR(20), MastPaysView_EmpYear))=6 THEN\t\t   SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),1,2)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),3,2)+'/'+SUBSTRING(CONVERT(NVARCHAR(20), MastPaysView_EmpYear),4,2)\r\n\tELSE SanavatGhairRasmi END,\r\n\tMastPaysView_InsDateUp30=MastPaysView_InsDateUp30,\r\n    Hokm = Hokm,\r\n    Marriage = Marriage,\r\n    Reshteh = Reshteh,\r\n    Madrak = Madrak,\r\n    Major = Major,\r\n    Gender = Gender,\r\n\tEmptype = Emptype,\r\n\tEmptype2 = Emptype2,\r\n    SazPost = SazPost,\r\n    IsarStatus = IsarStatus,\r\n    IsarRelStatus = IsarRelStatus,\r\n    Rasteh = Rasteh,\r\n    Date = Date,\r\n    Employees_Name = Employees_Name,\r\n    Employees_LastName = Employees_LastName,\r\n    Employees_Gender = Employees_Gender,\r\n    Employees_FatherName = Employees_FatherName,\r\n    Employees_NationalCode = Employees_NationalCode,\r\n    Employees_IdNo = Employees_IdNo,\r\n    Employees_IssuePlace = Employees_IssuePlace,\r\n    Employees_Birthplace = Employees_IssuePlace,\r\n    BirthDate =BirthDate,\r\n    MastPaysView_EmpID = MastPaysView_EmpID,\r\n    MastPaysView_EduCode = MastPaysView_EduCode,\r\n    MastPaysView_EduSubCode = MastPaysView_EduSubCode,\r\n    Military = Military,\r\n    MastPaysView_Rasteh = MastPaysView_Rasteh,\r\n    MastPaysView_Sazpost = MastPaysView_Sazpost,\r\n    MastPaysView_Rshteh = MastPaysView_Rshteh,\r\n    Rotbeh = Rotbeh,\r\n    MastPaysView_Tbgh = MastPaysView_Tbgh,\r\n    MastPaysView_ChildNo = MastPaysView_ChildNo,\r\n    ServiceType = ServiceType,\r\n    EmptypePosition = EmptypePosition,\r\n    MastPaysView_IsarCode = MastPaysView_IsarCode,\r\n\tMastPaysView_IsarRelationCode=MastPaysView_IsarRelationCode,\r\n    MastPaysView_EmpDate2 = MastPaysView_EmpDate2,\r\n    MastPaysView_ChgDate = MastPaysView_ChgDate,\r\n    HokmDate = HokmDate,\r\n    MadrakEmp = MadrakEmp,\r\n    MastPaysView_Salcnd = MastPaysView_Salcnd,\r\n\tPhoneNum=PhoneNum,\r\n    Pay1 = Pay1,\r\n    Pay2 = Pay2,\r\n    Pay3 = Pay3,\r\n    Pay4 = Pay4,\r\n    Pay5 = Pay5,\r\n    Pay6 = Pay6,\r\n    Pay7 = Pay7,\r\n    Pay8 = Pay8,\r\n    Pay9 = Pay9,\r\n    Pay10 = Pay10,\r\n    Pay11 = Pay11,\r\n    Pay12 = Pay12,\r\n    Sayer = Sayer,\r\n    Pay13 = Pay13,\r\n\tPay51 = Pay51,\r\n    Pay18Rotbehbandi=Pay18Rotbehbandi,\r\n    Pay19Tarmim=Pay19Tarmim,\r\n    PayNegahdasht=0,\r\n\tPay46 = Pay46,\r\n    Pay14 = Pay14,\r\n    SumPay = SumPay,\r\n    Emp_Baz =case when Emptype2 IN (19,26) then 0 else  Emp_Baz end,\r\n    Org_Baz =case when Emptype2 IN (19,26) then Emp_Baz+Org_Baz else Org_Baz end,\r\n    MSumPay = MSumPay,\r\n    MEmpBaz = MEmpBaz,\r\n    MOrgBaz = MOrgBaz,\r\n    Code95 = Code95,\r\n\tCode97 = Code97,\r\n    Code96 = Code96,\r\n    Code27 = Code27,\r\n    Mogarrari = Mogarrari,\r\n    SayerOrg = SayerOrg,\r\n    SayerEmp =SayerEmp,\r\n    SumKasrBaz = SumKasrBaz,\r\n\tTrialDate = TrialDate\r\n\tFROM #UpperTable10 --where MastPaysView_EmpID=13178008\r\ndrop table #UpperTable10\r\n";