Reputation: 7
I am using a SQL substring statement to return periods after a Middle Name initial, such as, "H." When there is no Middle name to return, the period still returns in a field that should be null.
Here is my statement:
substring([Middle Name],1,1)+('.')
Upvotes: 1
Views: 78
Reputation: 172398
Try this:
substring(nullif([Middle Name], ''), 1, 1) + '.'
You may check the NULLIF
Returns a null value if the two specified expressions are equal.
Upvotes: 2
Reputation: 175758
As NULL + anything == NULL
how about:
select left(nullif([Middle Name], ''), 1) + '.'
Or
select case when [Middle Name] is not null then left([Middle Name], 1) + '.' end
Upvotes: 3