简体   繁体   English

选择查询固定长度

[英]Select Query for the fixed length

I have an table called customer where i am selecting few columns and making its fixed length,where i need to send the values to SSIS packages for an fixed length output and is written in a text file 我有一个名为customer的表,我选择几列并使其固定长度,我需要将值发送到SSIS包以获得固定长度的输出并写入文本文件

customerID:10
Mobilenumber:11
Emailaddress:256

select customerID,mobilenumber,Emailaddress from customer 

I want to make sure my customerID is always length of 10, mobile number 11, emailaddress 256. 我想确保我的customerID始终为10,手机号码为11,电子邮件地址为256。

You can cast them as fixed-length string datatypes, as in: 您可以将它们转换为固定长度的字符串数据类型,如:

select cast(customerID as char(10)) as customerID,
    cast(mobilenumber as char(11)) as mobilenumber,
    cast(Emailaddress as char(256)) as Emailaddress
from customer

If you come across any oddity while working like this, it may be helpful to keep your ANSI_PADDING settings in mind. 如果您在这样工作时遇到任何奇怪的现象,那么记住您的ANSI_PADDING设置可能会有所帮助。

Another way to do this might be: 另一种方法可能是:

select left(Emailaddress + replicate(' ', 256), 256)

Though this method is often just to apply similar logic from other languages to T-SQL. 虽然这种方法通常只是将类似的逻辑从其他语言应用到T-SQL。

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

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