In MS Access SQL, I have a query that uses two excel tables, one with all the data and a second as an input, the input fields are names, start date and end date. The query i wrote will return data from the first tale where the name is matched and the date falls between the range of start to end.
The problem is when the start and end date fields are left empty the query returns nothing, i need a query that when the date range is empty it will just match the name and return all dates with the matching name. I attached my query that I have so far
You want logic like this:
where (sheet1.b1_file_dd > [inputs].start_date or [inputs].start_date is null) and
(sheet1.b1_file_dd < [inputs].end_date or [inputs].end_date is null)
Related
I want a query for selecting data between two dates (p_startdt,p_enddt) which is given by the user, if there is no input then by default data of last one year will be given as output. I am not able to put case for null or no input
where invc_dt between p_startdt and p_enddt
Use NVL to handle the case of a NULL value. The following example will take start date as a year ago if p_startdt is null and p_enddt as the current date if p_enddt is null:
WHERE invc_dt
BETWEEN NVL(p_startdt,ADD_MONTHS(SYSDATE,-12)) AND
NVL(p_enddt,SYSDATE)
Note: I'm assuming the data type of the column invc_dt is DATE.
I need to return a list of names which have parameters which fall between 2 dates. I have hard-coded two dates as part of the data verification. My sql query in BigQuery is as follows:
declare DS_START_DATE FLOAT64;
declare DS_END_DATE FLOAT64;
SET DS_START_DATE = 1578390532050;
SET DS_END_DATE = 1578391211289;
SELECT DISTINCT
Name AS block_names
FROM
my_data_source
LEFT JOIN
UNNEST (holes) AS n_holes
ON
1=1
WHERE
(n_holes.LastModifiedHoleDate ) > CAST(DS_START_DATE as FLOAT64)
AND n_holes.LastModifiedHoleDate < CAST(DS_END_DATE as FLOAT64)
Note: the DS_START_DATE and DS_END_DATE are both in UNIX time.
So basically, I am querying for results that were modified on the 7th of January, which will return only one result.
The above query return only one result, which is correct.
I have changed the format of the query slightly so that I can use it in the connection to my table in BigQuery from Data Studio:
SELECT DISTINCT
Name AS block_names,
n_holes.LastModifiedHoleDate as LM
FROM
my_data_source
LEFT JOIN
UNNEST (holes) AS n_holes
ON
1=1
WHERE
(n_holes.LastModifiedHoleDate ) >= CAST(#DS_START_DATE as FLOAT64)
AND n_holes.LastModifiedHoleDate <= CAST(#DS_END_DATE as FLOAT64)
I have enabled the date parameters in my Data Studio data source, and have finished creating the data source.
I have then made a test report using the above data source for the report. I simply have a date range control and a chart on my report.
No matter what range I choose on the date range, I get two results on the chart, where I should only get one.
Normally the tables and charts I have used in Data Studio have the option to select a date range dimension which links the data to the date range selected on the date time picker:
The table that I have added to this report has no such option. I am assuming that this is correct because we are using a data source that needs a start and end date?
The parameters option in the chart has nothing in it:
Again, I am assuming that Data Studio doesn't need parameters specified because I am using start and end dates.
What have I missed that my date range picker is not affecting the data displayed in my chart? It seems like it is linked to the chart automatically, but the results are wrong.
Many thanks in advance!
You need to convert #DS_START_DATE and #DS_END_DATE into a Unix timestamp if you want to compare them with one. For me this did the trick:
WHERE
n_holes.LastModifiedHoleDate >= UNIX_MILLIS(PARSE_TIMESTAMP('%Y%m%d', #DS_START_DATE))
AND n_holes.LastModifiedHoleDate <= UNIX_MILLIS(PARSE_TIMESTAMP('%Y%m%d', #DS_END_DATE))
I hope it also works for you!
I'm trying to build a SSRS report with multiple parameters, and when I add a standard START_DATE and END_DATE parameter I get the expected results from my SQL query line:
WHERE SOME_FIELD BETWEEN (:P_START_DATE) and (:P_END_DATE)
I would like to do something like the following scenario:
Setup:
Date Field 1 & 2: WHERE NEXT_SHIP_DUE_DATE BETWEEN (:P_NEXT_SHIP_START_DATE) AND (:P_NEXT_SHIP_END_DATE)
Date Field 3 & 4: AND PRIMARY_IMAGE_DATE BETWEEN (:P_PID_START_DATE) AND (:P_PID_END_DATE)
All date parameters in SSRS report allow NULL values and are initially set to NULL.
User should be able to:
Get all records when no date field parameters are requested
Get only records based off the date fields that user selects This may be 1 or more.
Scenarios:
Example 1: User selects no date fields - returns no data
Example 2: User selects date fields 1 & 2 only - returns no data
Example 3: User selects all date fields same date range - RETURNS CORRECT DATA
Example 4: User selects all date fields different date ranges - SSRS error: date not valid for month specified
I've tried several different pieces of SQL but no luck so far. Any help would be appreciated!
You need to add some kind of null check, be careful of brackets.
WHERE (NEXT_SHIP_DUE_DATE BETWEEN (:P_NEXT_SHIP_START_DATE) AND (:P_NEXT_SHIP_END_DATE) OR ((:P_NEXT_SHIP_END_DATE) IS NULL AND (:P_NEXT_SHIP_START_DATE) IS NULL))
AND (PRIMARY_IMAGE_DATE BETWEEN (:P_PID_START_DATE) AND (:P_PID_END_DATE) OR ((:P_PID_END_DATE) IS NULL AND (:P_PID_START_DATE) IS NULL))
I have a query that searches saved records and creates a report based on the record(s). Some of the fields are searchable either independently or in association with other parts of the saved record (e.g., one could search the ID, location, and/or whether or not police were notified). However, I run into problems when searching by date.
I have fields for the user to input Start Date and End Date of their desired date range. When ONE or NEITHER field are filled, the search pulls up all records AFTER the Start Date, BEFORE the end date, or ALL the records. When BOTH fields are filled, the search pulls up a record where all fields are blank (which does not exist in the table).
Each searchable field uses the same criteria in the Query:
Like Nz([field that you're searching],"*")
But the date range uses a modified version (sorry if it's SUPER clunky):
Like Nz(([Data_Input_Table].[Day_Current])>=[Forms]![Search_Form]![Start_Date_Lookup_text] And ([Data_Input_Table].[Day_Current])<=[Forms]![Search_Form]![End_Date_Lookup_text],"*")
Ideally, I'd like the user to search by NEITHER, ONE, or BOTH Start Date and End Date.
Please help!
You can't use Like on dates. Try this:
[Data_Input_Table].[Day_Current] >= Nz([Forms]![Search_Form]![Start_Date_Lookup_text], [Data_Input_Table].[Day_Current]) And [Data_Input_Table].[Day_Current] <= Nz([Forms]![Search_Form]![End_Date_Lookup_text], [Data_Input_Table].[Day_Current])
I have a excel format dataset that need to be imported to a table, one column is a date, but the data is stored in number format, such as 41275, when importing data, i tried to choose data format yyyy-mm-dd, it gives an error: not a valid month, also tried MM/DD/YYYY also error: day of month must be between 1 and last day of month. does anyone know what is this number and how can i convert it to a date format when importing it into the database?Thanks!
The expression (with respect to the Excel's leap year bug AmmoQ mentions) you are looking for is:
case
when yourNumberToBeImported <= 59 then date'1899-12-31' + yourNumberToBeImported
else date'1899-12-30' + yourNumberToBeImported
end
Then, you may either
Create a (global) temporary table in your Oracle DB, load your data from the Excel to the table and then reload the data from the temporary table to your target table with the above calculation included.
or you may
Load the data from the Excel to a persistent table in your Oracle DB and create a view over the persistent table which would contain the above calculation.
The number you got is the excel representation of a certain date ...
excel stores a date as the number of days, starting to count at a certain date ... to be precise:
1 = 1-JAN-1900
2 = 2-JAN-1900
...
30 = 30-JAN-1900
so, to get your excel number into an oracle date, you might want to try something like this...
to_date('1899-12-31','yyyy-mm-dd') + 41275