SQL Server string non-null Judgment implementation method _mssql

Source: Internet
Author: User
Copy Code code as follows:

CREATE function GetColor
(@ well number varchar (50), @ Layer varchar (50))
Returns varchar (200)
As
Begin
declare @sql varchar; set @sql = '
--distinct
Select @sql = @sql + color + ', ' from (
Select distinct color from yourtablename where
Well number =@ well number and layer =@ layer and color is not NULL
) AA
Set @sql =substring (@sql, 1,len (@sql)-1)
Return @sql
End


I found that the set @sql =substring (@sql, 1,len (@sql)-1) is sometimes an error, because @sql at substring time, it is an empty string.

In the function, how to judge the substring before the @sql is empty?

If @sql is not null and Len (@sql) >= 1
Set @sql =substring (@sql, 1,len (@sql)-1)
Else
...

Copy Code code as follows:


If (@sql is not null)
Set @sql =substring (@sql, 1,len (@sql)-1)
Return @sql


Copy Code code as follows:

If IsNull (@sql, ') <> '
Set @sql =substring (@sql, 1,len (@sql)-1)


"SELECT * FROM DB where img? NOT NULL" This is to select all img not empty content
"SELECT * FROM DB where img is null" This is to select all img-Empty content

If you want the SQL Judge value to be empty, a default value can be used in this way
SELECT top 1 IsNull (yu_yse_commodity_kd,0) from [Yu_records]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.