我似乎无法理解为什么我遇到了麻烦,因为我已经多次这样做但是要将此字符串格式化string fsql = string.Format(sql, fa);
我收到错误:Input string was not in a correct format case
。
sql
:
"DECLARE @BENTEST varchar(100)\r\nSET @BENTEST = '{1}'\r\n\r\nSELECT a.Period, a.NCR, a.Date_Created, a.Date_Done, a.Item, a.Qty, tc.TargetCost*a.Qty Cost, a.Aging FROM\r\n\t(SELECT \r\n\tnt.NCR, \r\n\tDATENAME(yy, nt.Date_Done) + '-' + RIGHT('0' + DATENAME(ISOWK, nt.Date_Done), 2) Period,\r\n\tnt1.Date_Created,\r\n\tnt.Date_Done,\r\n\t(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%\"%', a.FirstParseStep)) ParsedItem FROM\r\n\t\t(SELECT SUBSTRING(nt.ItemData, PATINDEX('%\"Item%', nt.ItemData) + LEN('\"Item\":\"'), 60) FirstParseStep) a) Item,\r\n\t(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%}%', a.FirstParseStep)) ParsedQuantity FROM\r\n\t\t(SELECT SUBSTRING(nt.ItemData, PATINDEX('%\"ItemQty%', nt.ItemData) + LEN('\"ItemQty\":'), 60) FirstParseStep) a ) Qty,\r\n\tPCF.dbo.GetRealWorkDays(nt1.Date_Created, nt.Date_Done) Aging\r\n\tFROM PCF.dbo.NCRTask nt, PCF.dbo.NCRTask nt1\r\n\tWHERE nt1.NCR = nt.NCR\r\n\tAND (nt.TaskType = 5 OR nt.TaskType = 16)\r\n\tAND nt1.TaskType = 25\r\n\tUNION\r\n\tSELECT \r\n\tnt.NCR, \r\n\tDATENAME(yy, @BENTEST) + '-' + RIGHT('0' + DATENAME(ISOWK, @BENTEST), 2) Period,\r\n\tnt.Date_Created,\r\n\tnull Date_Done,\r\n\t(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%\"%', a.FirstParseStep)) ParsedItem FROM\r\n\t\t(SELECT SUBSTRING(nt.ItemData, PATINDEX('%\"Item%', nt.ItemData) + LEN('\"Item\":\"'), 60) FirstParseStep) a) Item,\r\n\t(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%}%', a.FirstParseStep)) ParsedQuantity FROM\r\n\t\t(SELECT SUBSTRING(nt.ItemData, PATINDEX('%\"ItemQty%', nt.ItemData) + LEN('\"ItemQty\":'), 60) FirstParseStep) a ) Qty,\r\n\tPCF.dbo.GetRealWorkDays(nt.Date_Created, GETDATE()) Aging\r\n\tFROM PCF.dbo.NCRTask nt\r\n\tWHERE nt.TaskType = 25\r\n\tAND nt.Date_Created < @BENTEST\r\n\tAND NOT EXISTS (SELECT 1 FROM PCF.dbo.NCRTask WHERE nt.NCR = NCR AND (TaskType = 5 OR TaskType = 16) AND Date_Done < @BENTEST)) a,\r\n\tavvik1.dbo.TargetCost tc\r\nWHERE a.Period = DATENAME(yy, @BENTEST) + '-' + RIGHT('0' + DATENAME(ISOWK, @BENTEST), 2)\r\nAND tc.ItemNo = a.Item"
sql
但格式化,以便您可以轻松阅读:
DECLARE @BENTEST varchar(100)
SET @BENTEST = '{1}'
SELECT a.Period, a.NCR, a.Date_Created, a.Date_Done, a.Item, a.Qty, tc.TargetCost*a.Qty Cost, a.Aging FROM
(SELECT
nt.NCR,
DATENAME(yy, nt.Date_Done) + '-' + RIGHT('0' + DATENAME(ISOWK, nt.Date_Done), 2) Period,
nt1.Date_Created,
nt.Date_Done,
(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%"%', a.FirstParseStep)) ParsedItem FROM
(SELECT SUBSTRING(nt.ItemData, PATINDEX('%"Item%', nt.ItemData) + LEN('"Item":"'), 60) FirstParseStep) a) Item,
(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%}%', a.FirstParseStep)) ParsedQuantity FROM
(SELECT SUBSTRING(nt.ItemData, PATINDEX('%"ItemQty%', nt.ItemData) + LEN('"ItemQty":'), 60) FirstParseStep) a ) Qty,
PCF.dbo.GetRealWorkDays(nt1.Date_Created, nt.Date_Done) Aging
FROM PCF.dbo.NCRTask nt, PCF.dbo.NCRTask nt1
WHERE nt1.NCR = nt.NCR
AND (nt.TaskType = 5 OR nt.TaskType = 16)
AND nt1.TaskType = 25
UNION
SELECT
nt.NCR,
DATENAME(yy, @BENTEST) + '-' + RIGHT('0' + DATENAME(ISOWK, @BENTEST), 2) Period,
nt.Date_Created,
null Date_Done,
(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%"%', a.FirstParseStep)) ParsedItem FROM
(SELECT SUBSTRING(nt.ItemData, PATINDEX('%"Item%', nt.ItemData) + LEN('"Item":"'), 60) FirstParseStep) a) Item,
(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%}%', a.FirstParseStep)) ParsedQuantity FROM
(SELECT SUBSTRING(nt.ItemData, PATINDEX('%"ItemQty%', nt.ItemData) + LEN('"ItemQty":'), 60) FirstParseStep) a ) Qty,
PCF.dbo.GetRealWorkDays(nt.Date_Created, GETDATE()) Aging
FROM PCF.dbo.NCRTask nt
WHERE nt.TaskType = 25
AND nt.Date_Created < @BENTEST
AND NOT EXISTS (SELECT 1 FROM PCF.dbo.NCRTask WHERE nt.NCR = NCR AND (TaskType = 5 OR TaskType = 16) AND Date_Done < @BENTEST)) a,
avvik1.dbo.TargetCost tc
WHERE a.Period = DATENAME(yy, @BENTEST) + '-' + RIGHT('0' + DATENAME(ISOWK, @BENTEST), 2)
AND tc.ItemNo = a.Item
fa
是一个字符串[],它包含:
[0] = ""
[1] = date with format "YYYY-MM-DD"
我无法弄清楚为什么我会收到错误。我已经完成了其他一些sql:s而且我没有这个问题。谁知道什么是错的?
答案 0 :(得分:3)
您的字符串中有非转义}
:
(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%}%', a.FirstParseStep)) ParsedQuantity FROM
^
你应该逃避它:
(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%}}%', a.FirstParseStep)) ParsedQuantity FROM
答案 1 :(得分:1)
从我可以看到你只是将2个变量放在string.Format中,但是你从未设置过字符串或者告诉它使用变量。也许你打算做这样的事情? :
string fsql = string.Format("{0} {1}":sql, fa);
因为你必须放弃一个字符串,你可以使用{}和字符串后面给出的变量的索引,而不是仅仅声明字符串。有关String.Format的更多信息,请参阅MSDN
答案 2 :(得分:1)
(SELECT SUBSTRING(a.FirstParseStep, 0, PATINDEX('%}}%', a.FirstParseStep)) ParsedQuantity FROM