在select中添加“ - ”而不是空格

时间:2022-08-16 16:28:18

Is there a way to add the "-" carector instead of speces in the below column in my select statement:

有没有办法在我的select语句的下面的列中添加“ - ”carector而不是speces:

SPACE(([Depth]-1)*4) + [OrgUnitName] AS [OrgUnitName],

So right now it adds spaces for however many depths there are. Is it possible to have this add the "-" charector instead of the spaces? The data is locations so it could have spaces between the words as well.

所以现在它增加了许多深度的空间。是否可以添加“ - ”charector而不是空格?数据是位置,因此它们之间也可以有空格。

So for:

Test
 Test2
  Test23

I want:

Test
-Test2
--Test23

1 个解决方案

#1


3  

From the documentation of REPLICATE:

从REPLICATE的文档:

REPLICATE ( string_expression ,integer_expression )

That is

REPLICATE ('-', ([Depth]-1)*4) + [OrgUnitName] AS [OrgUnitName],

in your case.

在你的情况下。

However I prefer to return Depth as an output column and format the dashes with code. As little formatting and logic as possible in the SQL queries increases maintainability in my experience.

但是我更喜欢将Depth作为输出列返回,并使用代码格式化破折号。由于SQL查询中的格式和逻辑很少,因此在我的体验中提高了可维护性。

#1


3  

From the documentation of REPLICATE:

从REPLICATE的文档:

REPLICATE ( string_expression ,integer_expression )

That is

REPLICATE ('-', ([Depth]-1)*4) + [OrgUnitName] AS [OrgUnitName],

in your case.

在你的情况下。

However I prefer to return Depth as an output column and format the dashes with code. As little formatting and logic as possible in the SQL queries increases maintainability in my experience.

但是我更喜欢将Depth作为输出列返回,并使用代码格式化破折号。由于SQL查询中的格式和逻辑很少,因此在我的体验中提高了可维护性。