Trimme leading og trailing spaces på alle data i en tabel
Hej,Findes der en enkel måde jeg kan køre en query på en tabel og fjerne alle leading og trailing mellemrum (spaces) på alle data?
Jeg kører den i dag fra en VB.NET applikation, men ønsker at execute den direkte på SQL serveren som en prodecure/funktion/query:
Dette er min VB.NET kode - jeg ønsker som sagt at få den til at fungere i SQL alene uden VB.net:
frmMain.txtAction.Text = "(Import column names)"
frmMain.Refresh()
Dim ColumnNames As New DataTable()
myQuery = "SELECT column_name FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'tblXSAP_Contracts'"
myCommand = New System.Data.SqlClient.SqlCommand(myQuery, myConnection)
myCommand.Connection = myConnection
myCommand.Connection.Open()
Dim myReader As New System.Data.SqlClient.SqlDataAdapter
myReader.SelectCommand = myCommand
myReader.Fill(ColumnNames)
myCommand.Connection.Close()
myConnection.Close()
myCommand.Connection.Close()
myConnection.Close()
For Each row As DataRow In ColumnNames.Rows
Try
frmMain.txtAction.Text = "(Clean SQL Data)"
frmMain.Refresh()
myQuery = "UPDATE dbo.tblXSAP_Contracts SET " & row(0) & " = LTRIM(RTRIM(" & row(0) & "))"
myCommand = New System.Data.SqlClient.SqlCommand(myQuery, myConnection)
myCommand.Connection = myConnection
myCommand.Connection.Open()
myCommand.ExecuteScalar()
myCommand.Parameters.Clear()
myCommand.Connection.Close()
myConnection.Close()
Catch ex As Exception
myQuery99 = "INSERT INTO tblXIST_Log VALUES (@DateAndTime, @UserName, @MachineName, @System, @LogText)"
myCommand99 = New System.Data.SqlClient.SqlCommand(myQuery99, myConnection99)
myCommand99.Parameters.AddWithValue("@DateAndTime", Now())
myCommand99.Parameters.AddWithValue("@UserName", System.Environment.UserName)
myCommand99.Parameters.AddWithValue("@MachineName", System.Environment.MachineName)
myCommand99.Parameters.AddWithValue("@System", "BASS Server")
myCommand99.Parameters.AddWithValue("@LogText", "During update of current customers, this error occured: " & ex.Message)
myCommand99.Connection = myConnection99
myCommand99.Connection.Open()
Try
myCommand99.ExecuteNonQuery()
Finally
End Try
myCommand99.Connection.Close()
myConnection99.Close()
End Try
Next row