c#从字符串中删除所有空格

时间:2014-02-28 09:22:24

标签: sql-server

我有一个sql字符串检索三列的数据,我不想在这些列之间有任何空格。我想删除列之间的空格。

 string stringSql = " SELECT distinct  " +
                   "'" + comboBox6.Text + "' as RecordType" +
                 " , left([Claimant Name] +' ',29) " +
                " , left([Claimant Address1] +' ',29)  " +
                " , left([Claimant Address2] +' ',29) as ClaimantAddress2 " +

我的客户要求就像

**1xyz1dundas**

MY输出就像

**1 xyz 1 dundas**

4 个答案:

答案 0 :(得分:4)

使用替换

stringSql .Replace("  ", string.empty);

在sql中

 SELECT REPLACE(stringSql, ' ', '')

答案 1 :(得分:2)

如果你想删除空白(不仅仅是普通空格 ' ',而是说 tabluations {{1你可以使用LINQ:

'\t'

答案 2 :(得分:1)

在SQL-Server上:

SELECT REPLACE(yourField, ' ', '')

答案 3 :(得分:0)

MSSQL Server:

RTRIM(LTRIM(a))

Oracle,MySQL,SQL Server 2012:TRIM功能

string stringSql = " SELECT distinct  " +
                   "'" + comboBox6.Text + "' as RecordType" +
                 " ,  RTRIM(LTRIM(left([Claimant Name] +' ',29))) " +
                " ,  RTRIM(LTRIM(left([Claimant Address1] +' ',29)))  " +
                " ,  RTRIM(LTRIM(left([Claimant Address2] +' ',29))) as ClaimantAddress2 "