I'm trying to take some queries from Access and store them as VIEW in SQL Server. I have an iif
statement that I'm trying to make SQL friendly. It looks like this... (40 zeros)
Format([PassNo],"0000000000000000000000000000000000000000")) AS SORTPassNo
Pass No is anywhere from 5 to 12 numbers (12345 to 123456712345). What this formatting does in Access, is it makes the entire number 40 characters, with all the zeros in front so the 5 digit number above looks like this
0000000000000000000000000000000000012345
How would I do this in SQL Server?
Here's the code... not all of it, just the GROUP BY part of it that needs fixing, Im declaring this at the top...
DECLARE @PassNo varchar(40)
GROUP BY dbo.tblVendors.VendorName
, dbo.tblCentralInv.VendorID, dbo.tblCentralInv.InvType
,CASE
WHEN tblcentralinv.invtype ='qty'
THEN '0000000000000000000000000000000000000000'
ELSE (SELECT Replicate ('0', 40 -len(@PassNo)) + @PassNo)
END
EDIT I AM CREATING A VIEW WITH THIS CODE. I am not able to use a subquery or declare any variables
Here's all my CODE From the view
Create View [dbo].[vBegInv]
AS
Declare @ SORTInvVoucherChkPassNo varchar(40)
SELECT dbo.tblCentralVendors.VendorName
, dbo.tblCentral.VendorID
, dbo.tblCentral.InvType
, CASE
when invtype = 'qty'
THEN '0'
ELSE InvVoucherChkPassNo
END as invoucherchkpassnum
, Sum(dbo.tblCentral.InvQty) AS SumOfInvQty
,CASE
WHEN tblcentral.invtype ='qty'
THEN '0000000000000000000000000000000000000000'
ELSE REPLACE(STR(@InvVoucherChkPassNo, 40), ' ', 0)
END AS SORTInvVoucherChkPassNo
, Max(dbo.tblCentral.InvValue) AS InvValue
FROM dbo.tblCentralI NNER JOIN dbo.tblCentralVendors ON dbo.tblCentral.VendorID = dbo.tblCentralVendors.VendorID
WHERE ((dbo.tblCentral.InvTransDate)<'12/1/2015')
GROUP BY dbo.tblCentralVendors.VendorName
, dbo.tblCentral.VendorID, dbo.tblCentral.InvType
,CASE
WHEN tblcentral.invtype ='qty'
THEN '0000000000000000000000000000000000000000'
ELSE REPLACE(STR(@InvVoucherChkPassNo, 40), ' ', 0)
END
HAVING (Sum(dbo.tblCentral.InvQty))> 0