Jump to content
Ian Branch

Check if database table string field is empty or NULL??

Recommended Posts

Hi team,

Given a database table field   statement..

MyTable.Fieldbyname('MyField').AsString

What is the best/most efficient way to test if it is empty or NULL??

IsNullOrEmpty(MyTable.Fieldbyname('MyField').AsString)

But Delphi didn't like it.

 

Regards & TIA,

Ian

Share this post


Link to post
MyTable.Fieldbyname('MyField').IsNull
Edited by Stano

Share this post


Link to post
3 hours ago, Ian Branch said:

What is the best/most efficient way to test if it is empty or NULL??



 

if it's a string field then you can check against empty string => .AsString = ''   , both, empty string and NULL will be true

(side note: you can set a field to NULL with ".Clear", if it's a nullable field)

 

Share this post


Link to post

note in SQL databases:

  • CHAR( n ) = space is add on field to fill it  <-- FIXED TYPO! not "VAR(n)"
  • VARCHAR( n ) = no-space is add on field to fill it

sometimes needs a "TRIM( ... )" to remove all spaces if none-char exists (if not automatic done by Delphi)

Edited by programmerdelphi2k

Share this post


Link to post
var
  MyVar: string;
begin
  if ''.IsNullOrEmpty(MyVar) then //   Result := Value = Empty;
  or
  if ''.IsNullOrWhiteSpace(MyVar) then //   Result := Value.Trim.Length = 0;
    caption := 'null or empty'
  else
    caption := 'no-no-no';

 

Edited by programmerdelphi2k

Share this post


Link to post
2 hours ago, programmerdelphi2k said:

note in SQL databases:

  • VAR( n ) = space is add on field to fill it 

what the actual f VAR(n) is?

Share this post


Link to post

maybe.. you can read Firebird documentation if dont!

 

https://www.ibexpert.net/ibe/pmwiki.php?n=Doc.CHAR  a long text to reflect

https://www.firebirdfaq.org/faq237/

Quote

What's the difference between CHAR and VARCHAR?

 

The most important difference is that CHAR is padded with spaces and VARCHAR is not. For example, if you have:

  • CREATE TABLE t1 ( c1 VARCHAR(2)c2 CHAR(2)  );
  • INSERT INTO t1 (c1,c2) VALUES ('a', 'a');

 

The column c1 will contain value 'a', while column c2 will contain value 'a ' with additional space.

Trailing spaces are ignored when doing comparisons, so both columns would match the WHERE c = 'a' clause of some query. Trailing spaces are respected by LIKE operator, which is a source of confusion for beginners (example in FAQ #309).

 

Edited by programmerdelphi2k

Share this post


Link to post
13 hours ago, Ian Branch said:

What is the best/most efficient way to test if it is empty or NULL??


IsNullOrEmpty(MyTable.Fieldbyname('MyField').AsString)

But Delphi didn't like it.

IsNullOrEmpty() is a method of TStringHelper, so you have to call it on a string instance, eg:

if MyTable.FieldByName('MyField').AsString.IsNullOrEmpty then

Which is really no better than simply testing for an empty string (since AsString can't return a null string):

if MyTable.FieldByName('MyField').AsString = '' then

However, since Delphi strings don't differentiate between null and empty (they are both a nil pointer), if you really need to differentiate then you will have to use TField.IsNull instead, eg:

if MyTable.FieldByName('MyField').IsNull then
  // is null ...
else if MyTable.FieldByName('MyField').AsString = '' then
  // is empty ...
else
  // is not null or empty ...

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×