How to compare two different data type variables in where condition of SQL query
I have one column which is integer
in first table and second is varcha
. Now I need to compare these 2 variables which ar开发者_StackOverflowe of different data types and retrieve data if it matches.
Please could please help me out how to work on this?
Please specify which DBMS you're using please.
Sql Server? CAST / CONVERT
EDIT:
Well, I see you mentioned Sql*Plus above which to me says Oracle so for the sake of thoroughness. CAST appears to be what you're looking for. CONVERT does something a bit different in Oracle so you'd be doing something like
CAST(@YourStringValue AS INTEGER) = @YourOtherIntegerValue
or whatever datatype you want to cast your column to and then compare it against your other value. The syntax may be off a bit as I'm no Oracle guy but this should point you in the right direction.
If it is about MS SQL, then it will cast those for you implicitly, no need to cast or convert.
declare @IntVariable int
set @IntVariable = 3
declare @VarcharVariable varchar(10)
set @VarcharVariable = '3'
select case when @IntVariable = @VarcharVariable then 1 else 0 end
You can either cast varchar to int or int to varchar and then compare.
Short example of casting:
CAST(ColumnName as datatype)as CertainName
Here CertainName
means u can show that to display as your new column name
if yo are using c# java or other strict type programming languase then you will have to convert the int type into string then you will be able to compare them if you are using PHP then no conversion will be required
精彩评论