A colleague wrote a view that needs changing; the problem is I was not there when it was made, so I do not know it inside out as if I coded this SQL view.
My colleague created a view and used CTEs to grab data faster. This is the CTE I'm having trouble with
vDateRange AS
(
SELECT
Date,
Year,
Month,
Day,
DayOfWeek
FROM
Util.fnDateRange('2017-08-17', '2017-08-22')
)
so as you can see this view will only get data from that date range, can someone show me how I can grab FROM something else instead of the Util.fnDateRange() because this CTE is used so many times in the view I can't just comment it out.
I changed the query by using the range like this instead:
Util.fnDateRange('01-01-2014', GETUTCDATE())
now i can get from 2014 till the date of today.
Related
I'm working on a query that pulls a date from another query, I have my reasons for the nesting. The problem I'm facing is that there is a field that is called DueDate.
My SQL is
SELECT DueDate
FROM qryDueDates
WHERE DueDates <= DateAdd("d",60,Date())
The data causing the issue is when it equals something like "1/25/2019", "11/19/2019" or any date in 2019.
Goal
I need to limit the results to show dates that are expired or expiring within 60 days or less.
I'm trying to prepare the dataset for the conditional formatting.
if you can put your nested sub-query in your post that may give better picture, and if you can mention what is the error you are getting that may also help. Since you mentioned that you are getting error only when sub-query returns certain dates, I would suggest that cast your sub-query result to DATE if you have not already done.
Below is my attempt to help you with limited information I could extract from your post. I have used some of MS-SQL function below, please replace with your DB specific function.
SELECT myDates.* FROM (select COLUMN_NAME DueDates from TABLE_NAME) as myDates WHERE myDates.DueDates <= DateAdd("d",60, GETDATE())
Turns out that the original query was screwing it up. I moved the query into the main one and it worked.
I have a basic query (Select field from table, etc) where I can easily put in created between 1491721200 and 1492326000 and it works fine. It's simple! My question is, can I have one query that will give me data for multiple time ranges? I would like to select the data from Sunday through Thursday from 2-8pm PST in this one query.
Is this possible?
Thanks!
Yes, simple or will do the trick
SELECT * from table where
created between 1491721200 and 1492326000
OR created between 149999999 and 150000000
I'm looking to calculate how many days have passed since a specific date, retrieved from a table in my database. Based on the info I've found on W3Schools (Here), I have attempted using DATEDIFF, but am coming up against a couple of different errors I can't seem to work around.
I have included my code below, and based on this, what I want to happen is this: Select the "DD" from the "Wave_Data" table, and, based on "sysdate", work out how many days have lapsed since then.
SELECT DATEDIFF(WEEKDAY,:P1_DD,SYSDATE)
FROM WAVE_DATA
WHERE WAVE_NUMBER = :P1_WAVE;
The final calculation would then be inputted into a text field within my ApEx database.
Thank you in advance for any help you may be able to provide,
Dominic
In Oracle you can just subtract one Date from another to get the difference (in days) between them:
SELECT SYSDATE - :p1_dd
FROM Wave_Data
WHERE Wave_Number = :p1_wave;
If you want to know the difference between the dates without any time parts then you can do:
SELECT TRUNC( SYSDATE ) - TRUNC( :p1_dd )
FROM Wave_Data
WHERE Wave_Number = :p1_wave;
or
SELECT FLOOR( SYSDATE - :p1_dd )
FROM Wave_Data
WHERE Wave_Number = :p1_wave;
I have a database where users stores their birthday. y-m-d, and I'm trying to get every user that has the same birthday; the year can be different between each user.
So how do I turn this to a working script:
SELECT username FROM table_name WHERE birthday='$birthday'
$birthday gets its data from a form, where the inputs is example: 2002-02-02. And if users have this birthday it should echo it out. But the problem is that it checks with the year, and I'm trying to only get month and day, not year.
I have tried with EXTRACT(MONTH FROM ...) but didn't get it to work. What am I missing?
You should store your data as DATE. Then the date/time like EXTRACT functions will work.
I recommend adding new column and filling it with data from the original one using CAST. Then dropping the original column. Also consider using parameters instead of string concatenation to prevent SQL injection.
select
extract(MONTH from cast('2002-02-02' as date))
from rdb$database
I have a function below that I'm using under my SELECT statement that I'm trying to limit the results of in my where statement but can't seem to figure out how to name it or tell it to reference that specific function. I'm thinking I can accomplish this by putting the function in its own WITH statement, but would rather just do it this way if possible.
SELECT
DAYS (CURRENT DATE) - DAYS (DATE(B.TSTAMP)) AS "TRANSIT"
From this function I want to put "WHERE TRANSIT >= '8'" When I do this now, I get the error "TRANSIT" is not valid in the context where it is used. Any suggestions?
You can't refer to something from the SELECT clause in your WHERE, it processes the WHERE first (see this article).
You can "fix" your problem by pushing your query into a sub-query, and then referring to the name:
SELECT *
FROM (
SELECT DAYS (CURRENT DATE) - DAYS (DATE(B.TSTAMP)) AS "TRANSIT"
FROM your_table
) A
WHERE TRANSIT >= 8