TSQL get data regardless of Null value - sql

I have a stored procedure that is getting information from my employee table and returning the data.
There are 3 columns that are used:
B.[SiloDesc] + ' (' + B.[TitleDesc] + ') ' + B.[SkillSetDesc] as SkillSetDesc,
My issue is, if one of those happens to be null, it wont display any of the data. What is the best way to have it include the data regardless of if one of those fields are null.

You could use coalesce() or isnull() for each individual column... or you could simply use...
CONCAT ( string_value1, string_value2 [, string_valueN ] )
Takes a variable number of string arguments and concatenates them into a single string. It requires a minimum of two input values; otherwise, an error is raised. All arguments are implicitly converted to string types and then concatenated. Null values are implicitly converted to an empty string.
A.: Remove the parentheses when TitleDesc is null:
select concat(B.[SiloDesc], ' (' + B.[TitleDesc] + ')', ' ' + B.[SkillSetDesc])
Because of the way null is treated in sql, the expression ' (' + null + ')' results in null which concat() will treat as an empty string... which is kind of nice as it effectively removes the parentheses if the value is null.
B.: Keep the parentheses regardless:
select concat(B.[SiloDesc], ' (', B.[TitleDesc], ') ', B.[SkillSetDesc])
Samples:
select concat('john', ' (' + null + ')', ' adams') -- john adams
select concat('john', ' (test)', ' ' + null) -- john (test)
select concat('john', ' (the man)', ' adams') -- john (the man) adams

isnull(B.[SiloDesc], '')
+ ' (' + isnull(B.[TitleDesc], '') + ') '
+ isnull(B.[SkillSetDesc], '') as SkillSetDesc,

Related

How do I remove a value that I define if a row returns NULL within a CONCAT function?

I'm looking to remove a value that I define if a row returns NULL within a CONCAT function.
I'm not entirely sure how to go about it..
CONCAT('Floor:', ' ', l1.floor, ' ', 'Unit:', ' ', l1.unit_number, ' ', 'Buzzer:', ' ', l1.buzzer) as R_ADDRESS2,
I'd like each instance of values to be blank if the row is NULL.
So if l1.floor returns NULL then 'Floor:', ' ' don't showup in the column for R_ADDRESS2 either.
Same for l1.unit_number, and l1.buzzer.
How would I do that?
EDIT:
Here is how it is showing up currently:
r_address2
Floor: 10 Unit: Buzzer:
Floor: 2 Unit: 200 Buzzer:
Floor: Unit: Buzzer:
Use CONCAT_WS():
CONCAT_WS(' ',
('Floor: ' || l1.floor),
('Unit: ' || l1.unit_number),
('Buzzer: ' || l1.buzzer)
) as R_ADDRESS2,
|| returns NULL if any argument is NULL. However, CONCAT_WS() ignores NULL arguments, so the combination does what you want.
Here is a db<>fiddle.
concat function ignores nulls, "usual" concatenation does't. Use both of them.
CONCAT('Floor: ' + l1.floor + ' ', -- or cast(l1.floor as varchar) if it is int
'Unit: ' + l1.unit_number + ' ',
'Buzzer: ' + l1.buzzer) as R_ADDRESS2,

How do I catch a handle a null value in a concatenated string?

I have the following in a view
c2.title + ' ' + c2.initials + ' ' + c2.surname AS referredTo
As it stands now, if one of those values are null, then referredTo is just null. But for some of the records, the title, the initials, or the surname may be null. What is the best way to handle this so that is one or more of the values are null, it will return only the string with the non-null values. So for example, if title is null, it will only return "initals surname", if initials is null, it will return "title surname", if both are null, it will return "surname", etc, to cover any of those values being null. This is probably a really easy answer but I'm kinda new to SQL and having to learn as I go.
Thanks.
As suggested in the comment, put the White Space within the ISNULL:
SELECT ISNULL(Title + ' ', '') + ISNULL(Initials + ' ','') + ISNULL(Surname,'')
FROM YourTable;
That means that you don't end up with WhiteSpace at the front, or (possibly) two spaces between Title and Surname if Initials has a value of NULL.
Maybe you can use ISNULL, please check following statement
RTRIM(
LTRIM(
ISNULL(c2.title, '') + ' ' +
ISNULL(c2.initials, '') + ' ' +
ISNULL(c2.surname, '')
)
) AS referredTo
SET #FinalAnswer= ISNULL(#Col1, '') + ' ' +ISNULL(#Col2, '')
Try above approach.
Use Concat()
Select LTRIM(RTRIM(Concat(c2.title,' ',c2.initials,' ',c2.surname))) AS referredTo
FROM yourTable
DEMO

SELECT DISTINCT is omitting NULL values when not desired

I am trying to build a distinct string in a query, which works unless one of the values is NULL. I've tested removing LOCATION_ADDR_LINE_2, and the query will work just fine. When I do not SELECT DISTINCT, I find that LOCATION_ADDR_LINE_2 values are NULL. How can I gather these values in the SELECT DISTINCT even if NULL?
SELECT DISTINCT(LOCATION_ADDR_LINE_1 + ', ' + LOCATION_ADDR_LINE_2 + ', ' + LOCATION_CITY + ', ' + LOCATION_WORK_STATE) AS Addresses
FROM OracleReport
WHERE (LOCATION_ADDR_LINE_1 LIKE '%1135 Auto%' OR LOCATION_ADDR_LINE_1 LIKE '%199 Easy%')
Returns:
Addresses
NULL
SELECT DISTINCT(LOCATION_ADDR_LINE_1 + ', ' + LOCATION_CITY + ', ' + LOCATION_WORK_STATE) AS Addresses
FROM [OperationReport].[dbo].[OracleReport]
WHERE (LOCATION_ADDR_LINE_1 LIKE '%1135 Auto%' OR LOCATION_ADDR_LINE_1 LIKE '%199 Easy%')
Returns:
Addresses
1135 Auto...
189-199 Easy...
Assuming you don't mind text,,text,... (empty string) when a value is NULL...
SELECT DISTINCT(coalesce(LOCATION_ADDR_LINE_1,'') + ', ' +
coalesce(LOCATION_ADDR_LINE_2,'') + ', ' +
coalesce(LOCATION_CITY,'') + ', ' +
coalesce(LOCATION_WORK_STATE,'')) AS Addresses
FROM OracleReport
WHERE (LOCATION_ADDR_LINE_1 LIKE '%1135 Auto%'
OR LOCATION_ADDR_LINE_1 LIKE '%199 Easy%')
Coalesce will take the first non-null value and return it. It requires consistent data types and will early exit once the first non-null value in a series is encountered. (more details Oracle Differences between NVL and Coalesce)

Absolute value of varchar from table

I have a field with value= '''VALUE'''.when i am selecting it return me 'VALUE' with single quotes.
I want the VALUE in a varchar variable without quotes.how to achieve this without replace function.
THIS IS MY DYNAMIC QUERY WHICH IS IN THE TABLE
''''+'SELECT'+''''+'+'+''''''''''+'+ RTRIM(#start_Insert) +'+''' ''''+'''+'+ '+'''''''('''+' + RTRIM(#Column_List_Insert) +'+'''''''+'''+'+' +''''''')''''''+'+
''' ''''+''+ ''''''(''+ RTRIM(#Column_List_Insert) + ''''''+''+'''''')''''''+'+
''' + '''' VALUES(''''+ '' +#Actual_Values_Insert+ ''+'''')''''''+'' ''+' +
''' FROM ''+ ''['' +RTRIM(#source) + '']''+ '+
'''where (<col_name> BETWEEN ''''<col_value1>'''' AND ''''<col_value2>'''') AND <col_name3> <> ''''<col_value3>'''''''
How about using SUBSTRING(), and removing the leading and trailing single apostrophes?
SELECT SUBSTRING(field, 2, LEN(field)-2) FROM table
As #RahulTripathi alluded in his comment, I would prefer to just REPLACE() out the apostrophes:
SELECT REPLACE(field, '''', '') FROM table

Concatenating null to string using + results in null

I have the following SQL Query in Access:
SELECT ID, CurrencyName + ' (' + CurrencySymbol + ')' AS [Currency],
CurrencyLocation, CurrencySymbol FROM Currencies ORDER BY SortOrder
What I noticed is that I get a full table of results except if if the field CurrencySymbol is left NULL or empty. If the CurrencySymbol field is null, rather than Concatenate nothing, Access skips over the record and continues as shown below
.
Did I get something wrong or is there a better way to write this Query?
If you concatenate strings with +, string + NULL yields NULL.
If you concatenate strings with &, string & NULL yields string.
Thus, you have two options to fix this:
Option 1: CurrencyName + ' (' + Nz(CurrencySymbol, '') + ')'. Nz (NullToZero) converts Null values to its second argument.
Option 2: CurrencyName & ' (' & CurrencySymbol & ')'
You can use this fact to create an expression that only shows the parenthesis when a currency symbol is present (Credit for this idea goes to this blog post):
CurrencyName & (' (' + CurrencySymbol + ')') will yield Points and Euro (€).
That's because concatenating a string and NULL results in NULL.
SELECT ID, CurrencyName + ' (' + Iif(IsNull(CurrencySymbol), '', CurrencySymbol) + ')'
AS [Currency], CurrencyLocation, CurrencySymbol
FROM Currencies
ORDER BY SortOrder