如何在 searchWord 存储过程中使用 NormalizeChars 存储过程

发布于 2024-10-26 12:48:12 字数 3852 浏览 2 评论 0原文

我想

ALTER PROCEDURE [dbo].[SearchWord] (@Word            NVARCHAR(50),
                                    @PageNumber      INT,
                                    @ProductsPerPage INT,
                                    @HowManyResults  INT OUTPUT)
AS
  SET @Word = '%' + RTRIM(@Word) + '%';

  DECLARE @Results TABLE (
    ProductsId          INT,
    ProductsCode        NVARCHAR(250),
    ProductsDesc        NVARCHAR(MAX),
    ProductsIngredients NVARCHAR(MAX),
    ProductsName        NVARCHAR(250),
    ProductsPhoto       NVARCHAR(MAX),
    ProductsPrice       MONEY,
    ProductsWeight2     FLOAT,
    RowNumber           INT)

  -- Obtain the matching products
  INSERT INTO @Results
  SELECT DISTINCT T1.ProductsID,
                  T1.ProductsCode,
                  LOWER(T1.ProductsDesc)                      asProductsDesc,
                  T1.ProductsIngredients,
                  LOWER(T1.ProductsName)                      AS ProductsName,
                  T1.ProductsPhoto,
                  T1.ProductsPrice,
                  T1.ProductsWeight2,
                  ROW_NUMBER() OVER(ORDER BY T1.ProductsName) RowNumber
  FROM   (SELECT ProductsID,
                 ProductsCode,
                 LOWER(ProductsDesc) AS ProductsDesc,
                 ProductsIngredients,
                 LOWER(ProductsName) AS ProductsName,
                 ProductsPhoto,
                 ProductsPrice,
                 ProductsWeight2
          FROM   Products
          WHERE  ProductsName LIKE @Word) AS T1
         FULL OUTER JOIN (SELECT *
                          FROM   Products
                          WHERE  ProductsDesc LIKE @Word) AS T2
           ON T1.ProductsID = T2.ProductsID
  ORDER  BY T1.ProductsPrice

  DELETE FROM @Results
  WHERE  NULLIF([ProductsName], '') IS NULL

  SELECT @HowManyResults = COUNT(*)
  FROM   @Results

  INSERT INTO @Results
  SELECT DISTINCT ProductsId,
                  ProductsCode,
                  ProductsDesc,
                  ProductsIngredients,
                  LOWER(ProductsName) AS ProductsName,
                  ProductsPhoto,
                  ProductsPrice,
                  ProductsWeight2,
                  RowNumber
  FROM   @Results

  SELECT DISTINCT ProductsId,
                  ProductsCode,
                  ProductsDesc,
                  ProductsIngredients,
                  LOWER(ProductsName) AS ProductsName,
                  ProductsPhoto,
                  ProductsPrice,
                  ProductsWeight2,
                  RowNumber
  FROM   @Results R
  WHERE  R.RowNumber > ( @PageNumber - 1 ) * @ProductsPerPage
         AND R.RowNumber <= @PageNumber * @ProductsPerPage
  ORDER  BY R.ProductsPrice ASC  

使用此过程

ALTER PROCEDURE normalizeChars 
(@NAME NVARCHAR(100)) 
AS
BEGIN
  DECLARE @TempString NVARCHAR(100)
  SET @TempString = @NAME 
  SET @TempString = LOWER(@TempString)
  SET @TempString =  REPLACE(@TempString,'à', 'a')
  SET @TempString =  REPLACE(@TempString,'è', 'e')
  SET @TempString =  REPLACE(@TempString,'é', 'e')
  SET @TempString =  REPLACE(@TempString,'ì', 'i')
  SET @TempString =  REPLACE(@TempString,'ò', 'o')
  SET @TempString =  REPLACE(@TempString,'ù', 'u')
  SET @TempString =  REPLACE(@TempString,'ç', 'c')
  SET @TempString =  REPLACE(@TempString,'''', '')
  SET @TempString =  REPLACE(@TempString,'`', '')
  SET @TempString =  REPLACE(@TempString,'-', '')
  SET @TempString =  REPLACE(@TempString,'ά','α')
  SET @TempString =  REPLACE(@TempString,'έ','ε')
  SET @TempString =  REPLACE(@TempString,'ί','ι')
  SET @TempString =  REPLACE(@TempString,'ό','ο')
  SET @TempString =  REPLACE(@TempString,'ή','η')
  SET @TempString =  REPLACE(@TempString,'ύ','υ')
  RETURN @TempString
END

on ProductsName and ProductsDesc

从该函数中删除变音符号是否可能以及如何实现?

I want to remove the diacritics from this function

ALTER PROCEDURE [dbo].[SearchWord] (@Word            NVARCHAR(50),
                                    @PageNumber      INT,
                                    @ProductsPerPage INT,
                                    @HowManyResults  INT OUTPUT)
AS
  SET @Word = '%' + RTRIM(@Word) + '%';

  DECLARE @Results TABLE (
    ProductsId          INT,
    ProductsCode        NVARCHAR(250),
    ProductsDesc        NVARCHAR(MAX),
    ProductsIngredients NVARCHAR(MAX),
    ProductsName        NVARCHAR(250),
    ProductsPhoto       NVARCHAR(MAX),
    ProductsPrice       MONEY,
    ProductsWeight2     FLOAT,
    RowNumber           INT)

  -- Obtain the matching products
  INSERT INTO @Results
  SELECT DISTINCT T1.ProductsID,
                  T1.ProductsCode,
                  LOWER(T1.ProductsDesc)                      asProductsDesc,
                  T1.ProductsIngredients,
                  LOWER(T1.ProductsName)                      AS ProductsName,
                  T1.ProductsPhoto,
                  T1.ProductsPrice,
                  T1.ProductsWeight2,
                  ROW_NUMBER() OVER(ORDER BY T1.ProductsName) RowNumber
  FROM   (SELECT ProductsID,
                 ProductsCode,
                 LOWER(ProductsDesc) AS ProductsDesc,
                 ProductsIngredients,
                 LOWER(ProductsName) AS ProductsName,
                 ProductsPhoto,
                 ProductsPrice,
                 ProductsWeight2
          FROM   Products
          WHERE  ProductsName LIKE @Word) AS T1
         FULL OUTER JOIN (SELECT *
                          FROM   Products
                          WHERE  ProductsDesc LIKE @Word) AS T2
           ON T1.ProductsID = T2.ProductsID
  ORDER  BY T1.ProductsPrice

  DELETE FROM @Results
  WHERE  NULLIF([ProductsName], '') IS NULL

  SELECT @HowManyResults = COUNT(*)
  FROM   @Results

  INSERT INTO @Results
  SELECT DISTINCT ProductsId,
                  ProductsCode,
                  ProductsDesc,
                  ProductsIngredients,
                  LOWER(ProductsName) AS ProductsName,
                  ProductsPhoto,
                  ProductsPrice,
                  ProductsWeight2,
                  RowNumber
  FROM   @Results

  SELECT DISTINCT ProductsId,
                  ProductsCode,
                  ProductsDesc,
                  ProductsIngredients,
                  LOWER(ProductsName) AS ProductsName,
                  ProductsPhoto,
                  ProductsPrice,
                  ProductsWeight2,
                  RowNumber
  FROM   @Results R
  WHERE  R.RowNumber > ( @PageNumber - 1 ) * @ProductsPerPage
         AND R.RowNumber <= @PageNumber * @ProductsPerPage
  ORDER  BY R.ProductsPrice ASC  

using this procedure

ALTER PROCEDURE normalizeChars 
(@NAME NVARCHAR(100)) 
AS
BEGIN
  DECLARE @TempString NVARCHAR(100)
  SET @TempString = @NAME 
  SET @TempString = LOWER(@TempString)
  SET @TempString =  REPLACE(@TempString,'à', 'a')
  SET @TempString =  REPLACE(@TempString,'è', 'e')
  SET @TempString =  REPLACE(@TempString,'é', 'e')
  SET @TempString =  REPLACE(@TempString,'ì', 'i')
  SET @TempString =  REPLACE(@TempString,'ò', 'o')
  SET @TempString =  REPLACE(@TempString,'ù', 'u')
  SET @TempString =  REPLACE(@TempString,'ç', 'c')
  SET @TempString =  REPLACE(@TempString,'''', '')
  SET @TempString =  REPLACE(@TempString,'`', '')
  SET @TempString =  REPLACE(@TempString,'-', '')
  SET @TempString =  REPLACE(@TempString,'ά','α')
  SET @TempString =  REPLACE(@TempString,'έ','ε')
  SET @TempString =  REPLACE(@TempString,'ί','ι')
  SET @TempString =  REPLACE(@TempString,'ό','ο')
  SET @TempString =  REPLACE(@TempString,'ή','η')
  SET @TempString =  REPLACE(@TempString,'ύ','υ')
  RETURN @TempString
END

on ProductsName and ProductsDesc

is it possible and how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不美如何 2024-11-02 12:48:12

您说您在 Greek_CI_AI 上。在这种情况下,大部分映射已经发生(只有前 3 个映射没有发生)。

;WITH T(col1,col2) AS
(
  SELECT N'''',N'' UNION ALL
  SELECT N'`',N'' UNION ALL
  SELECT N'-',N'' UNION ALL

  SELECT N'à',N'a' UNION ALL
  SELECT N'è',N'e' UNION ALL
  SELECT N'é',N'e' UNION ALL
  SELECT N'ì',N'i' UNION ALL
  SELECT N'ò',N'o' UNION ALL
  SELECT N'ù',N'u' UNION ALL
  SELECT N'ç',N'c' UNION ALL
  SELECT N'ά',N'α' UNION ALL
  SELECT N'έ',N'ε' UNION ALL
  SELECT N'ί',N'ι' UNION ALL
  SELECT N'ό',N'ο' UNION ALL
  SELECT N'ή',N'η' UNION ALL
  SELECT N'ύ',N'υ'
)
SELECT *
FROM T 
WHERE col1<>col2  COLLATE Greek_CI_AI 

返回

col1 col2
---- ----
'    
`    
-    

此外,您不需要使用 LOWER,因为排序规则不区分大小写。

如果您确实需要忽略搜索中剩余的​​ 3 个字符,您可以使用标量 UDF,如下所示

CREATE FUNCTION dbo.normalizeChars 
(@Name NVARCHAR(100)) 
RETURNS NVARCHAR(100)
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS 
BEGIN
  SET @Name =  REPLACE(@Name,'''', '')
  SET @Name =  REPLACE(@Name,'`', '')
  SET @Name =  REPLACE(@Name,'-', '')
  RETURN @Name
END

You say you are on Greek_CI_AI. In that case most of your mappings already happen (Only the top 3 don't).

;WITH T(col1,col2) AS
(
  SELECT N'''',N'' UNION ALL
  SELECT N'`',N'' UNION ALL
  SELECT N'-',N'' UNION ALL

  SELECT N'à',N'a' UNION ALL
  SELECT N'è',N'e' UNION ALL
  SELECT N'é',N'e' UNION ALL
  SELECT N'ì',N'i' UNION ALL
  SELECT N'ò',N'o' UNION ALL
  SELECT N'ù',N'u' UNION ALL
  SELECT N'ç',N'c' UNION ALL
  SELECT N'ά',N'α' UNION ALL
  SELECT N'έ',N'ε' UNION ALL
  SELECT N'ί',N'ι' UNION ALL
  SELECT N'ό',N'ο' UNION ALL
  SELECT N'ή',N'η' UNION ALL
  SELECT N'ύ',N'υ'
)
SELECT *
FROM T 
WHERE col1<>col2  COLLATE Greek_CI_AI 

Returns

col1 col2
---- ----
'    
`    
-    

Also you don't need to use LOWER as the collation is case insensitive.

If you do need to ignore the remaining 3 characters in your search you could use a scalar UDF as below

CREATE FUNCTION dbo.normalizeChars 
(@Name NVARCHAR(100)) 
RETURNS NVARCHAR(100)
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS 
BEGIN
  SET @Name =  REPLACE(@Name,'''', '')
  SET @Name =  REPLACE(@Name,'`', '')
  SET @Name =  REPLACE(@Name,'-', '')
  RETURN @Name
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文