如何使用t-sql将下划线替换为空格?

时间:2015-09-17 21:09:00

标签: sql sql-server updates

SELECT [Name]
      ,[Code]
      ,case when len([ParentCode]) = '' then [Code] else [ParentCode] end as [ParentCode]
      ,case when len([Descr])=0 then [Code] else [Descr] end as [Descr]
      ,[Cumulative]
      ,[Expense]
      ,[Accts]
      ,[Admin]
      ,[Assessment]
      ,[Balance]
      ,[Fiber]
      ,[GL]
      ,[LV]
      ,[Slush]
  FROM [KR].[pl].[Accounts]

目前我有这个查询,但是我想更改数据包含下划线的部分而不是ParentCode和Code列中的空格。 Ex(slush_Code) 如何修改此查询才能工作?

1 个答案:

答案 0 :(得分:6)

基本替换 command

SELECT REPLACE(ParentCode,'_',' ')

/

SELECT [Name]
  ,[Code]
  ,case 
       when len([ParentCode]) = 0 then REPLACE(Code,'_',' ') 
       else REPLACE(ParentCode,'_',' ') 
   end as [ParentCode]
  ,case when len([Descr]) = 0 then REPLACE(Code,'_',' ') 
       else [Descr] 
   end as [Descr]
  ,[Cumulative]
  ,[Expense]
  ,[Accts]
  ,[Admin]
  ,[Assessment]
  ,[Balance]
  ,[Fiber]
  ,[GL]
  ,[LV]
  ,[Slush]
FROM [KR].[pl].[Accounts]