I have a PSQL database that has fields for "start_time" and "end_time" in a table.
These get imported from a json file as string to a text field in the database. The string has the following format: yyyy-mm-dd hh:mm:ss
I want to do a query to give all id's of a table in the database that are within a given date range.
To do this I can do one of two things. Either I can query for a list of start and end times given a date range, and with the return from that query do a separate query for the actual data I am looking to get with WHERE start_time = ANY ('{list of dates go here}')
The other option is to figure out how to change the format of the row to something that is comparable. Something like WHERE start_time > beginning of date range AND end_time > end of date range if the fields are not text fields.
The date range is going to come from a date time picker from a website.
This might be a duplicate post but after hours of searching for a solution to either of those nothing has come up. So anything in the right direction is helpful.
Thanks
PostgreSQL should go easy on you in this case. Simply cast the string to timestamp and to the comparison.
This should work out of the box:
WHERE start_time::timestamp > '2016-01-01' AND end_time::timestamp < '2016-12-31'
Related
The form uses begin date and end date text boxes formatted as "General Date" to filter results. They are generally filled using the calendar date picker tool as format m/d/yyyy. ODBC SQL table that it's pulling the date field from is also showing format "General Date" in Design View but the field includes date and time as format m/d/yyyy H:MM:SS AM/PM.
When the same date is entered in the form begin and end date it shows no results, but I am told by my team in the past it has shown results. I am assuming that this is due to the form date picker including no time and defaulting to 12 AM in both boxes when the query runs. If that is/may be the case, is there a way to edit the end date text box to default to 11:59:59 PM of the date selected?
10/28 EDIT: I was asked to provide the SQL that refers to the end date text box. For context: the command button on the form opens query dbo_GEN_INSP_Count_Daily which is based on query dbo_GEN_INSP_Count_Monthly where the text box is mentioned. See below.
query dbo_GEN_INSP_Count_Daily
SELECT DateValue([INSP_DTE]) AS Insp_Date, Count([INSP_DTE]) AS DailyCount
FROM dbo_GEN_INSP_Count_Monthly
GROUP BY DateValue([INSP_DTE]);
query dbo_GEN_INSP_Count_Monthly
SELECT dbo_VEHICLE.DMV_VIN_NUM, dbo_INSPECTION_GENERAL.INSP_DTE, dbo_INSPECTION_GENERAL.CI_NUM, dbo_INSPECTION_GENERAL.DMV_FACILITY_NUM, dbo_INSPECTION_GENERAL.VIP_UNIT_NUM
FROM dbo_INSPECTION_GENERAL INNER JOIN dbo_VEHICLE ON (dbo_INSPECTION_GENERAL.VIP_UNIT_NUM = dbo_VEHICLE.VIP_UNIT_NUM) AND (dbo_INSPECTION_GENERAL.DMV_FACILITY_NUM = dbo_VEHICLE.DMV_FACILITY_NUM) AND (dbo_INSPECTION_GENERAL.CI_NUM = dbo_VEHICLE.CI_NUM) AND (dbo_INSPECTION_GENERAL.INSP_DTE = dbo_VEHICLE.INSP_DTE)
WHERE (((dbo_INSPECTION_GENERAL.INSP_DTE) Between [Forms]![Form1]![StartDate] And [Forms]![Form1]![EndDate]));
I'm not sure how it would play with your ODBC connection, but in the date field in the form under the data tab in the property sheet there is a default value for the form, just like in the MS Access table. You could try to put your default time in there at 11:59:59, it will automatically put quotes around it and it should reflect in your form.
You could also create a default constraint on the database itself to reflect 11:59:59 for the end date if nothing is there. Going this route you would probably need to break up your time and date fields in the database and bring them together in MS Access with a function. That way if a date was placed there with no time it would default to 11:59:59.
When the user enters the same date for both StartDate and EndDate, you want the query to return all rows containing that date regardless of the time component stored with the date.
So, instead of your current Between ... And approach, make the endpoint target less than one day after EndDate.
WHERE
dbo_INSPECTION_GENERAL.INSP_DTE >= Forms!Form1!StartDate
And dbo_INSPECTION_GENERAL.INSP_DTE < (DateValue(Forms!Form1!EndDate) + 1)
I am writing the query to show the data entries for a specific year. Date is stored in dd/mm/yyyy hh:mm:ss.(Date TIMESTAMP - e.g. 12/2/2014 0:00:00).
I am trying to display the two columns(name, orderdate) filtered by a specific year(year from orderdate). The requirement is to enter the specific year(2010 or 2020 etc) not the entire date. I tried using date_format() and regexp_replace() with WHERE but nothing helped.
Can someone help me?
If your are storing the date -- incorrectly -- as a string, then you can use string functions to do what you want:
where orderdate like '__/__/2010%'
However, you should really put your effort into storing the date using a correct format -- YYYY-MM-DD for strings at least.
I am trying to fetch the records which are having all time stamp columns.
I am using the following query to fetch the products that are created between the final date and (final date - 30) days, i.e products created during the last 30 days that fall in the 'final date' range.
I have products that are created on 30-OCT-2014. For the same products, the initiated date is 12-NOV-2014. However they are not being fetched when I using the below query.
SELECT A.ROW_ID,
A.PROD_NAME
FROM PROD A,
PROD_REL B
WHERE A.ROW_ID = B.PAR_ROW_ID
AND TO_DATE(A.CREATED_DT,'YYYY-MM-DD') BETWEEN (TO_DATE(B.FINAL_DATE,'YYYY-MM-DD') - 30)
AND (TO_DATE(B.FINAL_DATE,'YYYY-MM-DD'));
So, could you please let me know if I am missing something?
Here is a link to a SQLFiddle that demonstrates the problem.
… or just fix your format string TO_DATE(A.CREATED,'DD-MON-YYYY')
SQL Fiddle
Storing dates as DATE, is , of course, always a good starting point.
Since your data types are all dates, there is no need to use to_date. It's harmful, in fact, since to_date doesn't take a date as a parameter. Oracle has to do an implicit conversion from a date to a string, using your session's NLS_DATE_FORMAT which gets passed in to to_date and converted back to a date using the explicit format mask you specified. If the two conversions aren't using the same format mask, bad things happen.
Your WHERE clause just needs to be
AND a.created_dt BETWEEN b.final_date - 30
AND b.final_date
If I make that change, your SQLFiddle returns two rows
I am having two textfields which represents startDate and endDate.Now the problem I am facing is that I want all the records from the database which occured between this interval.But the field which stores date is TimeStamp and is of format :
24-APR-14 09.23.44.458000 PM or we can say dd-mm-yy hh.min.ss.milli AM/PM
Now obviously user entering the date is not going to enter it in such a format.So what should be query to select records from table say t1 between this date interval.
Saving Date data as Date or Datetime makes life easy.
You have tagged Mysql in the question so here is a Mysql Solution.
This is what you can do
select * from
test
where
date_format(str_to_date(`date`,'%d-%b-%y'),'%Y-%m-%d')
between '2014-04-15' AND '2014-04-24'
DEMO
You can format the user input as you want in the query in DATE_FORMAT().
User input will have a startdate and a enddate may be in datetime OR date variable
while trying to use these two input variables in the WHERE CLAUSE use BETWEEN CAST(#StartDate AS DATE) AND (CAST(#EndDate AS DATE) + 1)
This will fetch results that lies in both dates and the data range as well
I've been tasked to take a calendar date range value from a form front-end and use it to, among other things, feed a query in a Teradata table that does not have a datetime column. Instead the date is aggregated from two varchar columns: one for year (CY = current year, LY = last year, LY-1, etc), and one for the date with format MonDD (like Jan13, Dec08, etc).
I'm using Coldfusion for the form and result page, so I have the ability to dynamically create the query, but I can't think of a good way to do it for all possible cases. Any ideas? Even year differences aside, I can't think of anything outside of a direct comparison on each day in the range with a potential ton of separate OR statements in the query. I'm light on SQL knowledge - maybe there's a better way to script it in the SQL itself using some sort of conversion on the two varchar columns to form an actual date range where date comparisons could then be made?
Here is some SQL that will take the VARCHAR date value and perform some basic manipulations on it to get you started:
SELECT CAST(CAST('Jan18'||TRIM(EXTRACT(YEAR FROM CURRENT_DATE)) AS CHAR(9)) AS DATE FORMAT 'MMMDDYYYY') AS BaseDate_
, CASE WHEN Col1 = 'CY'
THEN BaseDate_
WHEN Col1 = 'LY'
THEN ADD_MONTHS(BaseDate_, -12)
WHEN Col1 = 'LY-1'
THEN ADD_MONTHS(BaseDate_, -24)
ELSE BaseDate_
END AS DateModified_
FROM {MyDB}.{MyTable};
The EXTRACT() function allows you to take apart a DATE, TIME, or TIMESTAMP value.
You have you use TRIM() around the EXTRACT to get rid of the whitespace that is added converting the DATEPART to a CHAR data type. Teradata is funny with dates and often requires a double CAST() to get things sorted out.
The CASE statement simply takes the encoded values you suggested will be used and uses the ADD_MONTHS() function to manipulate the date. Dates are INTEGER in Teradata so you can also add INTEGER values to them to move the date by a whole day. Unlike Oracle, you can't add fractional values to manipulate the TIME portion of a TIMESTAMP. DATE != TIMESTAMP in Teradata.
Rob gave you an sql approach. Alternatively you can use ColdFusion to generate values for the columns you have. Something like this might work.
sampleDate = CreateDate(2010,4,12); // this simulates user input
if (year(sampleDate) is year(now())
col1Value = 'CY';
else if (year(now()) - year(sampleDate) is 1)
col1Value = 'LY'
else
col1Value = 'LY-' & DateDiff("yyyy", sampleDate, now());
col2Value = DateFormat(sampleDate, 'mmmdd');
Then you send col1Value and col2Value to your query as parameters.