title: Relative Date Calculation Algorithm in SQL
body: <p>I have a requirement to calculate the next monthly run time of a job which can be specified using two parameters which can take values from </p>
  
  <p>Parameter 1:<br>
  1 for Sunday, 2 for Monday, 3 for Tuesday, 4 for Wednesday, 5 for Thursday, 6 for Friday, 7 for Saturday, 8 for Day, 9 for Weekday, 10 for Weekend day</p>
  
  <p>Parameter 2:<br>
  1 for First, 2 for Second, 4 for Third, 8 for Fourth, 16 for Last</p>
  
  <p>Based on these two parameters you can specify a run time like First weekday of the month or Last sunday of the month.</p>
  
  <p>How do I arrive at this date using a stored procedure which will know current date, para1 and para2</p>