Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
use set condition in sql server.

query : select 'abc'+null,

execution answer : null,

but i need answer abc while using set condition in sql server.

What I have tried:

while executing answer is null, but i need there 'abc'. while using set condition.
Posted
Updated 25-Jul-19 3:18am

Your question is not particularly clear, but I'm guessing that "using set condition" means you're expected to change the SQL Server options for your query. In particular:
SET CONCAT_NULL_YIELDS_NULL (Transact-SQL) - SQL Server | Microsoft Docs[^]
SQL
SET CONCAT_NULL_YIELDS_NULL OFF;
SELECT 'abc' + Null; -- Result: "abc"
NB: This is a bad idea. As the documentation says, this option will be removed in a future version of SQL Server, and should be avoided.
 
Share this answer
 
In SQL NULL is supreme - any operation involving NULL will yield NULL - so if you attempt to add two columns together if either is Null then the result is NULL as well.

Try this:
SQL
SELECT CONCAT('abc', NULL)
And you will get what you wanted: 'abc'
 
Share this answer
 
Comments
Rayalacheruvu Hemanth 24-Jul-19 21:55pm    
your answar is correct, but it's not going sute my question.
i ware already told in my question use "set" condition.
OriginalGriff 25-Jul-19 1:11am    
So use CONCAT in your SET command ... what part of this is difficult for you?

DECLARE @X NVARCHAR(MAX);
SET @X = CONCAT('abc', NULL);
SELECT @X;
Adding onto Original Griff's answer; and depending on your needs, you could set up a value to replace null(s) as needed. I typically use an empty varchar for this.
SQL
DECLARE @PossibleNull    VARCHAR(32)
DECLARE @NullReplacement VARCHAR(1) = ''

SELECT 'abc' + Concat(@PossibleNull, @NullReplacement)
If you are wondering, Concat is preferred over the MS SQL only IsNull function as it is part of the ANSI language spec and can also be chained over multiple "replacement" values.
 
Share this answer
 
Comments
Rayalacheruvu Hemanth 24-Jul-19 22:01pm    
yeah, its true,
but its not going to answer my question.
MadMyche 24-Jul-19 22:10pm    
Then you need to use the "Improve Question" widget to improve your question; and include ALL of the relevant information. It would be recommended to also include the specific code you are working with in the "What I've Tried" block.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900