关于如何将单个列的值拆分为多个列的问题,posts有很多,但是我在标准化列中的数据以使用这些技术时遇到了麻烦。标准化以下示例的最佳方法是什么?
例如:
**emails**
first_email,second_email
third_email; fourth_email
fifth_email ;sixth_email
seventh_email, eight_email
ninth_email.tenth_email
我对标准化的预期输出是:
**emails**
first_email, second_email
third_email, fourth_email
fifth_email, sixth_email
seventh_email, eight_email
ninth_email, tenth_email
答案 0 :(得分:1)
这显示了如何规范格式,但是由于.
字符有时在电子邮件地址中使用,而在其他时候则用于分隔电子邮件地址,因此您可能不包括替换为.
,然后手动进行操作。
您可以使用嵌套的replace()
来消除空格,然后用,[space]
替换有效的电子邮件地址分隔符。
declare @str varchar(max) = 'first_email,second_email
third_email; fourth_email
fifth_email ;sixth_email
seventh_email, eight_email
ninth_email.tenth_email'
select replace(replace(replace(replace(
@str, ' ', ''), -- a space is not valid in an email
',', ', '),
';', ', '),
'.', ', '
)
哪个返回:
first_email, second_email
third_email, fourth_email
fifth_email, sixth_email
seventh_email, eight_email
ninth_email, tenth_email