QGIS Virtual Layer – Create Virtual Layer That Retains Source Field Types

qgisvirtual-layer

In a previous question that I posted, the answer that I accepted involved creating a Virtual Layer from a source layer. The query used to create the Virtual Layer was:

select *  
from my_layer  
order by field_a 

Although the query works as intended, some of the source field types are converted to different types in the Virtual Layer. Specifically:

  • Integer (32 bit) are converted to Integer (64 bit)
  • Date and Date&Time are converted to Text (string)
  • Boolean are converted to Integer (64 bit)

I use the Refactor Fields to get the field types back to their original, but this involves another step that I would prefer to eliminate.

Question: How to create a Virtual Layer that retains the source field types?

Best Answer

Virtual layers lack field type support for Date, Time, and Boolean. See what Virtual layer context help says:

where type can be any of int, real or text

enter image description here

See also here: https://docs.qgis.org/latest/en/docs/user_manual/managing_data_source/create_layers.html#special-comments

Related Question