Marek
Marek

Reputation: 3575

SqlCommand - Select ISNULL

Hello I'm trying to select * data but for column named payments if is NULL I need to insert set value 0.

I tried to do it like this:

string sqlcom = "SELECT (ISNULL(payments,0)) * FROM zajezd WHERE id >= '" + txt_od.Text + "'AND id <='" + txt_do.Text + "' AND year='" + klientClass.Rocnik() + "'";

But I'm still receiving exception bad syntax near FROM

Would somebody help me solve this out please?

Thanks so much in advance

Upvotes: 2

Views: 423

Answers (5)

Giannis Paraskevopoulos
Giannis Paraskevopoulos

Reputation: 18411

You need a comma or clear the *.

SELECT (ISNULL(payments,0)), * FROM

or

SELECT (ISNULL(payments,0)) FROM

Just to add the sum of the commends (Thanks to Liath) you should not use *. Instead specify which columns you really need.

Also remove the external parenthesis as they are not really needed.

I would also add that you should use an allias for that column.

So it would be:

SELECT ISNULL(payments,0) AS payments, Col1, Col2,..., Coln FROM...

or just plain:

SELECT ISNULL(payments,0) AS payments FROM...

Upvotes: 3

Moslem Ben Dhaou
Moslem Ben Dhaou

Reputation: 7005

Try this

string sqlcom = "SELECT ISNULL(payments,0), * FROM zajezd WHERE id >= '" + txt_od.Text + "'AND id <='" + txt_do.Text + "' AND year='" + klientClass.Rocnik() + "'";

Your WHERE clause is a bit ambigious. <= txt_do.Text AND >= txt_od.Text

That will not filter anything out

Upvotes: 1

Nadeem_MK
Nadeem_MK

Reputation: 7689

Either you remove the * or you put a comma in between (ISNULL(payments,0)) and *

Upvotes: 1

Sachin
Sachin

Reputation: 40970

Put , before *. Try this

string sqlcom = "SELECT (ISNULL(payments,0)), * FROM zajezd WHERE id >= '" + txt_od.Text + "'AND id <='" + txt_do.Text + "' AND year='" + klientClass.Rocnik() + "'";

But I would also suggest you to not to pass the parameter like this. Use Parameterized query to avoid sql injection.

Upvotes: 4

Joe Taras
Joe Taras

Reputation: 15379

Add comma between

(ISNULL(payments,0))
and
*

Upvotes: 2

Related Questions