如何使用sql server之类的查询?

时间:2021-08-19 15:43:58

I only want to get all data from table start with sh, i use this query but it shows all data starts with 's'

我只想从表开始用sh启动所有数据,我使用此查询但它显示所有数据以's'开头

  Declare @search varchar    
  Set @search = 'sh'    
  BEGIN    
    SELECT * FROM tbl_master WHERE Name LIKE @search+'%'    
  END

2 个解决方案

#1


3  

If you declare a variable as varchar - you get a vachar(1) - exactly ONE character long!

如果你将一个变量声明为varchar - 你得到一个vachar(1) - 正好一个字符!

So if you assign sh to that variable, it really becomes just s ....

因此,如果你将sh分配给那个变量,它真的变成了......

You need to explicitly define a length for your varchar - ALWAYS!

您需要明确定义varchar的长度 - 总是!

 DECLARE @search VARCHAR(20)

 SET @search = 'sh'    

 SELECT * FROM tbl_master WHERE Name LIKE @search+'%'    

#2


0  

Please Specify Varchar Length

请指定Varchar长度

You can try also

你也可以试试

  Declare @search varchar(10)    
  Set @search = 'sh'    
  BEGIN    
    SELECT * FROM tbl_master WHERE LEFT(Name,2) = @search
  END

Fiddle Demo

#1


3  

If you declare a variable as varchar - you get a vachar(1) - exactly ONE character long!

如果你将一个变量声明为varchar - 你得到一个vachar(1) - 正好一个字符!

So if you assign sh to that variable, it really becomes just s ....

因此,如果你将sh分配给那个变量,它真的变成了......

You need to explicitly define a length for your varchar - ALWAYS!

您需要明确定义varchar的长度 - 总是!

 DECLARE @search VARCHAR(20)

 SET @search = 'sh'    

 SELECT * FROM tbl_master WHERE Name LIKE @search+'%'    

#2


0  

Please Specify Varchar Length

请指定Varchar长度

You can try also

你也可以试试

  Declare @search varchar(10)    
  Set @search = 'sh'    
  BEGIN    
    SELECT * FROM tbl_master WHERE LEFT(Name,2) = @search
  END

Fiddle Demo