在 T-SQL 中用单个空格替换重复空格 [英] Replace duplicate spaces with a single space in T-SQL

查看:16
本文介绍了在 T-SQL 中用单个空格替换重复空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确保给定的字段在字符之间没有超过一个空格(我不关心所有空格,只关心空格).

I need to ensure that a given field does not have more than one space (I am not concerned about all white space, just space) between characters.

所以

'single    spaces   only'

需要变成

'single spaces only'

下面的将不起作用

select replace('single    spaces   only','  ',' ')

因为它会导致

'single  spaces  only'

我真的更愿意坚持使用原生 T-SQL 而不是基于 CLR 的解决方案.

I would really prefer to stick with native T-SQL rather than a CLR based solution.

想法?

推荐答案

更整洁:

select string = replace(replace(replace(' select   single       spaces',' ','<>'),'><',''),'<>',' ')

输出:

选择单个空格

这篇关于在 T-SQL 中用单个空格替换重复空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆