我想查看字段的字符内容,然后在该字段中生成字符类型模式的计数。因此,'SomeStuff 123'的值将具有'XXXXXXXXX 999'的模式。
我已经有两种方法可以做到这一点,但它们似乎很长而且很笨拙。我想找到最短/最优雅的方式来做到这一点。
我认为PATINDEX可能是最佳选择:
Select
Pattern =
Case
When PatIndex('%[0-9]%', Left([MyField], 1)) = 1 Then '9'
When PatIndex('%[a-Z]%', Left([MyField], 1)) = 1 Then 'X'
When PatIndex('% %', Left([MyField], 1)) = 1 Then ' '
Else '?'
End
+
Case
When PatIndex('%[0-9]%', Substring([MyField], 2, 1)) = 1 Then '9'
When PatIndex('%[a-Z]%', Substring([MyField], 2, 1)) = 1 Then 'X'
When PatIndex('% %', Substring([MyField], 2, 1)) = 1 Then ' '
Else '?'
End
--...
以下是我测试过的几种方法:
Literal REPLACE方法:
Select [MyFieldPattern] = Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace (Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace([MyField], 'A','X') ,'B','X') ,'C','X') ,'D','X') ,'E','X') ,'F','X') ,'G','X') ,'H','X') ,'I','X') ,'J','X') ,'K','X') ,'L','X') ,'M','X') ,'N','X') ,'O','X') ,'P','X') ,'Q','X') ,'R','X') ,'S','X') ,'T','X') ,'U','X') ,'V','X') ,'W','X') ,'Y','X') ,'Z','X') ,'0','1') ,'2','1') ,'3','1') ,'4','1') ,'5','1') ,'6','1') ,'7','1') ,'8','1') ,'9','1')
, Count(*) As Counts
From [MyDatabase]
Group By
Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace (Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace([MyField], 'A','X') ,'B','X') ,'C','X') ,'D','X') ,'E','X') ,'F','X') ,'G','X') ,'H','X') ,'I','X') ,'J','X') ,'K','X') ,'L','X') ,'M','X') ,'N','X') ,'O','X') ,'P','X') ,'Q','X') ,'R','X') ,'S','X') ,'T','X') ,'U','X') ,'V','X') ,'W','X') ,'Y','X') ,'Z','X') ,'0','1') ,'2','1') ,'3','1') ,'4','1') ,'5','1') ,'6','1') ,'7','1') ,'8','1') ,'9','1')
Order By Count(*) Desc
暴力CASE方法:
Select
FieldName_Analyzed = Convert(VarChar(15), 'MyField'),
Literal_Value_Aggregated = Convert(VarChar(30),
Convert(VarChar(1), Case When ASCII(Substring([MyField], 1, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 1, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 1, 1)) = 32 Then '_' When ASCII(Substring([MyField], 1, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 2, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 2, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 2, 1)) = 32 Then '_' When ASCII(Substring([MyField], 2, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 3, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 3, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 3, 1)) = 32 Then '_' When ASCII(Substring([MyField], 3, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 4, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 4, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 4, 1)) = 32 Then '_' When ASCII(Substring([MyField], 4, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 5, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 5, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 5, 1)) = 32 Then '_' When ASCII(Substring([MyField], 5, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 6, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 6, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 6, 1)) = 32 Then '_' When ASCII(Substring([MyField], 6, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 7, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 7, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 7, 1)) = 72 Then '_' When ASCII(Substring([MyField], 7, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 8, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 8, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 8, 1)) = 82 Then '_' When ASCII(Substring([MyField], 8, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 9, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 9, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 9, 1)) = 92 Then '_' When ASCII(Substring([MyField], 9, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 10, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 10, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 10, 1)) = 102 Then '_' When ASCII(Substring([MyField], 10, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 11, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 11, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 11, 1)) = 112 Then '_' When ASCII(Substring([MyField], 11, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 12, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 12, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 12, 1)) = 122 Then '_' When ASCII(Substring([MyField], 12, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 13, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 13, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 13, 1)) = 132 Then '_' When ASCII(Substring([MyField], 13, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 14, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 14, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 14, 1)) = 142 Then '_' When ASCII(Substring([MyField], 14, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 15, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 15, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 15, 1)) = 152 Then '_' When ASCII(Substring([MyField], 15, 1)) Is Null Then Char(216) Else '?' End)
),
Percent_Populated = Convert(Decimal(5,2), Convert(Decimal(5,4), Convert(Float, Count(
Convert(VarChar(1), Case When ASCII(Substring([MyField], 1, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 1, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 1, 1)) = 32 Then '_' When ASCII(Substring([MyField], 1, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 2, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 2, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 2, 1)) = 32 Then '_' When ASCII(Substring([MyField], 2, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 3, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 3, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 3, 1)) = 32 Then '_' When ASCII(Substring([MyField], 3, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 4, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 4, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 4, 1)) = 32 Then '_' When ASCII(Substring([MyField], 4, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 5, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 5, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 5, 1)) = 32 Then '_' When ASCII(Substring([MyField], 5, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 6, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 6, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 6, 1)) = 32 Then '_' When ASCII(Substring([MyField], 6, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 7, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 7, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 7, 1)) = 72 Then '_' When ASCII(Substring([MyField], 7, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 8, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 8, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 8, 1)) = 82 Then '_' When ASCII(Substring([MyField], 8, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 9, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 9, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 9, 1)) = 92 Then '_' When ASCII(Substring([MyField], 9, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 10, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 10, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 10, 1)) = 102 Then '_' When ASCII(Substring([MyField], 10, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 11, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 11, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 11, 1)) = 112 Then '_' When ASCII(Substring([MyField], 11, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 12, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 12, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 12, 1)) = 122 Then '_' When ASCII(Substring([MyField], 12, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 13, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 13, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 13, 1)) = 132 Then '_' When ASCII(Substring([MyField], 13, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 14, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 14, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 14, 1)) = 142 Then '_' When ASCII(Substring([MyField], 14, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 15, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 15, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 15, 1)) = 152 Then '_' When ASCII(Substring([MyField], 15, 1)) Is Null Then Char(216) Else '?' End)
))/(Select Count(*) From [MyDatabase]))*100),
Total_Count = Convert(Int, Count(
Convert(VarChar(1), Case When ASCII(Substring([MyField], 1, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 1, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 1, 1)) = 32 Then '_' When ASCII(Substring([MyField], 1, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 2, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 2, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 2, 1)) = 32 Then '_' When ASCII(Substring([MyField], 2, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 3, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 3, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 3, 1)) = 32 Then '_' When ASCII(Substring([MyField], 3, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 4, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 4, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 4, 1)) = 32 Then '_' When ASCII(Substring([MyField], 4, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 5, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 5, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 5, 1)) = 32 Then '_' When ASCII(Substring([MyField], 5, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 6, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 6, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 6, 1)) = 32 Then '_' When ASCII(Substring([MyField], 6, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 7, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 7, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 7, 1)) = 72 Then '_' When ASCII(Substring([MyField], 7, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 8, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 8, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 8, 1)) = 82 Then '_' When ASCII(Substring([MyField], 8, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 9, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 9, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 9, 1)) = 92 Then '_' When ASCII(Substring([MyField], 9, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 10, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 10, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 10, 1)) = 102 Then '_' When ASCII(Substring([MyField], 10, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 11, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 11, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 11, 1)) = 112 Then '_' When ASCII(Substring([MyField], 11, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 12, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 12, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 12, 1)) = 122 Then '_' When ASCII(Substring([MyField], 12, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 13, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 13, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 13, 1)) = 132 Then '_' When ASCII(Substring([MyField], 13, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 14, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 14, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 14, 1)) = 142 Then '_' When ASCII(Substring([MyField], 14, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 15, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 15, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 15, 1)) = 152 Then '_' When ASCII(Substring([MyField], 15, 1)) Is Null Then Char(216) Else '?' End)
))
From [MyDatabase]
Group By
Convert(VarChar(1), Case When ASCII(Substring([MyField], 1, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 1, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 1, 1)) = 32 Then '_' When ASCII(Substring([MyField], 1, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 2, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 2, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 2, 1)) = 32 Then '_' When ASCII(Substring([MyField], 2, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 3, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 3, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 3, 1)) = 32 Then '_' When ASCII(Substring([MyField], 3, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 4, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 4, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 4, 1)) = 32 Then '_' When ASCII(Substring([MyField], 4, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 5, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 5, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 5, 1)) = 32 Then '_' When ASCII(Substring([MyField], 5, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 6, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 6, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 6, 1)) = 32 Then '_' When ASCII(Substring([MyField], 6, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 7, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 7, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 7, 1)) = 72 Then '_' When ASCII(Substring([MyField], 7, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 8, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 8, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 8, 1)) = 82 Then '_' When ASCII(Substring([MyField], 8, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 9, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 9, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 9, 1)) = 92 Then '_' When ASCII(Substring([MyField], 9, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 10, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 10, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 10, 1)) = 102 Then '_' When ASCII(Substring([MyField], 10, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 11, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 11, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 11, 1)) = 112 Then '_' When ASCII(Substring([MyField], 11, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 12, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 12, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 12, 1)) = 122 Then '_' When ASCII(Substring([MyField], 12, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 13, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 13, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 13, 1)) = 132 Then '_' When ASCII(Substring([MyField], 13, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 14, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 14, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 14, 1)) = 142 Then '_' When ASCII(Substring([MyField], 14, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 15, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 15, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 15, 1)) = 152 Then '_' When ASCII(Substring([MyField], 15, 1)) Is Null Then Char(216) Else '?' End)
Order By
Convert(VarChar(1), Case When ASCII(Substring([MyField], 1, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 1, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 1, 1)) = 32 Then '_' When ASCII(Substring([MyField], 1, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 2, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 2, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 2, 1)) = 32 Then '_' When ASCII(Substring([MyField], 2, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 3, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 3, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 3, 1)) = 32 Then '_' When ASCII(Substring([MyField], 3, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 4, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 4, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 4, 1)) = 32 Then '_' When ASCII(Substring([MyField], 4, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 5, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 5, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 5, 1)) = 32 Then '_' When ASCII(Substring([MyField], 5, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 6, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 6, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 6, 1)) = 32 Then '_' When ASCII(Substring([MyField], 6, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 7, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 7, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 7, 1)) = 72 Then '_' When ASCII(Substring([MyField], 7, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 8, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 8, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 8, 1)) = 82 Then '_' When ASCII(Substring([MyField], 8, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 9, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 9, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 9, 1)) = 92 Then '_' When ASCII(Substring([MyField], 9, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 10, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 10, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 10, 1)) = 102 Then '_' When ASCII(Substring([MyField], 10, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 11, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 11, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 11, 1)) = 112 Then '_' When ASCII(Substring([MyField], 11, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 12, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 12, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 12, 1)) = 122 Then '_' When ASCII(Substring([MyField], 12, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 13, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 13, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 13, 1)) = 132 Then '_' When ASCII(Substring([MyField], 13, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 14, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 14, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 14, 1)) = 142 Then '_' When ASCII(Substring([MyField], 14, 1)) Is Null Then Char(216) Else '?' End)
+ Convert(VarChar(1), Case When ASCII(Substring([MyField], 15, 1)) Between 48 And 57 Then '9' When ASCII(Substring([MyField], 15, 1)) Between 65 And 122 Then 'X' When ASCII(Substring([MyField], 15, 1)) = 152 Then '_' When ASCII(Substring([MyField], 15, 1)) Is Null Then Char(216) Else '?' End)
Desc
答案 0 :(得分:0)
也许是另一种方式
Declare @YourTable table (ID int,MyField varchar(100))
Insert Into @YourTable values
(1,'SomeStuff 123')
Select A.ID
,A.MyField
,B.*
From @YourTable A
Cross Apply (
Select NewString=replace(Stuff((Select ''+C From (
Select C=case when C Like '[0-9]' then '9' else
case when C = ' ' then '§§Space§§' else 'X' end end
From (Select Top (len(A.MyField)) C=substring(A.MyField,Row_Number() Over (Order By (Select null)),1) From master..spt_values ) B1
) C1 For XML Path ('')),1,0,''),'§§Space§§',' ')
) B
返回
ID MyField NewString
1 SomeStuff 123 XXXXXXXXX 999