Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
May 2, 2000

Using Where on Fields of Differing Types

Question:
I need to extract information using WHERE on fields in two different tables: one a text field, the other a numeric field. Both hold only numeric data. How can I do this?

Answer:
If by the term text field you mean varchar, you can convert the column data using the STR function. And in the case of SQL Server, it will use the implicit conversion. So if you have this table:

NumberField int,           value 230
VarcharField varchar(255), value 230

These SQL statements will both work:
SELECT * FROM TestTable 
WHERE STR(NumberField) = 230
SELECT * FROM TestTable 
WHERE STR(VarcharField) = 230
If one of your fields is the actual text data type, you'll have to use the Cast function to convert it to varchar. But this involves a slightly different syntax, so you'll have to query the column data type or have some conditional logic if the column is numeric or text. For example:
SELECT * FROM TestTable 
WHERE CAST(TextField AS varchar) = 230
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date