Sql server 如何在SQL Server中使用nchar with like运算符
我正在尝试将like运算符用于Sql server 如何在SQL Server中使用nchar with like运算符,sql-server,Sql Server,我正在尝试将like运算符用于nchar(1)type列,下面是我的存储过程: Create procedure [dbo].[CouponSearch] ( @DiscountType nchar(1) )as begin set nocount on Select Couponcode from tblCoupons where DiscountType like '%'+@DiscountType+'%' set nocount off end 我在表tblCoupons中有2条记录,但
nchar(1)
type列,下面是我的存储过程:
Create procedure [dbo].[CouponSearch]
(
@DiscountType nchar(1)
)as
begin
set nocount on
Select Couponcode from tblCoupons where DiscountType like '%'+@DiscountType+'%'
set nocount off
end
我在表tblCoupons中有2条记录,但我调用proc CouponSearch'
它返回0条记录。蒂亚
我也试过线下练习,但效果不好
Select Couponcode from tblCoupons where DiscountType like N'%'+@DiscountType+'%'
您正在寻找处理
'
以及NULL
select * from tblCoupons
where DiscountType = IIF(@DiscountType = '' OR @DiscountType IS NULL,DiscountType,@DiscountType)
因此,您的程序变成:
Createprocedure [dbo].[CouponSearch]
(
@DiscountType nchar(1)
)as
begin
set nocount on
Select Couponcode from tblCoupons where DiscountType = IIF(@DiscountType = '' OR @DiscountType IS NULL,DiscountType,@DiscountType)
set nocount off
end
您正在寻找处理
'
以及NULL
的IIF
条件
select * from tblCoupons
where DiscountType = IIF(@DiscountType = '' OR @DiscountType IS NULL,DiscountType,@DiscountType)
因此,您的程序变成:
Createprocedure [dbo].[CouponSearch]
(
@DiscountType nchar(1)
)as
begin
set nocount on
Select Couponcode from tblCoupons where DiscountType = IIF(@DiscountType = '' OR @DiscountType IS NULL,DiscountType,@DiscountType)
set nocount off
end
或者,用于检查空值和空字符串 示例源
CREATE TABLE TBLCOUPONS
(
Couponid INT ,
Couponcode VARCHAR(10) ,
DiscountType NCHAR(1) ,
Discount INT
)
INSERT TBLCOUPONS
( Couponid, Couponcode, DiscountType, Discount )
VALUES ( 1, 'JT5326', '$', 5 ),
( 2, 'CY1990', '%', 9 )
GO
程序
CREATE PROCEDURE [dbo].[CouponSearch] ( @DiscountType NCHAR(1) )
AS
BEGIN
IF ( @DiscountType = '' )
BEGIN
SET @DiscountType = NULL
END
SELECT Couponcode
FROM tblCoupons
WHERE DiscountType = ISNULL(@DiscountType, DiscountType)
END
GO
执行
EXEC [CouponSearch] '%'
GO
EXEC [CouponSearch] '$'
GO
EXEC [CouponSearch] ''
GO
EXEC [CouponSearch] NULL
GO
结果
------------------EXEC [CouponSearch] '%'
Couponcode
----------
CY1990
(1 row(s) affected)
------------------EXEC [CouponSearch] '$'
Couponcode
----------
JT5326
(1 row(s) affected)
-------------------EXEC [CouponSearch] ''
Couponcode
----------
JT5326
CY1990
(2 row(s) affected)
-------------------EXEC [CouponSearch] NULL
Couponcode
----------
JT5326
CY1990
(2 row(s) affected)
或者,用于检查空值和空字符串 示例源
CREATE TABLE TBLCOUPONS
(
Couponid INT ,
Couponcode VARCHAR(10) ,
DiscountType NCHAR(1) ,
Discount INT
)
INSERT TBLCOUPONS
( Couponid, Couponcode, DiscountType, Discount )
VALUES ( 1, 'JT5326', '$', 5 ),
( 2, 'CY1990', '%', 9 )
GO
程序
CREATE PROCEDURE [dbo].[CouponSearch] ( @DiscountType NCHAR(1) )
AS
BEGIN
IF ( @DiscountType = '' )
BEGIN
SET @DiscountType = NULL
END
SELECT Couponcode
FROM tblCoupons
WHERE DiscountType = ISNULL(@DiscountType, DiscountType)
END
GO
执行
EXEC [CouponSearch] '%'
GO
EXEC [CouponSearch] '$'
GO
EXEC [CouponSearch] ''
GO
EXEC [CouponSearch] NULL
GO
结果
------------------EXEC [CouponSearch] '%'
Couponcode
----------
CY1990
(1 row(s) affected)
------------------EXEC [CouponSearch] '$'
Couponcode
----------
JT5326
(1 row(s) affected)
-------------------EXEC [CouponSearch] ''
Couponcode
----------
JT5326
CY1990
(2 row(s) affected)
-------------------EXEC [CouponSearch] NULL
Couponcode
----------
JT5326
CY1990
(2 row(s) affected)
如果您使用的是SQL Server 2012,则使用IIF()函数:
对于较低版本,您可以使用CASE
语句
如果您使用的是SQL Server 2012,则使用IIF()函数:
对于较低版本,您可以使用CASE
语句
你也可以试试这个:
DECLARE @DiscountType nchar(1)
SELECT COUPONCODE
FROM TBLCOUPONS
WHERE CASE WHEN COALESCE(@DISCOUNTTYPE,'')='' THEN 1 ELSE 0 END=1 OR DISCOUNTTYPE = @DISCOUNTTYPE
你也可以试试这个:
DECLARE @DiscountType nchar(1)
SELECT COUPONCODE
FROM TBLCOUPONS
WHERE CASE WHEN COALESCE(@DISCOUNTTYPE,'')='' THEN 1 ELSE 0 END=1 OR DISCOUNTTYPE = @DISCOUNTTYPE
请向我们展示表格中的数据,以及您如何调用该过程。共享示例输出数据。我在表格中有Couponcode和DiscountType,DiscountType包含%和$..,DiscountType类型为nchar(1)@SunilChaudhary:尝试我下面的回答中提到的
IIF
方法。请向我们展示表格中的数据,以及您如何调用该过程。共享示例输出数据。我在表中有Couponcode和DiscountType,DiscountType包含%和$…并且DiscountType的类型为nchar(1)@SunilChaudhary:尝试我下面的答案中提到的IIF
方法。谢谢@etsa,我使用的是2008,很简单,我试图替换%这就是为什么我用like运算符完全更改了它。@PrabhatG我会说“不”,但如果你有任何证据,请告诉我。@etsa:nevermind。只是在谷歌上搜索了一下:)IIF是case的语法糖。干杯。@esta如果我想用like运算符怎么办?如果你只是想更改最后一个条件或折扣类型,比如。。。你能做到。但是如果它是CHAR(1),我认为它是无用的……谢谢@etsa,我使用的是2008,很简单,我试图替换%,这就是为什么我用like操作符完全改变它的原因。@PrabhatG我会说“不”,但如果你有任何证据,请让我知道。@etsa:nevermind。只是在谷歌上搜索了一下:)IIF是case的语法糖。干杯。@esta如果我想用like运算符怎么办?如果你只是想更改最后一个条件或折扣类型,比如。。。你能做到。但是如果它是CHAR(1)我认为它没用…谢谢@Prabhat,但我用的是2008年,并且投票支持你的努力..谢谢@Prabhat,但是我用的是2008年,投票支持你的努力。。