SQL server String non-empty judgment Implementation Method

Source: Internet
Author: User

Copy codeThe Code is as follows:
CREATE function getcolor
(@ Varchar (50), @ varchar (50 ))
Returns varchar (200)
As
Begin
Declare @ SQL varchar (2000); set @ SQL =''
-- Distinct
Select @ SQL = @ SQL + color + ', 'From (
Select distinct color from yourtablename where
Well id = @ well ID and horizon = @ horizon 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) Statement sometimes reports an error because @ SQL is using substring, sometimes it is a Null String

In the function, how can I determine whether @ SQL is empty before substring?

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

Copy codeThe Code is as follows:

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


Copy codeThe Code is as follows:
If isnull (@ SQL, '') <>''
Set @ SQL = substring (@ SQL, 1, len (@ SQL)-1)


"Select * from db where img is not null" is the content that selects all img not empty.
"Select * from db where img is null" this is to select all the content with the img being empty.

If you want the SQL statement to determine whether the value is null, a default value can be used.
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.