Avatar of cbueno
cbueno asked on

SQL Select where date like in c#

Hi All
I was wondering how to do a select statement where date like something.
mystr="2008"
string myst2 ="Select * from myTable where myDate like '" + mystr + "'"

I am gettting nothing
Any idaeas?
Regards
CB
.NET ProgrammingMicrosoft SQL Server 2005Microsoft SQL Server

Avatar of undefined
Last Comment
cbueno

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

if you want all the dates from 2008, assuming that the field "myDate" is datetime, the best would be like below.

hence:
* is the field datetime, or is it varchar
  -> if varchar: why?  what format?

* will you only search by "years", or what would be other criteria types?
string myst2 ="Select * from myTable 
where myDate >= convert(datetime, '2008-01-01', 120)
and myDate < convert(datetime, '2009-01-01', 120)

Open in new window

ASKER
cbueno

Hi angellll

You see the field is a stored as 07/05/2008 00:00:00 (smalldatetime)
When i query for the date only i get it back in the format 2008-05-07 00:00:00

Now my string in c# comes like 07/05/2008 without the 00:00:00. What I want to do is select the date where is like 07/05/2008

CB
ASKER CERTIFIED SOLUTION
Guy Hengel [angelIII / a3]

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
cbueno

Its the 7th of May. Thank you very much its the first one.
Regards
CB
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck