English 中文(简体)
String padding in tsql
原标题:

I print out a bunch of DDL statements that are dynamically created and want to align the output in a specific way.

PRINT  ALTER TABLE   + @TableName +   WITH NOCHECK ADD CONSTRAINT CK_  + @TableName +  _  + @ColumnName +  _MinimumLength CHECK (LEN(  + @ColumnName +  ) > 0) 

Output:

ALTER TABLE SignType ADD CONSTRAINT CK_SignType_Description_MinimumLength CHECK (LEN(Description) > 0)
ALTER TABLE Person ADD CONSTRAINT CK_Person_Name_MinimumLength CHECK (LEN(Name) > 0)

What I want the output to be:

ALTER TABLE SignType                WITH NOCHECK ADD CONSTRAINT CK_SignType_Description_MinimumLength                CHECK (LEN(Description) > 0)
ALTER TABLE Person                  WITH NOCHECK ADD CONSTRAINT CK_Person_Name_MinimumLength                         CHECK (LEN(Name) > 0)

Is there a function that allows me to pad the string by n of character x. I would use it like this:

PRINT  ALTER TABLE   + @TableName + PAD(   , 50 - LEN(@TableName)) +   WITH NOCHECK ADD CONSTRAINT ..... 

Thanks

最佳回答

I believe what you want is the REPLICATE function available from SQL 2005 on.

PRINT  ALTER TABLE   + @TableName + REPLICATE(   , 50 - LEN(@TableName)) +   WITH NOCHECK ADD CONSTRAINT ..... 

MSDN: REPLICATE

问题回答

You can use the SPACE function:

PRINT  ALTER TABLE   + @TableName + SPACE( 50 - LEN(@TableName)) +   WITH NOCHECK ADD CONSTRAINT ..... 

If it s something else than a space you want to insert, you can use REPLICATE(varchar, int).

You need to use the REPLICATE function, along with DATALENGTH:

PRINT  ALTER TABLE   + @TableName + REPLICATE(   , 50 - DATALENGTH(@tablename)) +   WITH NOCHECK ADD CONSTRAINT ..... 




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签