Question: In the SPOJ problem [PPATH](http://www.spoj.com/problems/PPATH/) we are given two four-digit prime numbers and we have to convert, in the least possible steps, the first prime into the second one by changing a single digit at a time and at each step the number should be prime. We have to output 'IMPOSSIBLE' if the primes cannot be converted in said fashion. 

However, solutions to the problem in which the impossible case is not even considered have been accepted, which leads one to conjecture that every four-digit prime can be converted into any other four-digit prime in the specified manner. I was unable to prove it. Is it true? How can we prove it formally? Also, is there a general result for n-digit primes?

Answer: 
You can use CASE to branch between two scenarios when Sequence is null or not.
You can also use ISNULL and COALESCE for null checking

Added code sample, as requested in comments

```
REPLACE(STR(temp.AdminTestId,5),' ','0') + '-' + 
REPLACE(STR(A.UserTestId,5),' ','0') +  
ISNULL('-' + REPLACE(STR(A.Sequence,2),' ','0'),'') as TestId,

```
Concatenating null values to a string will return a null string in Sql server.
The solution is usually to use Sql server's built in function to convert null values to empty strings, so your code would look like this:

```
REPLACE(COALESCE(STR(temp.AdminTestId,5), ''),' ','0') + '-' + 
REPLACE(COALESCE(STR(A.UserTestId,5), ''),' ','0') + '-' + 
REPLACE(COALESCE(STR(A.Sequence,2), ''), ''),' ','0') as TestId,

```

The [COALESCE](https://msdn.microsoft.com/en-us/library/ms190349.aspx) function is Sql server will return the first argument it receives that is not `NULL`. 

**Edit**

To answer your comment, taking advantage of the fact that concatenating null to a string will return null, you can simply concatenate the hyphen to `A.Sequence`:

```
REPLACE(COALESCE(STR(temp.AdminTestId,5), ''),' ','0') + '-' + 
REPLACE(COALESCE(STR(A.UserTestId,5), ''),' ','0') + 
REPLACE(COALESCE('-' + STR(A.Sequence,2), ''),' ','0') as TestId,

```