使用TSQL,我可以增加一个CHAR(1)列一个,并用它在一个LEFT OUTER JOIN没有一个CASE语句? [英] Using TSQL, can I increment a CHAR(1) column by one and use it in a LEFT OUTER JOIN without a CASE statement?
问题描述
这个问题类似于我的<一个href="http://stackoverflow.com/questions/899502/can-i-use-sql-to-find-missing-numbers-in-the-example-table-i-give-below">last问题。除了这一次我使用的字母,而不是6位整数。我想找到走出序列字母。
This question is similar to my last question. Except this time I'm using letters rather than 6 digit integers. I want to find the out of sequence "letters".
让我们说我有以下数据:
Let's say I have the following data:
id | Date | Letter
-----------------------------
01 | 5/1/2009 | X
02 | 5/1/2009 | Y
03 | 5/1/2009 | Z
04 | 5/1/2009 | A
05 | 5/1/2009 | B
06 | 5/1/2009 | D
我想能够拿出一个查询,会告诉我应该有一排05和06之间的一排C。
I would like to be able to come up with a query that would tell me there should be a row with "C" in between row 05 and 06.
在我的最后一个问题(使用int)我提供类似于下面的解决方案的东西,和它的工作太棒了。
In my last question (using INTs) I was offered something similar to the following solution, and it worked great.
SELECT * from TABLE1 t1
LEFT OUTER JOIN TABLE2 t2 ON t2.INTCol - 1 = t2.INTCol AND t1.date = t2.date
WHERE t2.id IS NULL
那么,与字母(据我所知)我不能说(G - 1)。因此,有另一种方式我可以做到这一点?
Well, with letters (as far as I know) I can't say (G - 1). So, is there another way I can do this?
我使用的数据库为SQL Server 2005。我相信这是在PL / SQL使用翻译一个简单的解决方案,但我没有,我可以做任何事情,就像使用TSQL的事情。
The database I am using is SQL Server 2005. I believe there is an easy solution in PL/SQL that uses TRANSLATE, but I don't thing I can do anything like using TSQL.
推荐答案
您可以在CHAR(1)利用转换成它的ASCII数
You can convert the char(1) to its ascii number using
ASCII(Letter)
您可以通过再增加一本,并返回到使用CHAR(如有必要)的信中,这样你的code会是这样的:
You can then increment this by one and return it to a letter using CHAR (if necessary), so your code would be this:
SELECT * from TABLE1 t1
LEFT OUTER JOIN TABLE2 t2
ON ASCII(t1.INTCol) - 1 = ASCII(t2.INTCol)
AND t1.date = t2.date
WHERE t2.id IS NULL
这篇关于使用TSQL,我可以增加一个CHAR(1)列一个,并用它在一个LEFT OUTER JOIN没有一个CASE语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!