Air SQLite:how to use the date type? - air

var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = connection;
stat.text = "INSERT INTO names (id, name,myDate) VALUES (117, 'tarek', 3/10/2015)";
stat.execute();
i use air sqlite, why the date can't be inserted?

Assuming the myDate field was created as a DATE type, you have to cast the date you're inserting by using the SQLite date() function and you should use the format YYYY-MM-DD. This works:
INSERT INTO names (id, name, myDate) VALUES (117, 'tarek', date('2015-10-03'))

Related

Cast function SQL query

I have a column 'Created date' of string type (which has values like 19-01-2022, 05/02/1992 etc). I need to write a query to get the data from this table Orders where created date is greater than Jan 1st 2019. How can I write the query using cast function?
if you have just two string formats i.e. dd-mm-yyyy and dd/mm/yyyy then you can get through inline query only.
SELECT * FROM TABLENAME WHERE CONVERT(DATE,createdDate,103) >= '2019-01-01'
or if you passing search values in dd/mm/yyyy then
SELECT * FROM TABLENAME WHERE CONVERT(DATE,createdDate,103) >= CONVERT(DATE,'01/01/2019',103)
in case if you have more other types of string stored like mm/dd/yyyy then add one column to your table with datetime datatype. you only knowing the type of data in created date so you need to convert them into dates one by one. like for dd-mm-yyyy and dd/mm/yyyy you can convert to date.
Following is example of query for conversion
DECLARE #strdate VARCHAR(50)
SET #strdate = '19-01-2022'
SELECT CONVERT(DATE,#strdate,103)
SET #strdate = '05/02/2022'
SELECT CONVERT(DATE,#strdate,103)
SET #strdate = '12/31/2022'
SELECT CONVERT(DATE,#strdate,110)
you need to update newly added column from createddate column using update query and then apply your function on that
UPDATE TABLENAME SET newcreatedDate = CONVERT(DATE,createdDate,103) WHERE newcreatedDate IS NULL
and perform search query
SELECT * FROM TABLENAME WHERE newcreatedDate >= '2019-01-01'
or if you passing search values in dd/mm/yyyy then
SELECT * FROM TABLENAME WHERE CONVERT(DATE,newcreatedDate ,103) >= CONVERT(DATE,'01/01/2019',103)

Invalid datetime string when CAST As Date

I have Time column in BigQuery, the values of which look like this: 2020-09-01-07:53:19 it is a STRING format. I need to extract just the date. Desired output: 2020-09-01.
My query:
SELECT
CAST(a.Time AS date) as Date
from `table_a`
The error message is: Invalid datetime string "2020-09-02-02:17:49"
You could also use the parse_datetime(), then convert to a date.
with temp as (select '2020-09-02-02:17:49' as Time)
select
date(parse_datetime('%Y-%m-%d-%T',Time)) as new_date
from temp
How about just taking the left-most 10 characters?
select substr(a.time, 1, 10)
If you want this as a date, then:
select parse_date('%Y-%m-%d', substr(a.time, 1, 10))
select STR_TO_DATE('2020-09-08 00:58:09','%Y-%m-%d') from DUAL;
or to be more specific as your column do as:
select STR_TO_DATE(a.Time,'%Y-%m-%d') from `table_a`;
Note: this format is applicable where mysql is supported

How to insert string “dd/mm/yy hh:mm:ss” into datetime column in bigquery

I need to insert string “10/11/2019 12:34:45” into datetime column in bigquery , please advise any options ?
Below is for BigQuery Standard SQL
you can use
PARSE_DATETIME('%d/%m/%Y %T', datetime_col_as_string) datetime_col_as_datetime
for example, SELECT PARSE_DATETIME('%d/%m/%Y %T', '10/11/2019 12:34:45') produces datetime value as in below
Row datetime_col_as_datetime
1 2019-11-10T12:34:45

Extract date from string and insert into field Microsoft SQL Server 2012

Say I have a field UserAddedInfo with a string "User was added to the system and removed from list on 16/05/2016 by User Annon" and a field DateAdded in the same table.
Is there any way in SQL to extract that 16/05/2016 date from the string field and insert it into the DateAdded field as a datetime?
The date in the string is always going to be dd/MM/yyyy.
Thanks!
Use PATINDEX to get the start position of the date string in the column and extract 10 characters from that position. To convert the extracted string to date, use CONVERT with format 103.
103 = dd/mm/yyyy
select
convert(date,
substring(UserAddedInfo,patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]%',UserAddedInfo),10)
,103)
from table_name
where patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]%',UserAddedInfo) > 0
To update the dateadded field in the table, use
update table_name
set dateadded = convert(date,
substring(UserAddedInfo,patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]%',UserAddedInfo),10)
,103)
where patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]%',UserAddedInfo) > 0
Use try_cast or try_convert to return null when the substring returns invalid dates.
select
try_cast(
substring(UserAddedInfo,patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]%',UserAddedInfo),10)
as date)
--or
--try_convert(date,
--substring(UserAddedInfo,patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]%',UserAddedInfo),10)
--)
from table_name
where patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]%',UserAddedInfo) > 0
You can use patindex to find date string, and use cast to convert it to datetime
select
cast(substring('User was added to the system and removed from list on 27/08/2014 by User Annon',
patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]%',
'User was added to the system and removed from list on 27/08/2014 by User Annon'), 10) as datetime)

Accept UK date format in the database

I am sending the valid UK date 28/07/2014 10:53:46 to my database to get inserted. However the below statement says The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value
INSERT INTO user_Log (Id,IP,DateTime)
VALUES (6, '127.0.0.1', '28/07/2014 10:53:46');
But if I exchange the data&month part like below it works. What should I make the changes in the database to accept UK date format?
INSERT INTO user_Log (Id,IP,DateTime)
VALUES (6, '127.0.0.1', '07/28/2014 10:53:46');
You could try setting the language to british english:
SET LANGUAGE 'british english'
or set the date format to dd/mm/yyyy
SET DATEFORMAT dmy;
have a look at this too:
http://msdn.microsoft.com/en-us/library/ms189491.aspx
You can change your default format of date in sql server by using following query:
SET DATEFORMAT mdy;
You could use good ol' fashioned string manipulation to swap around the month and day:
declare #DATE_INPUT varchar(19); set #DATE_INPUT = '28/07/2014 10:53:46'
declare #US_DATE varchar(19)
set #US_DATE = SUBSTRING(#DATE_INPUT,4,2) + '/' + LEFT(#DATE_INPUT, 2) + RIGHT(#DATE_INPUT, 14)
INSERT INTO user_Log(Id,IP,DateTime)
VALUES(6, '127.0.0.1', #US_DATE);