我有一个将3个字符串连接在一起的方案。每个字符串的长度是25,所以当我连接时,我希望字符串具有固定的宽度。
所需的输出格式:
**String1(length 22 + 3spaces)String2(length 9 +16 spaces)String3(length8+17spaces)**
当前输出:
****String1(length 22 + 3spaces)**2 additional spaces**String2(length 9 +16 spaces)**2 additional spaces**String3(length8+17spaces)**2 additional spaces**
我不想显示其他空格。有办法吗?
https://rextester.com/GSWML31014
在此处附加了妊娠期演示。有帮助吗?!
答案 0 :(得分:1)
在连接前输入25个字符的字符串
SELECT (
SELECT LEFT(t1.testdata + SPACE(25), 25)
FROM #temp1 t1
FOR XML PATH ('')
) AS TheOutput
答案 1 :(得分:1)
您可以简单地将它们强制转换为 Sub Test()
Dim i As Integer
Dim MyRange As Range
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("RESULT")
last_row = Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 to 3
Set MyRange = ws.Range(Cells(2,i),Cells(last_row,i))
Call update_listbox(i, MyRange)
End Sub
Sub(update_listbox)
'here i am updating my listing in ListBox
Sheet4.ListBox & i &.Clear ''Getting Error here
Sheet4.ListBox & i & .AddItem "Select All" ''Getting Error here
Dim myList As Collection
Dim myVal As Variant
Set myList = New Collection
On Error Resume Next
For Each myCell In myRange.Cells
myList.Add myCell.Value, CStr(myCell.Value)
Next myCell
On Error GoTo 0
For Each myVal In myList
Sheet4.ListBox & i &.AddItem myVal 'Getting Error here
Next myVal
End Sub
-SQL Server将根据需要添加任意数量的尾随空格-您不需要char(25)
,因为您没有定界符可从结果的开始。
您甚至不需要连接一个空字符串,因为一旦使用了stuff
,该列就没有别名了,因此cast
将只连接这些值。有关更多信息,请see this answer.
for xml path('')
结果:SELECT CAST(cid AS CHAR(25))
FROM #temp1
FOR XML PATH('')
(添加'1001 1001 1001 '
以显示字符串的长度)