English 中文(简体)
T-SQL 操纵、替换、比较、配对、定期表述
原标题:T-SQL string manipulation, replacement, comparing, pattern matching, regular expressions

字母缩略语

特征和内容 电话号码载于插图中。

我想要缩小空间,并比较每一方位方位,而其模式只能与它们相匹配。 模式使用A表示任何特性A-Z,任何0-9。

6种模式是:

A99AA
A999AA
A9A9AA
AA99AA
AA999AA
AA9A9AA

我在表格中加上另一栏,有正确的空间:

pattern PatternTrimmed
A9 9AA  A99AA
A99 9AA A999AA
A9A 9AA A9A9AA
AA9 9AA AA99AA
AA99 9AA    AA999AA
AA9A 9AA    AA9A9AA

我正在使用服务器2005年,我不想有34份将每个特性和编号更改为A s和9页的声明。

关于我如何以简明扼要的方式做到这一点的建议,请参阅。

在此,我想避免:

update postcodes set Pattern = replace (Pattern,  B ,  A ) 
update postcodes set Pattern = replace (Pattern,  C ,  A ) 
update postcodes set Pattern = replace (Pattern,  D ,  A ) 
update postcodes set Pattern = replace (Pattern,  E ,  A )

等等。

以及

update postcodes set Pattern = replace (Pattern,  0 ,  9 ) 
update postcodes set Pattern = replace (Pattern,  1 ,  9 )
update postcodes set Pattern = replace (Pattern,  2 ,  9 )

等等

Basically, I am trying to take a UK postcode typed in at a call centre by an imbecile, 以及 pattern match the entered postcode against one of the 6 above patterns, 以及 work out where to insert the space.

问题回答

这一点:

 Declare @table table
(
ColumnToCompare varchar(20),
AmendedValue varchar(20)
)

Declare @patterns table
(
Pattern varchar(20),
TrimmedPattern varchar(20)
)

Insert Into @table (ColumnToCompare)
Select  BBB87 BBB 
Union all
Select  J97B B 
union all
select  282 8289 
union all
select  UW83 7YY 
union all
select  UW83 7Y0 

Insert Into @patterns
Select  A9 9AA ,  A99AA 
union all
Select  A99 9AA ,  A999AA 
union all
Select  A9A 9AA ,  A9A9AA 
union all
Select  AA9 9AA ,  AA99AA 
union all
Select  AA99 9AA ,  AA999AA 
union all
Select  AA9A 9AA ,  AA9A9AA 


Update @table
Set AmendedValue =  Left(Replace(ColumnToCompare,    ,  ), (CharIndex(   , Pattern)-1)) + space(1) + 
                    SubString(Replace(ColumnToCompare,    ,  ), (CharIndex(   , Pattern)), (Len(ColumnToCompare) - (CharIndex(   , Pattern)-1)))
From @table
Cross Join @Patterns
Where PatIndex(Replace((Replace(TrimmedPattern,  A , [A-Z] )),  9 , [0-9] ), Replace(ColumnToCompare,     ,  )) > 0

select * From @table

第一部分

Left(Replace(ColumnToCompare, , ), (CharIndex( , patterns)-1)

找到了配对方式的空间,将左手部分进行比较。

然后增加一个空间

然后是这一部分

其余部分则指新价值。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签