I have two tables I'm trying to join on a date field. The date fields are like:
01/01/2009
01/01/2009 1:00:00 AM
01/01/2009 2:00:00 AM
.....
One table has multiple entries for each hour. The other is just a date conversion table. I built a conversion table to help line up data.
When I run the query I only get one entry.
01/01/2009 1:00:00 AM
I should be getting a number of years of data returned.
I have tested the data in excel and it says other dates are equal. Also have both the fields within each table set to the same format. The format is Date/Time -> General Date
SELECT h2009v1.flow_rate_datetime, tblDateConv.DateTimeSE, h2009v1.flow_rate
FROM h2009v1 INNER JOIN tblDateConv ON h2009v1.flow_rate_datetime = tblDateConv.DateTimeSE;
Note: I tried changing the date formats to see if that made a difference.
Related
I have a table like this in SQLITE3:
I need to query this table by ID|DOC_ID|TRANS_DOC_ID and most importantly by DATE because I need to get the data day by day. ex: TODAY|YESTERDAY|ETC
So far the query is easy, as I can just do this to get the rows by day:
SELECT * FROM CLIENTRECORD WHERE DATE = '2020-12-01'
The problem is when I need to display specific records on other dates:
ex: I have a row with DATE 2020-12-01 but I also want it displayed on DATE 2020-01-01 or maybe 2020-01-02, etc. What do I do in this situation? and so I thought about adding another col as DATES which was supposed to be an array of comma-separated dates BUT I researched that this is a BAD solution, I also thought about adding a separate TABLE just for dates but since the dates aren't fixed (they might contain 1 date or maybe even 10 who knows), I am confused as to what I am supposed to do.
The end goal is that a row may or may not contain more than 1 date, would look something like this if I want to query for the row with or without multiple dates:
SELECT * FROM CLIENTRECORD WHERE DATE = '2020-12-01' OR DATES LIKE '2020-12-01'
something similar to it.
Here trip 1 involves 2 activity_code in a single day and also concludes in a single day and most other activities are just single day but i have one trip that span over more than one day.
What could be the best possible way to store date range for that column that span more than one days.
Splitting the column into multiple begin date and end date just doesn't make sense as there would be many blank columns?
trip_id(pk,fk) Activity_code(pk,fk) date
1 a1 1st October 2015
1 a2 1st October 2015
2 a3 2nd -5th October 2015
Keep in mind that i need to search the activity_code on basis of month. such as list all the activity code that occur in October ?
Is it possible to insert a range of date in a single column or any other design solution ?
Is there any datatype that can represent the date range in single value ?
PS: oracle 11g e
Store the date ranges as FirstDate/LastDate or FirstDate/Duration.
This allows you to store the values in the native format for dates. Storing dates as strings is a bad, bad idea, because strings don't have all the built-in functionality provided for native date types.
Don't worry about the additional storage for a second date or duration. In fact, the two columns together are probably smaller than storing the value as a string.
Splitting the date into start date and end date would be ideal. Storing dates as strings is not recommended. If you store your dates as strings then there is a possibility of malformed data being stored in the column since a VARCHAR2 column will allow any value. You will have to build strong validations in your script while inserting the data which is unnecessary.
Secondly, you will not be able to perform simple operations like calculating the duration/length of the trip easily if both the start_date and end_date are stored in the same column. If they are stored in different columns it would be as simple as
SELECT trip_id, activity_code, end_date - start_date FROM trips;
I would like to have a date and time column in my table. The main purpose of having these 2 columns is to be able to return query results like:
Number of treatments done in the period November 2011.
Number of people working in shifts between 00:01 and 08:00 hours.
I have two tables, which have the following attributes in them(among others):
Shift(day, month, year)
Treatment(start_time, date)
For the first table- Shift, query results need to return values in
terms of (ex: December 30,2012)
For the second table, start_time needs to have values like 0001 and
0800(as I mentioned above). While, date can return values like
'November 2011'.
Initially I thought using the date datatype for declaring each of the day/month/year/date variables would do the job. But this doesn't seem to work out. Should I use int, varchar and int respectively for day, month and year respectively? Also, since the date variable does not have component parts, will date datatype work here? Lastly, if I use timestamp data type for the start_time attribute, what should be the value I enter in the insert column- should it be 08:00:00?
I'm using SQL Server 2014.
Thank You for your help.
AFAIK it is better to use one column by type of DateTime instead of two columns which hold Date and Time separately.
Also you could simply query this column either by Date or Time by casting it to corresponding type :
DECLARE #ChangeDateTime AS DATETIME = '2012-12-09 16:07:43.937'
SELECT CAST(#ChangeDateTime AS DATE) AS [ChangeDate],
CAST(#ChangeDateTime AS TIME) AS [ChangeTime]
results to :
ChangeDate ChangeTime
---------- ----------------
2012-12-09 16:07:43.9370000
I have an MS Access database table datetime column. When I select a particular month (say, July), I have to get datewise data in that month.
The output should appear in the same format as the attached image.
Every Employee who comes in on a particular date should display “P” for that day. If the Employee doesn’t come in on a particular day (like Sat or Sun), then I have to display “WO” for that day.
When an Employee has not come in (like Sat or Sun), then there is no entry in the log table for that date.
How could an Access query be written to obtain this output? I am using an MS Access 2003 database.
Edit: we have to use TRANSFORM and PIVOT, but the issue is when an employee is not available (Sat, Sun) we still need to show data in the output.
Set up a query that reads EmpID and CheckTime from the first table, and adds one additional column:
DateWise: IIf(Weekday([CheckTime])=1 Or Weekday([CheckTime])=7,"WO","P")
You will need an additional table with every date of the year in it (we'll call it YearDates). Left join that table to your query like so:
Select YD.YearDates, Q2.* from YearDates YD LEFT JOIN Query2 Q2 ON YD.YearDates = DATEVALUE(Q2.CheckTime)
The DATEVALUE will strip the time off your dates in CheckTime so they will match date against date.
I'm using a GoDaddy-based SQL Server 2005 to store sales from a web site. I have a datePaid column that is formatted as smalldatetime. When I query the database to display all orders for a certain date, using this query:
SELECT DISTINCT
purchase.orderID, wfRegister.firstName, wfRegister.lastname,
storeOrder.subtotal, storeOrder.handling, storeOrder.total,
storeOrder.datePaid, storeOrder.dateOrdered, storeOrder.paypalID
FROM
wfRegister
INNER JOIN
(storeOrder INNER JOIN purchase ON storeOrder.orderID = purchase.orderID) ON wfRegister.customerID = storeOrder.customerID
WHERE
storeOrder.deleted = 0
AND storeOrder.datePaid BETWEEN '03/21/2013' AND '03/21/2013'
ORDER BY
storeOrder
I only get one record returned for the 21st, which is not correct. If I change the query to storeOrder.datePaid BETWEEN '03/21/2013' AND '03/22/2013 I get 7 records returned for the 21st, which is correct, but then I also get records from the 22nd, which I don't want. Is there a way to query the datePaid field for the 21st and get all 7 records returned and not just 1? My client imports the data into Excel and does not want to have to delete the records from the 22nd to get all the records from the 21st
For date arithmetic in SQL Server using strings, I would recommend to always use the ISO-8601 standard format: YYYYMMDD - this is the only format that is independent of any regional and/or language settings.
To reliably get all orders from a given day, you should use
AND storeOrder.datePaid >= '20130321' AND storeOrder.datePaid < '20130322'
The BETWEEN seems handy - but it's inclusive, e.g. you get the data from the 22nd of March, too.
This clause here gives you all records from the 21st of March, regardless of their time - but nothing from the 22nd.