我有一个oracle表,a_abc
有三列(name
,ID
和address
)。该表的数据将从txt文件中提取。如果以数字' 0'开头,如何删除ID列中的第一个字符。我需要一个VB解决方案。这就是我到目前为止所做的事情
Private Sub ReadData()
Dim rowvalue As String
Dim cellvalue(20) As String
Dim header As String = "ID"
Dim streamReader As IO.StreamReader = New IO.StreamReader("D:\local_costctr.txt")
'Reading CSV file content
While streamReader.Peek() <> -1
rowvalue = streamReader.ReadLine()
cellvalue = rowvalue.Split("|") 'check what is ur separator
ConnectOracle()
mySQLcmd.Connection = myConn
If SQL.Length > 0 Then
SQL.Replace(SQL.ToString, "")
End If
SQL.Append("insert into a_abc (name ,ID ,address) ")
SQL.Append(" values ('" & cellvalue(0) & "' ,'" & cellvalue(1) & "','" & cellvalue(2) & "') ")
Console.WriteLine(header)
Console.WriteLine(header.Trim({"0"c}))
Try
mySQLcmd.CommandText = SQL.ToString
mySQLcmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error " & ex.Message.ToString)
End Try
End While
End Sub
如果有多个前导零,我希望将它们全部删除。
答案 0 :(得分:3)
ltrim
函数可以解决这个问题:
SELECT name, LTRIM(id, '0'), address
FROM a_abc
编辑:
现在问题被编辑了,我看到你提到插入数据,而不是查询它。同样的解决方案也可以应用于insert
:
SQL.Append("insert into a_abc (name ,ID ,address) ")
SQL.Append(" values ('" & cellvalue(0) & "', LTRIM(" & cellvalue(1) & ", '0'), '" & cellvalue(2) & "') ")
答案 1 :(得分:0)
当您从txt文件中提取数据时,您会看到ID字段:
For Each line As String In IO.File.ReadAllLines("yourpath")
If "myID".Substring(0, 1) = "0" Then
'take the "myID" without the leading 0
"myID".Substring(1)
End if
Next
根据您的情况,您可能需要Null-checks等...
为您的代码编辑: 将SQL.append中的cellvalue(1)替换为:
IIF(cellvalue(1).Substring(0, 1) = "0",cellvalue(1).Substring(1),cellvalue(1))