How to handle date type in SQLite?How flexible/restricive are SQLite column types?How to list the tables in a SQLite database file that was opened with ATTACH?Sqlite: CURRENT_TIMESTAMP is in GMT, not the timezone of the machineHow do I check in SQLite whether a table exists?Improve INSERT-per-second performance of SQLite?What are the best practices for SQLite on Android?How to compare sqlite TIMESTAMP valuesIs there an SQLite equivalent to MySQL's DESCRIBE [table]?INSERT IF NOT EXISTS ELSE UPDATE?SQLite storing default timestamp as unixepochReformat dates in column

Can one block with a protection from color creature?

Sense of humor in your sci-fi stories

How can I use my cell phone's light as a reading light?

How do I explain that I don't want to maintain old projects?

Array or vector? Two dimensional array or matrix?

What is the meaning of "prairie-dog" in this sentence?

What was the nature of the known bugs in the Space Shuttle software?

How to say "is going" in Russian in "this game is going to perish"

Is it a good option to send images over LoRa network?

Why is there paternal, for fatherly, fraternal, for brotherly, but no similar word for sons?

Why do Martians have to wear space helmets?

Uniform initialization by tuple

How to evaluate the performance of open source solver?

Can we share mixing jug/beaker for developer, fixer and stop bath?

How do I talk to my wife about unrealistic expectations?

QR codes, do people use them?

The flying colours

What was the significance of Spider-Man: Far From Home being an MCU Phase 3 film instead of a Phase 4 film?

Difference between [[ expr1 || expr2 ]] and [[ expr1 ]] || [[ expr2 ]]

How was the website able to tell my credit card was wrong before it processed it?

How to understand flavors and when to use combination of them?

How many Jimmys can fit?

Execute code only once

KenKen solver - Python



How to handle date type in SQLite?


How flexible/restricive are SQLite column types?How to list the tables in a SQLite database file that was opened with ATTACH?Sqlite: CURRENT_TIMESTAMP is in GMT, not the timezone of the machineHow do I check in SQLite whether a table exists?Improve INSERT-per-second performance of SQLite?What are the best practices for SQLite on Android?How to compare sqlite TIMESTAMP valuesIs there an SQLite equivalent to MySQL's DESCRIBE [table]?INSERT IF NOT EXISTS ELSE UPDATE?SQLite storing default timestamp as unixepochReformat dates in column






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I've created a table, where I have "Date of birth" column of date type. The problem is that I can insert anything and it's successfully done. I want that field to restrict opportunities like inserting strings and not related staff.
insertions
wrongResults



I've searched for the solution, but I could only find codes for getting the current time in different formats. I also don't get how exactly modifiers work (https://www.sqlite.org/lang_datefunc.html).










share|improve this question




























    -1















    I've created a table, where I have "Date of birth" column of date type. The problem is that I can insert anything and it's successfully done. I want that field to restrict opportunities like inserting strings and not related staff.
    insertions
    wrongResults



    I've searched for the solution, but I could only find codes for getting the current time in different formats. I also don't get how exactly modifiers work (https://www.sqlite.org/lang_datefunc.html).










    share|improve this question
























      -1












      -1








      -1








      I've created a table, where I have "Date of birth" column of date type. The problem is that I can insert anything and it's successfully done. I want that field to restrict opportunities like inserting strings and not related staff.
      insertions
      wrongResults



      I've searched for the solution, but I could only find codes for getting the current time in different formats. I also don't get how exactly modifiers work (https://www.sqlite.org/lang_datefunc.html).










      share|improve this question














      I've created a table, where I have "Date of birth" column of date type. The problem is that I can insert anything and it's successfully done. I want that field to restrict opportunities like inserting strings and not related staff.
      insertions
      wrongResults



      I've searched for the solution, but I could only find codes for getting the current time in different formats. I also don't get how exactly modifiers work (https://www.sqlite.org/lang_datefunc.html).







      sqlite






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 20:05









      SedaSeda

      84 bronze badges




      84 bronze badges






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Bar the rowid column or an alias of the rowid column, any type of value can be stored in an type of column. That is the type of column does not restrict/constrain the data that can be stored.



          • p.s. there is no DATE type rather due to SQLite's flexibility DATE actually has a type (type affinity) of NUMERIC (not that that matters that much). You might find Datatypes In SQLite Version 3 an interesting read or perhaps this How flexible/restricive are SQLite column types?.


          • the rowid and, therefore an alias thereof, column MUST be an integer. Although typically you allow SQLite to assign the value.


          You should either check the data programatically or alternately use a CHECK constraint when defining the column in the CREATE TABLE SQL.




          A CHECK constraint may be attached to a column definition or specified
          as a table constraint. In practice it makes no difference. Each time a
          new row is inserted into the table or an existing row is updated, the
          expression associated with each CHECK constraint is evaluated and cast
          to a NUMERIC value in the same way as a CAST expression. If the result
          is zero (integer value 0 or real value 0.0), then a constraint
          violation has occurred. If the CHECK expression evaluates to NULL, or
          any other non-zero value, it is not a constraint violation. The
          expression of a CHECK constraint may not contain a subquery.




          SQL As Understood By SQLite - CREATE TABLE



          Example



          Consider the following code :-



          DROP TABLE IF EXISTS mychecktable ;
          CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'));
          INSERT INTO mychecktable VALUES('14-03-1900');
          INSERT INTO mychecktable VALUES('1900-03-14'); -- ouch 3rd char not -


          The is will result in :-




          DROP TABLE IF EXISTS mychecktable
          > OK
          > Time: 0.187s


          CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'))
          > OK
          > Time: 0.084s


          INSERT INTO mychecktable VALUES('14-03-1900')
          > Affected rows: 1
          > Time: 0.206s


          INSERT INTO mychecktable VALUES('1900-03-14')
          > CHECK constraint failed: mychecktable
          > Time: 0s



          • i.e. the first insert is successful, the second insert fails.





          share|improve this answer

























          • thank you! that was helpful

            – Seda
            Mar 25 at 21:35


















          0














          Usually you would enforce the correct format in your application, but you can also add constraints to your table definition to prevent this, e.g.,



          CREATE TABLE users(...,
          DoB TEXT CHECK(DATE(DoB) NOT NULL AND DATE(DoB)=DoB)
          )





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345616%2fhow-to-handle-date-type-in-sqlite%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Bar the rowid column or an alias of the rowid column, any type of value can be stored in an type of column. That is the type of column does not restrict/constrain the data that can be stored.



            • p.s. there is no DATE type rather due to SQLite's flexibility DATE actually has a type (type affinity) of NUMERIC (not that that matters that much). You might find Datatypes In SQLite Version 3 an interesting read or perhaps this How flexible/restricive are SQLite column types?.


            • the rowid and, therefore an alias thereof, column MUST be an integer. Although typically you allow SQLite to assign the value.


            You should either check the data programatically or alternately use a CHECK constraint when defining the column in the CREATE TABLE SQL.




            A CHECK constraint may be attached to a column definition or specified
            as a table constraint. In practice it makes no difference. Each time a
            new row is inserted into the table or an existing row is updated, the
            expression associated with each CHECK constraint is evaluated and cast
            to a NUMERIC value in the same way as a CAST expression. If the result
            is zero (integer value 0 or real value 0.0), then a constraint
            violation has occurred. If the CHECK expression evaluates to NULL, or
            any other non-zero value, it is not a constraint violation. The
            expression of a CHECK constraint may not contain a subquery.




            SQL As Understood By SQLite - CREATE TABLE



            Example



            Consider the following code :-



            DROP TABLE IF EXISTS mychecktable ;
            CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'));
            INSERT INTO mychecktable VALUES('14-03-1900');
            INSERT INTO mychecktable VALUES('1900-03-14'); -- ouch 3rd char not -


            The is will result in :-




            DROP TABLE IF EXISTS mychecktable
            > OK
            > Time: 0.187s


            CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'))
            > OK
            > Time: 0.084s


            INSERT INTO mychecktable VALUES('14-03-1900')
            > Affected rows: 1
            > Time: 0.206s


            INSERT INTO mychecktable VALUES('1900-03-14')
            > CHECK constraint failed: mychecktable
            > Time: 0s



            • i.e. the first insert is successful, the second insert fails.





            share|improve this answer

























            • thank you! that was helpful

              – Seda
              Mar 25 at 21:35















            1














            Bar the rowid column or an alias of the rowid column, any type of value can be stored in an type of column. That is the type of column does not restrict/constrain the data that can be stored.



            • p.s. there is no DATE type rather due to SQLite's flexibility DATE actually has a type (type affinity) of NUMERIC (not that that matters that much). You might find Datatypes In SQLite Version 3 an interesting read or perhaps this How flexible/restricive are SQLite column types?.


            • the rowid and, therefore an alias thereof, column MUST be an integer. Although typically you allow SQLite to assign the value.


            You should either check the data programatically or alternately use a CHECK constraint when defining the column in the CREATE TABLE SQL.




            A CHECK constraint may be attached to a column definition or specified
            as a table constraint. In practice it makes no difference. Each time a
            new row is inserted into the table or an existing row is updated, the
            expression associated with each CHECK constraint is evaluated and cast
            to a NUMERIC value in the same way as a CAST expression. If the result
            is zero (integer value 0 or real value 0.0), then a constraint
            violation has occurred. If the CHECK expression evaluates to NULL, or
            any other non-zero value, it is not a constraint violation. The
            expression of a CHECK constraint may not contain a subquery.




            SQL As Understood By SQLite - CREATE TABLE



            Example



            Consider the following code :-



            DROP TABLE IF EXISTS mychecktable ;
            CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'));
            INSERT INTO mychecktable VALUES('14-03-1900');
            INSERT INTO mychecktable VALUES('1900-03-14'); -- ouch 3rd char not -


            The is will result in :-




            DROP TABLE IF EXISTS mychecktable
            > OK
            > Time: 0.187s


            CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'))
            > OK
            > Time: 0.084s


            INSERT INTO mychecktable VALUES('14-03-1900')
            > Affected rows: 1
            > Time: 0.206s


            INSERT INTO mychecktable VALUES('1900-03-14')
            > CHECK constraint failed: mychecktable
            > Time: 0s



            • i.e. the first insert is successful, the second insert fails.





            share|improve this answer

























            • thank you! that was helpful

              – Seda
              Mar 25 at 21:35













            1












            1








            1







            Bar the rowid column or an alias of the rowid column, any type of value can be stored in an type of column. That is the type of column does not restrict/constrain the data that can be stored.



            • p.s. there is no DATE type rather due to SQLite's flexibility DATE actually has a type (type affinity) of NUMERIC (not that that matters that much). You might find Datatypes In SQLite Version 3 an interesting read or perhaps this How flexible/restricive are SQLite column types?.


            • the rowid and, therefore an alias thereof, column MUST be an integer. Although typically you allow SQLite to assign the value.


            You should either check the data programatically or alternately use a CHECK constraint when defining the column in the CREATE TABLE SQL.




            A CHECK constraint may be attached to a column definition or specified
            as a table constraint. In practice it makes no difference. Each time a
            new row is inserted into the table or an existing row is updated, the
            expression associated with each CHECK constraint is evaluated and cast
            to a NUMERIC value in the same way as a CAST expression. If the result
            is zero (integer value 0 or real value 0.0), then a constraint
            violation has occurred. If the CHECK expression evaluates to NULL, or
            any other non-zero value, it is not a constraint violation. The
            expression of a CHECK constraint may not contain a subquery.




            SQL As Understood By SQLite - CREATE TABLE



            Example



            Consider the following code :-



            DROP TABLE IF EXISTS mychecktable ;
            CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'));
            INSERT INTO mychecktable VALUES('14-03-1900');
            INSERT INTO mychecktable VALUES('1900-03-14'); -- ouch 3rd char not -


            The is will result in :-




            DROP TABLE IF EXISTS mychecktable
            > OK
            > Time: 0.187s


            CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'))
            > OK
            > Time: 0.084s


            INSERT INTO mychecktable VALUES('14-03-1900')
            > Affected rows: 1
            > Time: 0.206s


            INSERT INTO mychecktable VALUES('1900-03-14')
            > CHECK constraint failed: mychecktable
            > Time: 0s



            • i.e. the first insert is successful, the second insert fails.





            share|improve this answer















            Bar the rowid column or an alias of the rowid column, any type of value can be stored in an type of column. That is the type of column does not restrict/constrain the data that can be stored.



            • p.s. there is no DATE type rather due to SQLite's flexibility DATE actually has a type (type affinity) of NUMERIC (not that that matters that much). You might find Datatypes In SQLite Version 3 an interesting read or perhaps this How flexible/restricive are SQLite column types?.


            • the rowid and, therefore an alias thereof, column MUST be an integer. Although typically you allow SQLite to assign the value.


            You should either check the data programatically or alternately use a CHECK constraint when defining the column in the CREATE TABLE SQL.




            A CHECK constraint may be attached to a column definition or specified
            as a table constraint. In practice it makes no difference. Each time a
            new row is inserted into the table or an existing row is updated, the
            expression associated with each CHECK constraint is evaluated and cast
            to a NUMERIC value in the same way as a CAST expression. If the result
            is zero (integer value 0 or real value 0.0), then a constraint
            violation has occurred. If the CHECK expression evaluates to NULL, or
            any other non-zero value, it is not a constraint violation. The
            expression of a CHECK constraint may not contain a subquery.




            SQL As Understood By SQLite - CREATE TABLE



            Example



            Consider the following code :-



            DROP TABLE IF EXISTS mychecktable ;
            CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'));
            INSERT INTO mychecktable VALUES('14-03-1900');
            INSERT INTO mychecktable VALUES('1900-03-14'); -- ouch 3rd char not -


            The is will result in :-




            DROP TABLE IF EXISTS mychecktable
            > OK
            > Time: 0.187s


            CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'))
            > OK
            > Time: 0.084s


            INSERT INTO mychecktable VALUES('14-03-1900')
            > Affected rows: 1
            > Time: 0.206s


            INSERT INTO mychecktable VALUES('1900-03-14')
            > CHECK constraint failed: mychecktable
            > Time: 0s



            • i.e. the first insert is successful, the second insert fails.






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 25 at 23:24

























            answered Mar 25 at 21:15









            MikeTMikeT

            22.3k11 gold badges28 silver badges44 bronze badges




            22.3k11 gold badges28 silver badges44 bronze badges












            • thank you! that was helpful

              – Seda
              Mar 25 at 21:35

















            • thank you! that was helpful

              – Seda
              Mar 25 at 21:35
















            thank you! that was helpful

            – Seda
            Mar 25 at 21:35





            thank you! that was helpful

            – Seda
            Mar 25 at 21:35













            0














            Usually you would enforce the correct format in your application, but you can also add constraints to your table definition to prevent this, e.g.,



            CREATE TABLE users(...,
            DoB TEXT CHECK(DATE(DoB) NOT NULL AND DATE(DoB)=DoB)
            )





            share|improve this answer



























              0














              Usually you would enforce the correct format in your application, but you can also add constraints to your table definition to prevent this, e.g.,



              CREATE TABLE users(...,
              DoB TEXT CHECK(DATE(DoB) NOT NULL AND DATE(DoB)=DoB)
              )





              share|improve this answer

























                0












                0








                0







                Usually you would enforce the correct format in your application, but you can also add constraints to your table definition to prevent this, e.g.,



                CREATE TABLE users(...,
                DoB TEXT CHECK(DATE(DoB) NOT NULL AND DATE(DoB)=DoB)
                )





                share|improve this answer













                Usually you would enforce the correct format in your application, but you can also add constraints to your table definition to prevent this, e.g.,



                CREATE TABLE users(...,
                DoB TEXT CHECK(DATE(DoB) NOT NULL AND DATE(DoB)=DoB)
                )






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 25 at 21:17









                varrovarro

                1,6861 gold badge10 silver badges19 bronze badges




                1,6861 gold badge10 silver badges19 bronze badges



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345616%2fhow-to-handle-date-type-in-sqlite%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해