Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to reference this ?

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2012-02-13 : 11:18:41
Hi there,

Table like this:

ID | Code | Abbrev |
1 | 231 | BBV |
2 | 232 | CCD |
3 | 233 | BPG |
...

I already use code in where clause as:
where code between 231 and 233
here code column is varchar data type. What if I need to use Abbrev instead? I mean how can i get same effect as above clause with column of Abbrev? Thanks in advance.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-13 : 11:24:27
where Abbrev in ('BBV','CCD','BPG')

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-02-13 : 12:24:17
Thanks visakh16.
If the situation is:

ID|Code |
1 | 231 | ...
2 | 232 |
3 | 233 |
4 | BBV | ...
5 | CCD | ...

and actually BBV=231, CCD=232, etc. can I use just number code to represent the abbrev letter by a new table with these two cols there referenced each other? Maybe I can use this:
where code between 231 and 233 or code in ('BBV','CCD','BPG')
But I want just use numbered Code to represent letter Code.
Thanks.


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-13 : 12:26:38
yep...that where clause should work fine.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-02-13 : 12:29:23
Sorry I editted it even after you replied.
What if I only want to use numbered Code to represent letter Code.
Thanks.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-13 : 12:39:40
quote:
Originally posted by allan8964

Sorry I editted it even after you replied.
What if I only want to use numbered Code to represent letter Code.
Thanks.




then you need to have a separate mapping table where you map all codes (number as well as letter) to their number equivalents and then use it for seraching
so it will have column

Code MappedCode
-------------------------------
BBV 231
CCD 232
BPG 233
231 231
232 232
233 233
..

then use query like


SELECT t.
FROM YourTable t
JOIN MappingTable m
ON m.Code=t.Code
WHERE m.MappedCode BETWEEN 231 and 233



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-13 : 12:43:00
Does that mapping table exist already? How do you KNOW how to make that mapping if it doesn't?


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-02-13 : 17:46:19
Thanks gents. The table is not there, I thought I need a mapping table but not sure how to manipulate the codes.
Thanks visakh16 again.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-13 : 18:11:36
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -