Case-insensitive REPLACE() in SQL Server 2000

微笑、不失礼 提交于 2019-12-07 03:46:07

问题


I have a field that contains strings such as 'Blah-OVER', 'Blah-OveR', etc. and want to select them without the 'over's. This only catches the first case (so to speak) and not the others:

SELECT field as "before", REPLACE(field, 'OVER', '') as "after"

How do I just get them all to say 'Blah-' (preserving the case of what's left) without attempting to cover every case combination with another nested REPLACE function?


回答1:


Use a case insensitive collation:

SELECT field as "before", REPLACE(field COLLATE SQL_Latin1_General_Cp1_CI_AI
, 'OVER', '') as "after"

See COLLATE for list of collation names so you choose the one appropiate for your data.

Update

Ok, so I missed your actual request (change case of input, not find case-insensitive). The proper solution is... not to change the input but use an adequate collation for your data. If the data must be displayed in a specific format, use display options in the client, eg. CSS text-transform:uppercase, not in the server SELECT.

There isn't any built-in SQL function to do this transformation in-place, but is trivial to build a CLR function that uses RegEx. (On SQL 2005, not on SQL 2000... doh, I need more coffe).




回答2:


I'm not familiar with SQL Server, but maybe it allows you to make use of regular expressions. These usually offer a case-insensitive mode (set via the i-flag).

Otherwise you could uppercase before the replace call, e.g.

SELECT field as "before", REPLACE(UPPER(field), 'OVER', '') as "after"


来源:https://stackoverflow.com/questions/1468359/case-insensitive-replace-in-sql-server-2000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!