Let's say I have 2 columns Start date and end date both are timestamp.
I want to subtract both (Start date - end date ) as date diff
But it's throwing me error. What should be the correct syntax.
Note:- I am doing this in teradata
select Current_date, (Current_date-1);
select cast(Current_Timestamp as date), (cast(Current_Timestamp as date)-1);
Related
Let's say I have a column with values in the format of a datetime '2021-07-01 00:00:00.000', how do I get the last date of the week (Sunday) for any datetime such as this in sqlite3? The day of the last date of the week is a Sunday.
So the answer for this date would want something like 2021-07-04 or 2021/07/04 (removing the time part) which is the date of the Sunday for the week the date 2021-07-01 is belonging to.
I know how this can be done in sqlserver but I do not know how to do it in sqlite.
Could someone please assist me on this? Let me know if you need more clarification.
Here is my attempt on sqlserver, but i need to get the same result in sqlite
SELECT
somedate,
CONVERT(VARCHAR(10),DATEADD(DAY, 7 - DATEPART(WEEKDAY, [somedate]), [somedate]),111) AS Last_Date_Of_Week
FROM table
apparently i can't do this in sqlite because sqlite does not have an official datetime type.
Sqlite date and time functions consider Sunday to be the first day of the week, and have a weekday modifier to advance a given timestamp to the given day of the week (Using 0-based indexing from Sunday). So:
sqlite> SELECT date('2021-07-01 00:00:00.000', 'weekday 0');
date('2021-07-01 00:00:00.000', 'weekday 0')
--------------------------------------------
2021-07-04
I am working locally with an sqllite DB. I have imported some records from teradata where there was a date field in the format of 'YYYY-MM-DD'. When i imported the records the date switched from a date to a number. I know this is a feature of sqllite and that one can access it via date(sqllite_date) when selecting it in a where clause.
My problem is that the dates now appear to be a bit odd. For example the year appears to be negative.
Is there anyway to recover this to the correct format?
Below is an example of converting a number in the database into a date
SELECT date(18386)
# -4662-03-28
SELECT datetime('now')
# 2021-02-11 10:41:52
SELECT date(sqllite_date) FROM mydb
# Returns -4662-03-28
# Should return 2020-05-04
I am very new to this area so apologies if this is a basic question. Thank you very much for your time
In SQLite you can store dates as TEXT, REAL or INTEGER.
It seems that you stored the dates in a column with INTEGER or REAL affinity.
In this case, if you use the function date(), it considers a value like 18386 as a Julian day, meaning the number of days since noon in Greenwich on November 24, 4714 B.C.
This is why date(18386) returns 4662-03-28B.C.
But I suspect that the date values that you have are the number of days since '1970-01-01'.
In this case, 18386 days after '1970-01-01' is '2020-05-04'.
So you can get the dates in the format YYYY-MM-DD if you add the value of your column as days to '1970-01-01':
SELECT date('1970-01-01', datecolumn || ' day') FROM tablename
Or by transforming your date values to seconds and treat them as UNIX time (the number of seconds since '1970-01-01 00:00:00 UTC'):
SELECT date(datecolumn * 24 * 3600, 'unixepoch') FROM tablename
Replace datecolumn with the name of your column.
I have current_date in Teradata which 18 DEC 2019
I have to calculate the previous quarter start date and end date from the above current_date.
Input = '2019-12-18'
Output Start Date = '2019-07-01'
Output End Date = '2019-09-30'
You should be able to do this using the TRUNC function, something like:
SELECT
TRUNC(ADD_MONTHS(CURRENT_DATE, -3), 'Q') AS Start_Quarter, -- Previous quarter start
TRUNC(CURRENT_DATE, 'Q') - 1 AS End_Quarter -- Current quarter start date - 1 day
Give it a try and let me know. This assumes the mistake in the manual is still considered a "mistake".
Also, depending on what TD version you're using, you may be able to use built-in functions:
SELECT
TD_QUARTER_BEGIN(CURRENT_DATE) AS Start_Quarter,
TD_QUARTER_END(CURRENT_DATE) AS End_Quarter
Reference
TD Manual
Built-in functions
I have a table consisting of a date field and a barcode field; I want the number of barcodes grouped by day for the previous month.
This looked like it would work:
SELECT
COUNT(*) AS count,
strftime('%d-%m-%Y',date) AS day
FROM barcodes
WHERE date >= datetime('now', '-1 month')
GROUP BY day
ORDER BY date ASC;
But that gives me incorrect counts. E.g.:
341|30-01-2017
274|31-01-2017
288|01-02-2017
332|02-02-2017
224|03-02-2017
35|04-02-2017
1009|06-02-2017
1481|07-02-2017
1626|08-02-2017
507|09-02-2017
428|10-02-2017
125|11-02-2017
1838|13-02-2017
2591|
Whereas:
SELECT COUNT(*) FROM barcodes WHERE date LIKE '2017-02-10%';
579
If I do this:
SELECT
COUNT(*) AS count,
strftime('%d-%m-%Y',date) AS day
FROM barcodes
WHERE date LIKE '2017-02-10%'
GROUP BY day
ORDER BY date ASC;
I get:
428|10-02-2017
151|
So my question is: why is SQLite providing the result as two lines when I use strftime()?
%d-%m-%Y is not one of the supported date formats, so comparisons do not work correctly, and any of the built-in date functions will return NULL.
I have been trying with no success to to count how many values were created in a specific week day:
SELECT count(*) as count FROM packets WHERE strftime("%w", timeIn) = '1';
I have this values in timeIn
1472434822.60033
1472434829.12632
1472434962.34593
I don't know what I am doing wrong here.
furthermore, if I use this:
SELECT count(*) as count FROM packets WHERE strftime("%w", timeIn) = '6';
I get
2
which makes no sense. Thank you in advance.
You appear to be storing the date as the number of seconds since 1970 (the Unix epoch) - a common representation. The time strings accepted by the SQLite date functions (see the Time Strings section) default to interpreting numeric time strings as a Julian day numbers:
Similarly, format 12 is shown with 10 significant digits, but the date/time functions will really accept as many or as few digits as are necessary to represent the Julian day number.
You can see this with the following SELECT:
SELECT strftime('%Y-%m-%d', 1472428800.6) AS t
the result of which is:
4026-48-26
For your date representation to be interpreted as a Unix epoch, you need to include 'unixepoch' in the strftime call:
SELECT strftime('%Y-%m-%d', 1472428800.6, 'unixepoch') AS t
which returns:
2016-08-29
If you modify your SELECT to be:
SELECT count(*) as count FROM packets WHERE strftime("%w", timeIn, 'unixepoch') = '6'
you should see results more inline with your expectations.