Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have a table as id int, age int, name varchar(20), date datetime
and I have created a stored procedure as

SQL
create procedure search (@text varchar(50))
as
  select *from employees where 
    cast(id as varchar)=cast(@text as varchar) or
    cast(age as varchar)=cast(@text as varchar) or
    cast(name as varchar)=cast(@text as varchar) or
    cast(date as varchar(30))=cast(@text as varchar)


When I search for id, name, age it is giving me the correct output but
when I search for date it is not giving me any output. Even when that date exist in that table.

How can I search for a date?

Please help
Posted
Updated 10-Nov-10 7:51am
v3

I am by no means an SQL expert but a quick google gave me this[^].

From a quick reading I would suggest translating your @text into a DateTime for comparison, rather than converting the DateTime into text.
 
Share this answer
 
Comments
software_Engi08 10-Nov-10 14:05pm    
ok ,fine when i change the datatype of @text to datetime it is working but then it is not working for id,name,
age now what should i do to all work .please help
I think you might have misunderstood, or I did not explain properly :) .

I meant that you should change your SP to something like

SQL
create procedure search (@text varchar(50))
as
  select *from employees where
    cast(id as varchar)=cast(@text as varchar) or
    cast(age as varchar)=cast(@text as varchar) or
    cast(name as varchar)=cast(@text as varchar) or
    date = CAST(@text AS datetime))


As I said before, I am not an proficient in SQL, so the option I have shown for the date comparison may not be correct for your data. It may be that one of the SELECT convert(datetime,@text,4) would be a better fit.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900