Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
I want a query like this

select CONVERT(VARCHAR(20),[date] , 120) from timesheetentry 
where [date] LIKE  (SELECT convert(varchar(10), getdate(), 126))%


  i want to select the date where the date like in subquery can any help me to solve this
Posted

Try this...

SQL
SELECT CONVERT(VARCHAR,[date] , 120) DateColumn from timesheetentry 
WHERE CONVERT(VARCHAR,[date],126) LIKE ''+ CONVERT(VARCHAR, GETDATE(), 126))+'%'


Happy Coding :)
 
Share this answer
 
Comments
kesav prakash 23-Jul-13 6:02am    
thank u damo
damodara naidu betha 23-Jul-13 6:29am    
Welcome :)
Try this query

SQL
declare 
	@date varchar(10);
select 
	@date = convert(varchar(10), getdate(),126);
select 
	convert(varchar(20),[date] , 120) from timesheetentry 
where 
	[date]>= @date+' 00:00:00.000' and [date]< @date+' 23:59:59:998'
 
Share this answer
 
Above solutions work properly, but I think comparing date values as string (varchar) is not a good idea as it will limit to check just one value(date) and cannot use between and other operators.

According to your question you need to compare just the date part of the datetime value not the time part so the Query will be :

SQL
select CONVERT(VARCHAR(20),[date] , 120) from timesheetentry
where Convert(DateTime,Convert(Char(10),[date],120),120) = Convert(DateTime,Convert(Char(10),getdate(),120),120) 


this way the time part will be excluded from the Datetime value and Datetime operators(between) can also be used for comparisons.
 
Share this answer
 
Comments
kesav prakash 23-Jul-13 8:40am    
hello varshs i need to use like operator on that
Varsha Ramnani 24-Jul-13 1:13am    
like operator is used for string comparisons.
you are comparing date values thats why I suggested this solution.
If you want to use like operator then Solution 4 is correct and it is not a subquery.
select * from news_master where date like (select top(1) date from news_master)

i have tried this one and work for me may be you also
 
Share this answer
 
Comments
kesav prakash 19-Jul-13 1:52am    
its working but last 4 digits can be any so i need % there at last
Dholakiya Ankit 19-Jul-13 2:02am    
declare @dd nvarchar(50);
set @dd=(select top(1) date from news_master);
select * from news_master where date like '%'+@dd+'%'

try this one
subquery should return single Value and the like will return more than one records therefore directly you can not use like in subquery but you can use top(1)
 
Share this answer
 
Try this code.

SQL
declare @v varchar(1000)
set @v='select CONVERT(VARCHAR(20),[date] , 120) from timesheetentry
where [date] LIKE  '
Set @v = @v + ''''+ convert(varchar(10), getdate(), 126)+'%'''

exec(@v)
 
Share this answer
 
v2
Comments
kesav prakash 19-Jul-13 2:00am    
it say an err:
Msg 203, Level 16, State 2, Line 5
The name 'select CONVERT(VARCHAR(20),[date] , 120) from timesheetentry
where [date] LIKE '2013-07-19%'' is not a valid identifier.
ArunRajendra 19-Jul-13 2:28am    
Try the modified query.

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