D 2013-03-05T20:12:59.281 L 4.1.0\sChanges U sandro W 8212 back to XmlBlob main page

New SQL functions introduced in 4.1.0

Generic SQL functions - type casting

SQLite offers just a very basic type checking; this is sometimes useful, but could be really ennoying very often.
SELECT '1', TypeOf( '1' ), CAST ( '1' AS INTEGER ), TypeOf( CAST ( '1' AS INTEGER ) );
'1'TypeOf( '1' )CAST ( '1' AS INTEGER )TypeOf( CAST ( '1' AS INTEGER ) )
1text1integer

The SQL CAST operator already supports the possibility to explicitly set a data-type; now starting since 4.1.0 SpatiaLite supports few SQL functions performing the same task.
Please note: some subtle differences exists here and there; please carefully read this documentation page. SELECT CastToInteger( 1 ), CastToInteger( 1.4 ), CastToInteger( 1.6 ), CastToInteger( '123' ), CastToInteger( 'alpha' ), CastToInteger ( zeroblob(4) );
CastToInteger( 1 )CastToInteger( 1.4 )CastToInteger( 1.6 )CastToInteger( '123' )CastToInteger( 'alpha' )CastToInteger ( zeroblob(4) )
112123NULLNULL

The CastToInteger() SQL function will attempt to return a value of the Integer data-type, or NULL if no conversion is possible.
SELECT CastToDouble( 1 ), CastToDouble( 1.23 ), CastToDouble( '123.45' ), CastToDouble( 'alpha' ), CastToDouble ( zeroblob(4) );
CastToDouble( 1 )CastToDouble( 1.23 )CastToDouble( '123.45' )CastToDouble( 'alpha' )CastToDouble ( zeroblob(4) )
1.0000001.230000123.45000NULLNULL

The CastToDouble() SQL function will attempt to return a value of the Double data-type, or NULL if no conversion is possible.
SELECT CastToText( 1 ), CastToText( 1.5 ), CastToText( 'alpha' ), CastToText ( zeroblob(4) );
CastToText( 1 )CastToText( 1.5 )CastToText( 'alpha' )CastToText ( zeroblob(4) )
11.5alphaNULL

The CastToText() SQL function will attempt to return a value of the Text data-type, or NULL if no conversion is possible. SELECT CastToText( 1, 4 ), CastToText( 1234, 2 ), CastToText( 1, 8 ), CastToText( 1.5, 4 );
CastToText( 1, 4 )CastToText( 1234, 2 )CastToText( 1, 8 )CastToText( 1.5, 4 )
00011234000000010001.5

An overloaded version of CastToText() is supported as well, but only when the input argument is of the Integer or Double data-type: in this case a second Integer argument could be optionally specified, and will be interpretered ad zero-padding-length.
i.e. the returned Text string in this case will have an integer part at least of the specified length, and eventually padded by inserting not significative leading ZEROes.

SELECT CastToBlob( 1 ), CastToBlob( 1.5 ), Hex( CastToBlob( 'alpha' ) ), Hex ( CastToBlob ( zeroblob(4) ) );
CastToBlob( 1 )CastToBlob( 1.5 )Hex( CastToBlob( 'alpha' ) )Hex( CastToText ( zeroblob(4) ) )
NULLNULL616C70686100000000

The CastToBlob() SQL function will attempt to return a value of the BLOB data-type, or NULL if no conversion is possible.
SELECT ForceAsNull( 'a', 'b' ), ForceAsNull( 'abcd', 'abcd' ), ForceAsNull( 1, 1), ForceAsNull( 1.1, 1 );
ForceAsNull( 'a', 'b' )ForceAsNull( 'abcd', 'abcd' )ForceAsNull( 1, 1)ForceAsNull( 1.1, -9999 )
aNULLNULL1.1

The ForceAsNull() SQL function will evaluate two arbitrary arguments.
If they are equal and exactely of the same data-type NULL will be returned; otherwise the first argument value will be returned.
Useful e.g. while processing data imported from any Shapefile or DBF file (such formats doesn't support any real NULL value, often being replaced by some conventional value e.g. 0 or -9999 and alike).

Generic SQL function - UUID generator

An Universally unique identifier (aka UUID) simply is a 128 bit Integer; accordingly to theory, the probability that two randomly generated UUIDs could assume the same value is practically near zero. And consequently UUIDs are widely used as universally unique IDs.
Usually UUIDs are represented in their canonical form, i.e. as a sequence of 32 hexadecimal digits separeted into five distinct block, accordingly to a 8-4-4-4-12 schema. SELECT CreateUUID(); -------- fff5faf1-dcc0-4391-8054-6e7de75d85b1 The CreateUUID() SQL function is built on the top the High Quality Pseudo-Random Number Generator internally implemented by SQLite.
The returned UUID is conformant to Version 4 (random) (i.e. the first digit into the third block will always be 4 and the first digit into the fourth block will always be 8).
back to XmlBlob main page Z ea36518d8229d128faf6cf80b7a94a67