WPDB – Read and write value from / to database

This is still a valid question even if there is an easier way.

That easier way is get_user_meta($user_id,'vatnumber',true);

NOT $wpdb->get_user_meta($user_id, vatnumber,''); by the way.

Setting the $single flag to true is because although you can store multiple values in a single keyname but this is usually best avoided to avoid confusion.

Since you are getting a single value, the correct syntax for the database query would look something like this:

<?php 
global $wpdb; $metakey = "vatnumber";
$dbquery = $wpdb->prepare("SELECT meta_value FROM ".$wpdb->prefix."usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $metakey);
$vatnumber = $wpdb->get_var($dbquery);
?>

(You need to call global $wpdb; only once inside each function.)
Or, if you want to do it all at once:

<?php 
global $wpdb;
$vatnumber = $wpdb->get_var($wpdb->prepare("SELECT meta_value FROM ".$wpdb->prefix."usermeta WHERE user_id = %d AND meta_key = %s", $user_id, "vatnumber") );
?>

Note the use of $wpdb->prefix which generally will output wp_ since the standard table name is wp_usermeta but the prefix can be changed and it is good practice to support this in a plugin (but may not be needed for custom coding on your own site where you know it is wp_ anyway.)

Also note the action of $wpdb->prepare which tends to be needed these days to avoid MySQL injection attempts. The %d is replaced by the numeric value of $user_id. The %s is replaced by the string value of $meta_key.

If you do want to get ALL user_id’s and corresponding meta_value’s for those users with a VAT number stored, THEN you can use the get_results method to get them all, for example:

<?php 
global $wpdb; $metakey = "vatnumber";
$dbquery = $wpdb->prepare("SELECT user_id,meta_value FROM ".$wpdb->prefix."usermeta WHERE meta_key = %s", $metakey);
$vatdata = $wpdb->get_results($dbquery);
foreach ($vatdata as $data) {
    echo "VAT for User ".$data->user_id.": ".$data->meta_value."<br>";
}
?>

More examples: https://codex.wordpress.org/Class_Reference/wpdb