Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
In sql database table, i have one table name Priority.In that 3 columns, id , name, registered date.For registered date , default value is getdate() which automatically generates the record created date and time.Now in grid view, i want to search records using this registered date.In grid view, i have dropdown search by using name and date.While searching with this query, i'm getting no data found ,if i search with date but i getting correct data if i search with name.

What I have tried:

CREATE PROC [dbo].[SEARCHPRIOR]
@SearchBy   varchar(50),
@SearchVal  varchar(50),

AS
BEGIN
IF @SearchBy = '1'
           BEGIN
                SELECT * FROM PRIORITY WHERE P_NAME like '%' + @SearchVal + '%'
          END

  ELSE IF @SearchBy = '2'
          BEGIN
                SELECT * FROM PRIORITY WHERE P_TS like '%' + @SearchVal + '%'
          END

ELSE
        BEGIN
                SELECT * FROM PRIORITY 
        END
END


I have tried searching with this query but getting no results found in grid view. This is how table look like in SQL Server:

P_ID     P_NAME    P_TS
-------------------------------------------
1        High      2019-04-23 16:17:08.097
2        Medium    2019-04-24 16:17:23.070                                                               
3        Low       2019-04-25 16:17:32.420
Posted
Updated 26-Apr-19 4:02am
v3
Comments
Animesh Datta 26-Apr-19 9:49am    
what the datetime format you are passing in @SearchVal when @SearchBy = '2'
Member 14185275 26-Apr-19 9:55am    
datetime format passing as input is yyyy-mm-dd

1 solution

Hello ,
I would suggest you to cast the @P_TS value as date format like this way
SELECT * FROM PRIORITY WHERE 
FORMAT(P_TS, 'yyyy-MM-dd') like '%' + @SearchVal + '%' 
//FORMAT is introduced in SQL Server 2012. 

Or you may used Convert function as well
SELECT * FROM PRIORITY WHERE 
convert(varchar, P_TS, 126) like '%' + @SearchVal + '%' 

Here I am assuming you are passing @SearchVal in 'yyyy-MM-dd' format.
Hope it may helps you.
Thanks
 
Share this answer
 
Comments
Member 14185275 26-Apr-19 10:09am    
Thank u so much Animesh, its working fine.
thank u for ur valuable response.
Animesh Datta 26-Apr-19 20:59pm    
Glad to help you

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