Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing TSQL, how can I tell if my CHAR is last one in a sequence? Also, what if the letter sequences "rolls over" from 'z' to 'a'?
    primarykey
    data
    text
    <p>Let's say I have the following table in SQL Server 2005:</p> <pre><code>id | Date | Letter ----------------------------- 01 | 5/1/2009 | W 02 | 5/1/2009 | X 03 | 5/1/2009 | Y 04 | 5/1/2009 | Z 05 | 5/1/2009 | A 06 | 5/1/2009 | B 07 | 5/1/2009 | D 08 | 5/1/2009 | E 09 | 5/2/2009 | W 10 | 5/2/2009 | X 11 | 5/2/2009 | Y 12 | 5/2/2009 | Z 13 | 5/2/2009 | A 14 | 5/2/2009 | B 15 | 5/2/2009 | C 16 | 5/2/2009 | D 17 | 5/3/2009 | W 18 | 5/3/2009 | X </code></pre> <p>In my <a href="https://stackoverflow.com/questions/938357/using-tsql-can-i-increment-a-char1-column-by-one-and-use-it-in-a-left-outer-jo/938371#938371">previous question</a>, I asked how to figure out which letter sequence was missing. In my example it would letter C between row 6 and 7. I got the following answer and it works for the most part:</p> <pre><code>SELECT * from TABLE1 t1 LEFT OUTER JOIN TABLE2 t2 ON ASCII(t2.INTCol) - 1 = ASCII(t2.INTCol) AND t1.date = t2.date WHERE t2.id IS NULL </code></pre> <p>However, this solution does have two problems. It will return false positives in two cases.</p> <ol> <li><p>The letter sequence starts over at 'W' everyday. So, I get a false positive result for the last record of every day. That would be row 8 and 16 in my example data.</p></li> <li><p>Since the sequence starts over at 'W' everyday, I get a false positive for the 'Z' record because ('Z' + 1) gives me a '[', not an 'A'.</p></li> </ol> <p>For #2 I believe I can put in an "IF" statement that says: "If this is a Z, look for an A" but I'm open to any 'cleaner' solutions.</p> <p>I really have no idea how to do #1, so I'm really looking for help on that one. <strong>EDIT:</strong> Something I left out, doing a MAX(t1.id) isn't going to work, as the rows aren't always inserted in sequence. In fact, looking at my data, it's always inserted in [a-z] order even though the first record is the 'w'. I know it's lame, but I'm dealing with log files from dozens of million dollar phone switches. Not much I can do about it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload