Get all one column value through the function from the table in sql server.



if you want to get all one column value with comma-separated, you need to create function.


GO
/****** Object: UserDefinedFunction [dbo].[function_coalesce_post_category] Script Date: 25-04-2020 23:58:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
---select [dbo].[function_coalesce_post_category]()
ALTER FUNCTION [dbo].[function_coalesce_post_category]
(
--@postId AS nvarchar(50)
) RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @fcm VARCHAR(max);
SELECT @fcm=COALESCE(@fcm + ',', '') + (CAST([tbl_exampleTable].category AS varchar(max)))
FROM tbl_exampleTable
return @fcm
END
 
 
The COALESCE() function returns the first non-null value in a list. 
 
 
 

0 Comment's

Comment Form