I have on my DB the dates that I can filter like this:
select *
where
a.y=2021 and a.m=2 and a.d=7
However if I run this query tomorrow I'll have to go there and change manually.
Is there a way to do this automatically as in if I run the query tomorrow I'll get d=8 and the day after d=9 and so on?
I tried to use get date but I get the following error:
SQL Error [6]: Query failed (#20210207_153809_06316_2g4as): line 2:7: Function 'getdate' not registered
I also don't know if that is the right solution. Does anybody know how to fix that?
you can use NOW to get the current date, and use YEAR , MONTH , DAY to get parts of the date
SELECT *
FROM [TABLE]
WHERE a.y=YEAR(NOW()) and a.m=MONTH(NOW()) and a.d=DAY(NOW())
The best solution is to have a date column in your data. Then you can just use:
where datecol = current_date
Or whatever your particular database uses for the current date.
Absent that, you have to split the current date into parts. In Standard SQL, this looks like:
where y = extract(year from current_date) and
m = extract(month from current_date) and
d = extract(day from current_date)
That said, date functions notoriously vary among databases, so the exact syntax depends on your database.
For instance, a common way to write this in SQL Server would be:
where y = year(getdate()) and
m = month(getdate()) and
d = day(getdate())
Related
I'm working with the following SQL Query in Redash, the query retrieves monthly data from table.
SELECT *
FROM Table
WHERE
"Date" between '2021-04-01T00:00:00.669976+00:00' and '2021-04-30T23:59:59.669976+00:00'
I'd like to know if there's a workaround to updating the WHERE clause in an efficient manner rather than manually typing it out at the end of each month.
This worked well for me:
WHERE
EXTRACT(MONTH FROM "Date") = EXTRACT(MONTH FROM CURRENT_DATE) AND EXTRACT(YEAR FROM "Date") = EXTRACT(YEAR FROM CURRENT_DATE)
In your case, I suggest you avoid any solution that involves doing a convert or other type of conversion with the GRP_Date field. By doing that, you do not allow SQL Server to be able to use an index if there is one for the GRP_Date field and this can affect your performance in a very obvious way.
And of course between is not ideal in this specific case for the reasons already mentioned in your question.
I suggest the following condition for the best performance (good use of the indexes) and to avoid problems with the hours
where GRP.GRP_date >= #since
and GRP.GRP_date < dateadd(day, 1, #until) -- #until + 1 day
In the case where:
#since = 2016-11-01
#until = 2016-11-14
where GRP.GRP_Fecha >= '2016-11-01'
and GRP.GRP_Fecha < '2016-11-15'
When ordering dates before 2016-11-15, this includes all dates from 2016-11-14 regardless of time.
I have a query in which I need to get the third month of the given reporting date using SQL and then use it as part of the query. I am able to get all the months but I specifically need to get the third month how would I go about doing that? I know this is fairly easy to do in other languages but is it possible in SQL?
SELECT REPORTING_MONTH, COUNT(*)
FROM database1 AS fb
JOIN (
--derrived core set
SELECT service_no, subscription_id
FROM database2
WHERE REPORTING_MONTH = '2015-04-01' <-- this is the reporting month
) AS c
ON fb.SERVICE_NO = c.service_no
AND fb.subscription_id = c.subscription_id
AND fb.REPORTING_MONTH = '2015-07-01' <-- THIS SHOULD BE THE THIRD MONTH
AND fb.ACTIVE_BASE_IND_NEW = 1
GROUP BY 1
ORDER BY 1
For example if the reporting month is '2015-04-01 I need the variable month to then be '2015-07-01' to be used as part of the query
You don't specify the database you are using. A typical approach would be:
SELECT REPORTING_MONTH, COUNT(*)
FROM database1 fb JOIN
database2 c
ON fb.SERVICE_NO = c.service_no AND
c.REPORTING_MONTH = '2015-04-01' AND
fb.subscription_id = c.subscription_id AND
fb.REPORTING_MONTH = c.reporting_month + interval '3 month' AND
fb.ACTIVE_BASE_IND_NEW = 1
GROUP BY 1
ORDER BY 1;
The exact syntax for + interval '3 month' varies by database.
If the field REPORTING_MONTH is text then you might have to use SUBSTRING (SQL Server) or MID (Others).
If it's a proper date field then perhaps DATEPART(month, fb.REPORTING_MONTH) = 3 will work?
My SQL is a bit rusty but try those functions.
I would like to create a WHERE condition to return results where only 1 day has passed between two timestamps. I tried this:
SELECT * FROM RDMAVWSANDBOX.VwNIMEventFct
INNER JOIN VwNIMUserDim ON VwNIMUserDim.NIM_USER_ID = VwNIMEventFct.NIM_USER_ID
INNER JOIN rdmatblsandbox.TmpNIMSalesForceDB ON TmpNIMSalesForceDB.EMAIL = VwNIMUserDim.USER_EMAIL_ADDRESS
WHERE (CONTRACT_EFFECTIVE_DATE - EVENT_TIMESTAMP) =1
But the result was an error message "Invalid Operation On An ANSI DATETIME value".
I guess that, looking at the code now, Teradata has no way of knowing whether the "1" in "= 1" is a day, hour or year.
How would I select data where only 1 day has passed between CONTRACT_EFFECTIVE_DATE and EVENT_TIMESTAMP?
Same again for 2 days, and 3 days etc?
If both columns are DATEs you can use =1which means one day.
For Timestamps you need to tell what kind of interval you want:
WHERE (CONTRACT_EFFECTIVE_DATE - EVENT_TIMESTAMP) DAY = INTERVAL '1' DAY
But i'm not shure if this is what you really want, what's your definition of 1 day?
Edit:
Based on your comment the best way should be:
WHERE CAST(CONTRACT_EFFECTIVE_DATE AS DATE) - CAST(EVENT_TIMESTAMP AS DATE) = 1
This avoids dealing with INTERVAL arithmetic :-)
Not sure about Teradata, but I think most versions of SQL have built-in date math functions. In MSSQL for instance you could do this:
...
WHERE DATEDIFF(DAY, CONTRACT_EFFECTIVE_DATE, EVENT_TIMESTAMP) = 1
Or if you wanted to make sure 24 hours had passed you could do:
...
WHERE DATEDIFF(HOUR, CONTRACT_EFFECTIVE_DATE, EVENT_TIMESTAMP) = 1
Other SQL's have their own versions of this, and you may have to use 'D' or 'DD' instead of 'DAY' or something (and maybe 'HH' instead of 'HOUR' likewise).
Suppose I have the following query:
select customer_name, origination_date
where origination_date < '01-DEC-2013';
I would like to select all customers that have an origination date older than 30 days. Is there a way in SQL (oracle, if specifics needed) to specify it in a more dynamic approach than manually entering the date so that I don't need to update the query every time I run it?
Thanks!
Sure try something like this:
select customer_name, origination_date where
origination_date >= DATEADD(day, -30, GETUTCDATE());
This basically says where the origination_date is greater or equal to 30 days from now. This works in Microsoft SQL, not sure but there is probably a similar function on Oracle.
in Oracle, when you subtract dates, by default you get the difference in days, e.g.
select * from my_table where (date_1 - date_2) > 30
should return the records whose date difference is greater than 30 days.
To make your query dynamic, you parameterize it, so instead of using hard coded date values, you use:
select * from my_table where (:date_1 - :date_2) > :threshold
If you are using oracle sql developer to run such a query, it will pop up a window for you to specify the values for your paramteres; the ones preceded with colon.
How would I go about doing a query that returns results of all rows that contain dates for current year and month at the time of query.
Timestamps for each row are formated as such: yyyy-mm-dd
I know it probably has something to do with the date function and that I must somehow set a special parameter to make it spit out like such: yyyy-mm-%%.
Setting days to be wild card character would do the trick but I can't seem to figure it out how to do it.
Here is a link to for quick reference to date-time functions in mysql:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Thanks
I think EXTRACT is the function you are looking for:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM NOW())
you could extract the year and month using a function, but that will not be able to use an index.
if you want scalable performance, you need to do this:
SELECT *
FROM myTable
WHERE some_date_column BETWEEN '2009-01-01' AND '2009-01-31'
select * from someTable where year(myDt) = 2009 and month(myDt) = 9 and day(myDt) = 12