Select that does not return SQL blank

Asked

Viewed 619 times

1

select 'TEM' AS InSituacao
    from GTCLogist
where NrPlacaCarreta = ''
and ID <> '35514'
and year(DtBase)=year(GETDATE())
and MONTH(DtBase)=MONTH(GETDATE())
and DAY(DtBase)=DAY(GETDATE())

inserir a descrição da imagem aqui

I have the SQL above, I need that when the field is empty do not return me anything. I have tried the isnull but how the field is blank and not null I don’t know if I made the right use.

  • What field when it’s blank should not return anything to you?

  • NrPlacaCarreta

  • Tried to use coalesce?

  • 1

    What do you mean, "stay blank"?

  • What is the structure of the table?

  • @Kevin. F: There is difference between non-valued column (NULL), empty string column ('), or empty column (' '). Which means "blank", in your case?

  • @Kevin. F: How is the Dtbase column declared? If it is like datetime, the column contains date only (e.g., "2015-02-22") or contains date and time (e.g., "2015-02-22 02:35:12") ?

  • Try: nomeColuna IS NOT NULL AND nomeColuna != '', so if the column is valued null or if you have the white value (as I think it is your case) it will not bring.

Show 3 more comments

1 answer

2


"(...) I need that when the field is blank do not return me anything"

Kevin, evaluate the following suggestion:

-- código #1 v2
declare @Hoje date;

set @Hoje= cast(current_timestamp as date);

SELECT ID, NrPlacaCarreta, 'TEM' as InSituacao
  from GTCLogist
  where coalesce(ltrim(NrPlacaCarreta), '') <> ''
        and ID <> '35514'
        and cast(DtBase as date) = @Hoje;

Code #1 does not return lines where the column Nrplacacarreta is without information (NULL) or empty or with blank space(s).

  • Sorry it took so long to test, I’m back now. Perfect answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.