Using Access 2003
Table
ENO Time
001 020000
001 040000
001 220000
002 030000
002 050000
005 230000
So on…,
Time Date type is Varchar in the Database. How to convert a Varchar into Datetime?
Here I want to get a total of time for the ENO.
Select eno, sum (time) from table group by eno
It showing error like – data type mismatch criteria error
Expected Output
001 26:00:00
002 08:00:00
So on…,
Need Query Help.
You are best off working in seconds. Use the Mid() function to break up the string and calculate the number of seconds, sum the times, and then format the result however you like. If you write it as a single query you'll need to use the same long expression in three different places, so for improved readability I would do it as two queries. The first is:
SELECT eno, sum(val(mid(time,1,2))*3600+val(mid(time,3,2))*60+val(mid(time,5,2))) AS secs
FROM table
GROUP BY eno;
Save this query in access as "enosums", for example. The second query is:
SELECT eno, format(secs/3600, "00:") & format((secs/60 Mod 60), "00:") & format(secs Mod 60, "00")
FROM enosums;
The reason I suggest this method is that even if you manage to convert to datetime values (which you can do by using the Mid() function and concatenation operator to convert the "hhmmss" string into "hh:mm:ss" format and then apply the TimeValue function), there is no easy way to print this in the output format you need because the Format() function will only go up to 23:59:59 and then wrap back to 00:00:00.
Related
I have a table exchange_rates with columns ( Id , currency, date , selling_rate).
Sample data :
Id
currency
date
selling_rate
1
usd
10/11/2021
80
2
usd
15/11/2021
82
3
usd
29/11/2021
81
4
eur
30/11/2021
95
I want to get last entered (by date) for usd selling_rate
$sql = " SELECT selling_rate
FROM echange_rates
WHERE currency=usd
ORDER BY date DESC
LIMIT 1
";
Result should be 81 (for usd latest entry is 29/11/2021)
I have tested your query using the table data you provided except I addded a couple of quotation marks for you currency column due to its string datatype. The answer I got is 81. I suppose you ran into a syntax error for that? By the way be careful with the date column you defined. By default, MySQL does not support dd/mon/year format naturally. Therefore, to adjust to your depicted data format, I used the varchar type. This is prone to mistakes when doing comparisons as the initial character value matters most. e.g. insert into tb values (5,'usd','11/12/2021',84); The answer you get is still 81 rather than 84.
My suggestion is to use the date type for the column which is meant to be a date, then use date_format function to display it in the intended way. In this case , date_format(`date`,'%d/%m/%Y') will do the work.
i have a table like this
|id| date |name|
1 23/11/20 jake
2 01/07/20 jhon
3 23/05/20 blake
4 11/02/20 drake
5 1/03/14 crake
i ran a query like this
WHERE date >= '1/07/20' AND date <= '23/11/20'
i expected a result where i would get only the results between those dates
but i got some results which were from 2014
the data type for the date column is varchar
#note i can not change the datatype
how can i only get dates between the two ?
String-wise comparison is the problem: typically, '10/01/19' (Janurary 10th, 2019) is greater than '01/01/20' (January 1st, 2020), because the former starts with 1, and the later with 0.
You need to turn these strings to dates before you can compare them:
where str_to_date(date, '%d/%m/%y') between '2020-07-01' and '2020-66-23'
This is inefficient, because the entire column needs to be converted before the filtering can happen. I would warmly recommend fixing your data model, and store dates as dates.
Side note: your strings need to be consistently formatted as mm/dd/yy for this to work; if you have varying formats - or strings that do not map to valid dates - then you have a bigger problem than what you have asked here.
Here is a sample table that I am using,
User_id timestamp action
1 2020-10-01 09:00:00 Opened page
1 2020-10-01 09:10:00 Closed page
2 2020-10-02 04:00:00 Signed up
3 2020-10-02 06:00:00 Opened page
3 2020-10-03 11:00:00 Made a booking
3 2020-10-03 09:30:00 Closed page
need to write a SQL query to find the average time spent by a user on the page.
The expected answer is just a number which represents the average time spent by an average user on the page.
You can’t use SQL to calculate how much time a user spends on different pages of your UI application. You will need to implement this logic on your UI whenever there is an event such as when the user navigates to another page or a button click etc. You capture the timestamps you need on the UI and then make a database call through an SP call or Query through your server side code (such as .Net, Java or Node.js).
Once you have captured the data from the UI you will be able to implement any kind of logic on that data through an SP or a function or something like that in using SQL.
If you use TIMESTAMPDIFF(), and set its argument to SECOND, you can get back the difference of two datetime fields in a record in a manner that can be summed and divided. Documentation:
Returns datetime_expr2 − datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions.
Then use SUM() to sum up these values, and divide by the results of COUNT(). Documentation:
SUM(): Returns the sum of expr. If the return set has no rows, SUM() returns NULL.
COUNT(): Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement.
Your code will then basically look like this. You may need to make some adjustments based on your database setup.
SELECT
SUM(
TIMESTAMPDIFF(SECOND, OrigDateTime, LastDateTime)
) / (select COUNT(id) FROM yourTable)
AS average
FROM yourTable;
This, of course, follows our standard formula for calculating an average:
sum(differences) / count(differences)
I am having a table in MySQL Server where there is a column name StateStartTime and StateEndTime Datatype Varchar(100). It basically stores Time as HH:MM Format. As per the requirement now i it to gives me Minutes count i.e. 01:00 gives me 60, 01:30 -> 90. Please help me to write the query for MySQL, so that will convert Varchar (HH:MM) into Minutes(integer).
This Topic have the same problem but I need a solution for MySql
SELECT LTRIM(DATEDIFF(MINUTE, 0, StateStartTime))
FROM Time
Giving me error #1582 - Incorrect parameter count in the call to native function 'DATEDIFF'
You can try using the function TIME_TO_SEC() and divide the result by 60.
Example:
select TIME_TO_SEC(StateEndTime)/60 from table;
I have a mySQL query which is outputting decimal fields with a comma.
SELECT Metals.Metal, FORMAT(Fixes.GBPam, 3) AS AM, FORMAT(Fixes.GBPpm, 3) AS PM,
DATE_FORMAT(Fixes.DateTime, '%d-%m-%y') AS Date
FROM Fixes, Metals
WHERE Metals.Id = Fixes.Metals_Id
Fields GBPam and GBPpm are both of type decimal(10,5)
Now I want columns AM and PM to be formatted to 3 decimal places in my sql query - Correct
I want values in the thousands to be formatted as xxxx.xxx and not x,xxx.xxx - Incorrect
Example output from mysql query:
Metal AM PM Date
Gold 1,081.334 NULL 11-09-12
Silver 21.009 NULL 10-09-12
Platinum 995.650 NULL 11-09-12
Palladium 416.700 NULL 11-09-12
Can you see that output for Gold AM is 1,081.334? How can I get it to output 1081.334?
This is a pain in the ass for me because I have to then muck about in PHP to remove the comma. I would prefer to just get mysql to format it correctly.
Just use ROUND, this is a numeric function. FORMAT is a string function
ROUND(Fixes.GBPam, 3)
you can use replace command for this purpose.
REPLACE(Fixes.GBPam,',','')
EDIT:
With respect to your question you could do something like this:
SELECT Metals.Metal, ROUND(REPLACE(Fixes.GBPam,',',''),3) AS AM,
ROUND(REPLACE(Fixes.GBPpm,',',''),3) AS PM,
DATE_FORMAT(Fixes.DateTime, '%d-%m-%y') AS Date
FROM Fixes, Metals
WHERE Metals.Id = Fixes.Metals_Id
Use replace function. Whether the field is integer or varchar, it will work.
select replace(Fixes.GBPam,',','.');