Hi,
I want to search an IP address from DB. Query is like below;
select * FROM Log WHERE IP='3.3.3.3'
But I get this error,
"The data types ntext and varchar are incompatible in the equal to operator."
IP colon's format is "nText".
How may I search IP addresses?
regards.
Loading

VulpesRank #55 · 29.2K reputationPosted Apr 3, 2012, 12:58 AM EDT
select * FROM Log WHERE CAST(IP as nvarchar(max)) = '3.3.3.3'
EDIT: Senthil beat me to it :)
VulpesRank #55 · 29.2K reputationPosted Apr 3, 2012, 12:44 AM EDT
yokzuRank #5,421 · 300 reputationPosted Apr 3, 2012, 12:50 AM EDT
"SELECT * FROM Log WHERE IP=CAST('"+TextBox3.Text.ToString()+"' as ntext)";
But I get this error; The data types ntext and ntext are incompatible in the equal to operator.
SenthilkumarRank #387 · 6.5K reputationPosted Apr 3, 2012, 12:57 AM EDT
If you defined a column as integer then you can directly search by the value.
CREATE TABLE Example
(
ID INT IDENTITY(1,1) PRIMARY KEY,
Name Varchar(10)
}
Here the Id is integer column and name is varchar.
When you write the search ID then
SELECT * FROM Example WHERE ID=2
If you want to search the exactly name then
SELECT * FROM Example WHERE name='Senthil'
If you want to search the similar kind of names then
SELECT * FROM Example WHERE name LIKE '%Senthil%' --It will search the senthil which consist some thing like 'kumarsenthil'
SELECT * FROM Example WHERE name LIKE '%Senthil%' --It will search by name starts Senthil like 'Senthilkumar','SenthilRaja'
SELECT * FROM Example WHERE name LIKE 'Senthil%' -- It will search the name some thing like 'KumarSenthil'
I come to your example,
So we need to make both columns are equal data type to evaluate the expression.
SELECT * FROM Log WHERE CAST(IP AS nvarchar(4000))='3.3.3.3'
This will help you to fix.
If it is useful then mark it as "Correct Answer"
SenthilkumarRank #387 · 6.5K reputationPosted Apr 3, 2012, 1:02 AM EDT
yokzuRank #5,421 · 300 reputationPosted Apr 3, 2012, 2:34 AM EDT