日期:2014-05-18  浏览次数:20677 次

请求正则表达式优化
SELECT *
FROM (SELECT p.ID AS 平面图ID, z.flatplan_id, z.FLOOR_ID AS 层ID, b.ID AS 自然幢ID, 
  REPLACE(REPLACE(b.BUILDING_NAME, '/', '\'), '\', '_') AS 幢名, 
  b.LOCATION_NAME AS 幢坐落, EP.PROJECT_NAME AS 项目名称, 
  p.DWGFILENAME AS 文件名, p.DWGFILE
  FROM (SELECT f.FLOOR_ID, h.flatplan_id, f.NATURE_NUMBER, f.LAND_ID
  FROM E_FLOOR f INNER JOIN
  (SELECT floor_id, MAX(flatplan_id) AS flatplan_id
  FROM e_floor_flatplan
  GROUP BY floor_id) h ON f.FLOOR_ID = h.floor_id) z INNER JOIN
  E_BUILDING_SRC b ON z.LAND_ID = b.LAND_ID AND 
  z.NATURE_NUMBER = b.NATURE_NUMBER INNER JOIN
  E_FLATPLAN p ON p.ID = z.flatplan_id INNER JOIN
  E_LAND L ON L.LAND_ID = b.LAND_ID INNER JOIN
  E_PROJECT EP ON EP.PROJECT_ID = L.PROJECT_ID) j
WHERE (SUBSTRING(幢名, 10, 1) = '_') OR
  (SUBSTRING(幢名, 11, 1) = '_') OR
  (SUBSTRING(幢名, 12, 1) = '_') OR
  (SUBSTRING(幢名, 13, 1) = '_') OR
  (SUBSTRING(幢名, 14, 1) = '_') OR
  (SUBSTRING(幢名, 15, 1) = '_') OR
  (SUBSTRING(幢名, 16, 1) = '_')



where 后面的条件能否用正则表达式优化,要在SQLserver上执行

------解决方案--------------------
mssql本身沒有正則表達式,除非你建立sql項目中才可能會用到正則,或者你用oracle就有正則了

------解决方案--------------------
SQL code
where patindex('%[_]%', @string) >= 10 or patindex('%[_]%', @string) <= 16

------解决方案--------------------
SQL code
where patindex('%[_]%', @string) >= 10 and patindex('%[_]%', @string) <= 16

------解决方案--------------------
SQL code
where patindex('_________%[_]%', @string) >= 1 and patindex('_________%[_]%', @string) <= 6