title: SQL Server - Return All possible combinations of a 4 digit number passed to a stored procedure
body: <p>I have a task to write a stored procedure or a function to return all possible combinations of a 4 digit number.</p>
  
  <p>For example, if I pass <code>1234</code> to the stored procedure or function, it should return 4 digit numbers (all possible combinations), like </p>
  
  <pre><code>1123, 1112, 1324, 1342, 2134, 2234 
  </code></pre>
  
  <p>and so on.</p>
  
  <p>It can be of 4 digits only.</p>
  
  <p>I have been doing this with using LIKE operator:</p>
  
  <pre><code>select * 
  from Table 
  where mynumber like '%1%'
    and mynumber like '%2%'
    and mynumber like '%3%'
    and mynumber like '%4%'
  </code></pre>
  
  <p>but the problem is, I have hardcoded the numbers 1,2,3 and 4.</p>
  
  <p>The number can be anything.</p>
  
  <p>And these many LIKE operators can also impact the performance on a large table.</p>
  
  <p>Can anybody give me some generic query to get the combinations?</p>
  
  <p>Thanks in advance.</p>