简体   繁体   English

用前导和尾随空格填充SQL中列的所有值

[英]Padding with leading and trailing spaces all values of column in SQL

For a column in a table, I want to add leading & trailing spaces to all the values of the column in one go. 对于表中的一列,我想一次性向该列的所有值添加前导和尾随空格。 For example, table Employee(Name varchar(5)) . 例如, table Employee(Name varchar(5)) As table is having 5 characters each in a row, On executing a query it should be updated to 7 characters each in a row. 由于表格连续5字符,在执行查询时,表格应连续更新为7字符。 Infact leading & trailing spaces should be padded to the values in the column. 实际的前导和尾随空格应填充到该列中的值。

I need an SQL query. 我需要一个SQL查询。

select ' '||Name||' ' from Employee

如果要更新表

update EMPLOYEE set Name = ' ' || Name || ' '

In Oracle, this will put a single space in front of every value in the "name" column, and right pad it with spaces to a total length of 7 characters: 在Oracle中,这将在“名称”列中每个值的前面放置一个空格,并在其右边填充空格,其总长度为7个字符:

update employee
set name = rpad(' '||name,7);

You'll need to alter the table first to accommodate the extra spaces. 您需要先更改表格以容纳额外的空间。

alter table employee
modify
name varchar2(7);
UPDATE EMPLOYEE
SET Name = ' ' || Name || ' '

Since the column that needs to be padded is of variable length I do not believe hard coding a space before or after would be the correct solution. 由于需要填充的列的长度是可变的,因此我不认为在此之前或之后对空格进行硬编码将是正确的解决方案。 If the data in the column has a length of 3, padding 1 space before and after the data will still result in a length of 5. Since the OP did not specify if all of the data has a length of 5 a different approach is needed. 如果该列中的数据长度为3,则在数据前后填充1个空格仍将导致长度为5。由于OP未指定是否所有数据的长度都为5,因此需要另一种方法。 Depending on how you want spaces added you could try the following (this adds 1 space before the data and pads the remaining spaces to the end): 根据您希望添加空格的方式,可以尝试以下操作(这会在数据之前添加1个空格,并将剩余空格填充到末尾):

Declare @Name varchar(5) = 'Bob'        
Select ' ' + @Name + REPLICATE(' ',(6 - LEN(@NAME))) as NewName

This results in 1 space before the name and 3 spaces after. 这将导致名称前有1个空格,之后有3个空格。

Where is the data coming from? 数据来自哪里? If you make the width of the column fixed, any leading spaces in the source data should carry over. 如果您将列的宽度固定,则源数据中的任何前导空格都应保留。 (DISCLAIMER: I am working in SQL Server 2012, not Oracle.) I had an issue where I was sending the data from SQL server to an AS400 DB2 database, and the programs running from the data expect things to line up in certain columns. (免责声明:我使用的是SQL Server 2012,而不是Oracle。)我遇到了一个问题,我正在将数据从SQL Server发送到AS400 DB2数据库,并且从数据运行的程序希望某些列中的内容对齐。 Trimming leading spaces mis-aligns the whole row. 修剪前导空格会使整行未对齐。 When my SQL server data type was NVARCHAR(20), the "VAR" part trimmed the leading spaces. 当我的SQL Server数据类型为NVARCHAR(20)时,“ VAR”部分会修剪前导空格。 Making it NCHAR(20) fixed that problem. 使它成为NCHAR(20)可以解决该问题。 The DB2 Database removed the spaces though, so I am having issues on that end. 但是,DB2数据库除去了空格,因此我在这方面遇到了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM