Get Records from Current Date in SQLite - sql

In MSSQL using
where DateDiff(Day,CurrentDate,getdate())=0
shows all Records Created from CurrentDate
How to do it in Sqlite ?

WHERE CurrentDate BETWEEN DATE('now') AND DATE('now', '+1 day')
Source:
SQLite Query Language: Date And Time Functions

I'd suggest that using DATEDIFF or any other functions that involve your CurrentDate field will result in a query that cannot take advantage of any indexes you may have on that field.
Much better is to write the WHERE clause as WHERE CurrentDate >= DATEADD(DAY, DATEDIFF(DAY, '2000-01-01', GETDATE(), '2000-01-01')) - or any other rewrite that keeps CurrentDate unmodified in the comparison.
N.B. There may be a much better way of converting a date & time to a date - my approach is based on pre- MSSQL 2008 T-SQL.

Related

Presto - how is there an alternative to to_char like postgresql?

I need to build a query in presto that could look back the trailing 70 days, the table I am working with is storing the dates in the format of 'YYYYMMDD'.
in postgresql, I can just simply write the where clause as
where date >= to_char(current_date - 70, 'YYYYMMDD')
and it'll pull in the date 70 days ago in a YYYYMMDD format.
However, in PrestoSQL, that function doesn't seem to exist, is there an alternative to this?
You can do this with date_format():
where date >= date_format(current_date - interval '70' day, '%Y%m%d')
Note that storing dates as strings is not a good practice at all - you should be using the proper date-like datatype - and then you won't need to do conversions at all.
You would just use date arithmetic:
where date >= current_date - interval '70' day
I'm not sure why you would want to involve strings in comparisons that are strictly date related.

SQL date comparision in yyyy/mm/dd

Not being that great at sql, i've reached my limit.
I have a date in the yyyy/mm/dd format and i need to get all records "from a week ago"
I think i need some conversion stuff to be done cause this
d.date_begin >= DATEADD(day,-7, GETDATE())
is not working :), i'm TERRIBLE at convert and data type..
This will work if you want records from 7 days ago up to and including today's records
CAST(d.date_begin AS DATE) >= CAST(DATEADD(day,-7, GETDATE()) AS DATE)
where DATEDIFF(month,Your_date_column,getdate()) < 3
SQL server 2012 onwards, if date_begin is of datatype date
where d.date_begin >= cast(DATEADD(day,-7, GETDATE()) as date)
This will get anything in the last 7 days, regardless of time
You should store date/time values using native formats. Ok, sometimes we cannot. But you can easily convert your values to the correct format:
where cast(replace(d.date_begin, '/', '') as date) >= DATEADD(day, -7, GETDATE())
I should note that SQL Server is pretty good about conversions, so your initial code should not generate any errors -- unless you have unusual internationalization settings.
Or, actually, a better way to do this is to convert the current value to a string:
where d.date_begin >= format(dateadd(day, -7, getdate()), 'yyyy/MM/dd')
This is better because it is "sargable", meaning that SQL Server can use an index on the column if available.
SELECT * FROM tbl_name
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Don't manipulate d.date_begin. Making calculation on a column while comparing can give bad performance. You should manipulate getdate() to get the same format as d.date_begin. In this case it works because the format is yyyy/MM/dd - the comparison will give the same result as if both columns were date columns.
WHERE
d.date_begin >= convert(char(10),DATEADD(day,-7, GETDATE()), 111)

SQL Epoch Time "Where" Statement

I have two questions regarding querying data in SQL that uses Epoch time stamps. I'm able to convert the date in my SELECT statement using this text:
DATEADD(HOUR, -4, DATEADD(SECOND, aa.fldTimeOfEvent, '1970-01-01 00:00:00'))
I had to use the "hour,-4" to convert it to Eastern daylight savings time. I'm curious if I will need to adjust this to -5 in November when daylight savings time ends? Is there a way to formulate the SELECT statement so it automatically adjusts for DST? This will be part of an automated report and I'm afraid we may miss changing this value.
Another question is how to use the WHERE statement to get data for a certain day. For instance, if I wanted the WHERE statement to grab all entries that occurred any time today (7/14/16), how would I do that? I've tried this statement:
WHERE DATEADD(HOUR, -4, DATEADD(SECOND, aa.fldTimeOfEvent, '1970-01-01 00:00:00')) = '2016-07-14'
It appears to only grab the entries that are exactly equal to midnight of that day (I think). I want all entries that occurred for any time the day.
Thanks in advance for your help.
The easy part first:
If you need to compare the date only, convert your datetimes to dates before comparing with: CONVERT(date, yourdatefield)
The second part has been discussed and solved here for UTC time: Convert Datetime column from UTC to local time in select statement. You would still need to add in the offset for Epochtime.
I'm assuming you're using SQL Server. If you only ever need the current offset, you can use GETUTCDATE() to calculate it:
SELECT DATEDIFF(hour,GetUTCDate(),GETDATE())
If you need to calculate the offset for other dates you'll likely want to use a CLR function: https://dba.stackexchange.com/questions/28187/how-can-i-get-the-correct-offset-between-utc-and-local-times-for-a-date-that-is
Which links to: https://blogs.msdn.microsoft.com/sqlserverfaq/2011/07/29/how-to-convert-utc-time-to-local-time-in-sql/

How to transform Oracle DateTime to Date

For instance, I have a datetime like this '2016-04-02 00:00:00' and another like this '2016-04-02 15:10:00'. I don't care about the time-part, I want them to match just by the date-part.
I have tried with date(), to_date, datepart, nothing works.
Do it like this:
where yourField >= the start of your date range
and yourField < the day after the end of your date range
Edit starts here:
While you could use trunc, as suggested by others, bear in mind that filtering on function results tends to be slow.
Truncating the date to day should do the trick. Documentation here:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions201.htm
For example
SELECT TRUNC(SYSDATE, 'DAY') FROM DUAL;
As others have said - there is no separate "date" data type in Oracle. A pure "date" is stored as a date with the time portion set to 00:00:00 (midnight at the beginning of the day), and TRUNC(date) will take any date and truncate the time to 00:00:00, so if you need to compare two dates you can write
where trunc(date_1) = trunc(date_2)
If your tables are very large and you need to do these comparisons often, this is not ideal, because wrapping column values within function calls (like date_1 within a TRUNC) prevents the use of an index you may have on the date_1 column. If you need to compare dates in two columns you may not have much of a choice, but if you compare to a fixed date (or something like SYSDATE) you may be better off with something like
where date_1 >= trunc(sysdate) and date_1 < trunc(sysdate) + 1
Here you are not using trunc on the column value, so if there's an index on the column, Oracle is free to use it - and trunc(sysdate) is computed only once, not for every single row. "+1" by the way means "add one day".
TO_DATE converts a string to a date; if you apply TO_DATE to a value that is already a legitimate date, you will get unexpected results because Oracle will first convert your true date to a string and then back to date again, and since these conversions require a date FORMAT for strings, and the formats Oracle assumes for conversion from date to string and from string to date may not match, .... you get the idea. As far as I know, DATE() (a FUNCTION) and DATEPART do not exist in Oracle; when you use a new language, keep Google close by and use it often.
If you input a date with no time component, for example TO_DATE('04-apr-2016, 'dd-mon-yyyy'), then the implicit time is 00:00:00 so you don't need to apply TRUNC() to it.
Good luck!

current_date usage

I would like to subtract the current date from a given date in SQL or in JDBC. I would like to have the result in hours. Not sure how to treat the date in that case. Can some one give me a basic example Please.
You don't mention which database server you use - here's a sample in MySQL.
select hour(TIMEDIFF('2011-03-15 19:59:59.000001', now()))
Note: the "hour" function doesn't deal with rounding, so if you need that, you need to do some further arithmetic.
Date functions are pretty vendor-specific, so your mileage may vary....
In standard SQL
select (date '2011-03-16' - CURRENT_DATE) as days_different,
(date '2011-03-16' - CURRENT_DATE) * 24 as hours_different
days_different hours_different
--
1 24
As I write this, CURRENT_DATE = '2011-03-15'.