Cast text to numeric in sqlite table definition - sqlite

is there a cast in sqlite, I need this to work in a create table definition ...
....
mydatetime numeric not null DEFAULT (strftime('%Y%m%d%H%M','now', 'localtime')) );
thanks

sqlite> .headers on
sqlite> CREATE TABLE t1 (mydatetime int not null DEFAULT (strftime('%Y%m%d%H%M',
'now', 'localtime')), otherval TEXT);
sqlite>
sqlite> INSERT INTO t1 (otherval) VALUES ('qwe');
sqlite> SELECT * FROM t1;
mydatetime|otherval
201201231947|qwe

Related

Is there an SQLite function to return only a substring/partial value

For the following query
namelist
name
Thisismy/$name
Thisisalsomy/$name
Thisisnotmy/$name
SELECT name from namelist;
returns
Thisismy/$name
Thisisalsomy/$name
Thisisnotmy/$name
How can I limit the return to everything up to the /
ie:
Thisismy
Thisisalsomy
Thisisnotmy
You can do it with some sqlite core functions:
sqlite> select substr('Thisisalsomy/$name',0,instr('Thisisalsomy/$name','/'));
Thisisalsomy
So
SELECT substr(name,0,instr(name,'/')) from namelist;
Tested as follows:
sqlite> create table namelist (name);
sqlite> insert into namelist values ('Thisismy/$name'),('Thisisalsomy/$name'),('Thisisnotmy/$name');
sqlite> SELECT name from namelist;
Thisismy/$name
Thisisalsomy/$name
Thisisnotmy/$name
sqlite> SELECT substr(name,0,instr(name,'/')) from namelist;
Thisismy
Thisisalsomy
Thisisnotmy
sqlite>

how to get the ordered query in sqlite?

I insert some data into test table.
C:\> sqlite3 e:\\test.db
sqlite> create table test(code TEXT,content numeric);
sqlite> insert into test(code,content)values('x',3);
sqlite> insert into test(code,content)values('x',1.5);
sqlite> insert into test(code,content)values('x',1);
sqlite> insert into test(code,content)values('y',9);
sqlite> insert into test(code,content)values('y',3);
sqlite> insert into test(code,content)values('y',2);
sqlite> insert into test(code,content)values('y',12.3);
sqlite> insert into test(code,content)values('y',11.5);
how can i get the ordered output as the following ?
select * from test group by code order by content;can not get it.
select * from test order by content;neither.
x|1
x|1.5
x|3
y|2
y|3
y|9
y|11.5
y|12.3
You can sort by multiple criteria:
SELECT * FROM test ORDER BY code, content;

Is it allowed to have null foreign key in sqlite when PRAGMA foreign_keys=ON?

according to this null foreign keys are allowed unless and until we are adding the appropriate "NOT NULL" constraint to the schema.
but I am seeing the some different behavior,
sqlite> PRAGMA Foreign_keys;
1
sqlite> create table proc (
sqlite> pid integer,
sqlite> name text,
sqlite> ppid integer,
sqlite> foreign key (ppid) references proc (id)
sqlite> );
sqlite> .schema proc
CREATE TABLE proc (
pid integer,
name text,
ppid integer,
foreign key (ppid) references proc (id)
);
sqlite> insert into proc (pid, name, ppid)
sqlite> values (0, "init", null);
Error: foreign key mismatch
sqlite> PRAGMA Foreign_keys=OFF;
sqlite> PRAGMA Foreign_keys;
0
sqlite> insert into proc (pid, name, ppid)
sqlite> values (0, "init", null);
sqlite> select * from proc;
0|init|
how can I allow null foreign key in sqlite when PRAGMA foreign_keys=ON? or it is not possible at all?
The ID column is named pid, not id.
The parent key column must have a UNIQUE or PRIMARY KEY constraint.
Try adding a foreign key clause by changing your table create statement to:
CREATE TABLE proc (pid integer, name text, ppid integer, foreign key (ppid) references
proc (id) ON UPDATE CASCADE ON DELETE SET NULL);

SQLite Store strings that start with zero e.g. "001"

Which data type do I choose in SQLite to store a string such as "001" in the database.
I've tried using Text but when I go to type in the string 001, it drops the 00 and only puts a 1.
I've even tried using "blob" data type but still no luck. They all drop the zeros before the digit.
Works ok for me:
sqlite> create table sotest
...> (
...> col1 varchar(20)
...> );
sqlite>
sqlite> select * from sotest;
sqlite>
sqlite> insert into sotest values ('001');
sqlite> select * from sotest;
001
sqlite>
Maybe you weren't quoting your strings? For example:
sqlite> insert into sotest values ('001');
sqlite> select * from sotest;
001
sqlite> insert into sotest values (001);
sqlite> select * from sotest;
001
1
sqlite>
Column type should set to varchar, not string, if type is string zero will be dropped automatically.

Not empty string constraint in SQLite

Can I create a database constraint on a TEXT column in SQLite disallowing the value of the column to be empty string ""?
I want to allow the column to be null, but disallow empty string.
Yes you can:
sqlite> create table foo (bar TEXT, CHECK(bar <> ''));
sqlite> insert into foo values (NULL);
sqlite> insert into foo values ('bla');
sqlite> insert into foo values ('');
Error: constraint failed
You can use a CHECK constraint (http://www.sqlite.org/lang_createtable.html):
SQLite version 3.5.9
Enter ".help" for instructions
sqlite> create table example(col, CHECK (col is null or length(col) > 0));
sqlite> insert into example values ('');
SQL error: constraint failed
sqlite> insert into example values (null);
sqlite> insert into example values ('sample');
sqlite> .nullvalue NULL
sqlite> select col from example;
NULL
sample
As far as i know doesn't exist a similar constraint in SQLite, but maybe you can workaround with a Trigger that on INSERT and/or UPDATE automatically change the string empty in NULL.

Resources