Friday, March 23, 2012

How can i only get people which is brithday is today?

Why i got "SqlException was unhandled by user code" "{"Incorrect syntax near '12'."}" ?

How can i only get people which is brithday is today?

The "rc_brithday" datatype is datetime

Thanks you

Code is below:

cmdSelect = New SqlCommand("Select rc_email From recruiters WHERE rc_brithday = " & DateTime.Now(), conPubs)
conPubs.Open()
dtrTitles = cmdSelect.ExecuteReader()

himap:

Code is below:

cmdSelect = New SqlCommand("Select rc_email From recruiters WHERE rc_brithday = " & DateTime.Now(), conPubs)

Change the above line to - (I highlighted the single quote in red to make the line easier to read)

cmdSelect = New SqlCommand("Select rc_email From recruiters WHERE rc_brithday ='" & DateTime.Now() & "'", conPubs)

|||

hoopslife:

himap:

Code is below:

cmdSelect = New SqlCommand("Select rc_email From recruiters WHERE rc_brithday = " & DateTime.Now(), conPubs)

Change the above line to - (I highlighted the single quote in red to make the line easier to read)

cmdSelect = New SqlCommand("Select rc_email From recruiters WHERE rc_brithday ='" & DateTime.Now() & "'", conPubs)

I have no error now, but therc_brithdayinclude the time, so no any data is get from sql server.

How can i except the time that only focue the date find people birthday is today? THANKS YOU

i use follow statement is no error butno any data is get from sql server, why?:

cmdSelect = New SqlCommand("Select rc_email,rc_brithday From recruiters WHERE rc_brithday = '" & DateTime.Now().ToString("d") & "'", conPubs)

|||Use

DateTime.Now.ToShortDateString()

|||cmdSelect = New SqlCommand("Select rc_email,rc_birthday From recruiters WHERE rc_birthday>=dateadd(d,0,datediff(d,0,getdate())) AND rc_birthday<dateadd(d,1,datediff(d,0,getdate()))", conPubs) |||

Motley:

cmdSelect = New SqlCommand("Select rc_email,rc_birthday From recruiters WHERE rc_birthday>=dateadd(d,0,datediff(d,0,getdate())) AND rc_birthday<dateadd(d,1,datediff(d,0,getdate()))", conPubs)

this solution is work!! thanks you very much!!!

I also change to more simple solution, have same work result,

cmdSelect = New SqlCommand("Select rc_email,rc_birthday From recruiters WHERE rc_birthday>=dateadd(d,0,datediff(d,0,getdate())) AND rc_birthday<dateadd(d,1,datediff(d,0,getdate()))", conPubs)

or

cmdSelect = New SqlCommand("Select rc_email,rc_birthday From recruiters WHERE rc_birthday=dateadd(d,0,datediff(d,0,getdate()))", conPubs)

or

cmdSelect = New SqlCommand("Select rc_email,rc_birthday From recruiters WHERE rc_birthday=datediff(d,0,getdate())", conPubs)

No comments:

Post a Comment