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]