To avoid cosmetic adjustments in the presentation layer, I need to remove trailing zeros from my data outputs - e.g. in my Hours column I need "2" as opposed to "2.000" or "18.5" as opposed to "18.500".
Having reviewed existing resources, I've come up with the following:
SELECT
DATE_FORMAT(U.START_TIME + INTERVAL 1 hour, '%H:%i') as Start
, DATE_FORMAT(U.END_TIME + INTERVAL 1 hour, '%H:%i') as End
, CAST(CAST(TIMESTAMPDIFF(MINUTE, U.START_TIME, U.END_TIME)/60 as DECIMAL (6,2)) as FLOAT) as Hours
, REQUESTER_COMMENT as Requester_Commment
FROM VW_USER_SCHEDULE_INSTANCES U
I keep getting the error message:
Error Code: 1064. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'FLOAT), REQUESTER_COMMENT as Requester_Comment
I'm struggling to work out what needs to be adjusted to avoid the error. Trying to integrate float is preventing any outputs. Apologies if I'm missing something obvious - I'm a total SQL beginner. Any assistance would be much appreciated!
You should get into the habit of reading the documentation for functions like CAST(). See https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_cast
If you read the docs, you would learn that CAST() doesn't support FLOAT as a destination type. You can use BINARY, CHAR, DATE, DATETIME, DECIMAL, JSON, NCHAR, SIGNED INTEGER, TIME or UNSIGNED INTEGER. But not FLOAT or DOUBLE.
I suggest you try the ROUND() function. See https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_round
But my preferred answer is that you should use SQL for fetching raw data, and do formatting for a given presentation in your application code.
Just use to_seconds():
SELECT DATE_FORMAT(U.START_TIME + INTERVAL 1 hour, '%H:%i') as Start,
DATE_FORMAT(U.END_TIME + INTERVAL 1 hour, '%H:%i') as End,
(TO_SECONDS(U.END_TIME) - TO_SECONDS(U.START_TIME)) / (60*60) as Hours,
REQUESTER_COMMENT as Requester_Commment
FROM VW_USER_SCHEDULE_INSTANCES U;
You can format it however you like.
Related
Is there any sql command which I can insert into the stated query so I can convert the timestamp. Although it could be done separately which I have seen so far but I am trying to find something which I can add to the stated query as that would be helpful because I am using other queries to retrieve the data as well. If you any other questions please do mention. Addition: rating_timestamp contains both time and date.
SELECT rating_id,
rating_postid,
rating_posttitle,
rating_rating,
rating_timestamp,
rating_username,
rating_userid
FROM wp_ratings;
In cases of date arithmetic, it is especially important to specify the DBMS you are using - Oracle's math is different from Postgres' math is different from SQL Server's math is different from MySQL's math is...
This assumes that you are using SQL Server. Since there is no built in command to do this conversion, you need to create your own function to do that. The function below takes a UNIX / Linux timestamp and converts it to an SQL Server datetime.
CREATE FUNCTION dbo.fn_ConvertToLocalDateTime (#unixdate BIGINT)
RETURNS DATETIME
AS
BEGIN
DECLARE #UTCTimeOffset BIGINT
,#LocalDatetime DATETIME;
SET #UTCTimeOffset = DATEDIFF(second, GETUTCDATE(), GETDATE())
SET #LocalDatetime = DATEADD(second, #unixdate + #UTCTimeOffset, CAST('1970-01-01 00:00:00' AS datetime))
RETURN #LocalDatetime
END;
GO
I wast sure about about Sql version before. This worked perfectly for me.
FROM_UNIXTIME(rating_timestamp,'%h:%i:%s %D %M %Y')
Development is localhost running version 5.6.16, production is 5.1.73-cll
The DATE_ADD of this query returns NULL on production, but in development is does exactly what I want it to(adds 90 minutes to the game_time column), The game_time column is a string that contains time in the following format: '21:00'.
This is the query:
SELECT TIME(game_time),
DATE_ADD(TIME(game_time),
INTERVAL 90 MINUTE),
TIME(NOW())
FROM games
What is going on? What am i doing wrong?
I know time should be in a TIMESTAMP, or TIME, but I'm working on someone elses code, I didn't start this from scratch myself.
I've also just noticed that TIME() returns different things, in development, TIME('21:00') returns 21:00:00.000000, in production I only get 21:00:00
Managed to get around, not pretty, but it works.
SEC_TO_TIME(TIME_TO_SEC(TIME(game_time))+5400)
You better develop with the same version as the production server:
Your old version will convert your TIME value to a date and because it's an invalid date, it will get NULL, see manual chapter Conversion Between Date and Time Types
Here's the relevant part:
Before 5.6.4, MySQL converts a time value to a date or date-and-time
value by parsing the string value of the time as a date or
date-and-time. This is unlikely to be useful. For example, '23:12:31'
interpreted as a date becomes '2023-12-31'. Time values not valid as
dates become '0000-00-00' or NULL.
Edit:
To get a TIME value with the desired result, you could use ADDTIME.
This could be working:
SELECT TIME(game_time),
ADDTIME (TIME(CONCAT(CURDATE(), ' ', game_time))),
'01:30:00'),
TIME(NOW())
FROM games
untested, because I have no such old MySQL version anymore.
Try moving the conversion to time outside the DATE_ADD:-
SELECT TIME(game_time), TIME(DATE_ADD(game_time, INTERVAL 90 MINUTE)), TIME(NOW())
FROM games
DATE_ADD works on a DATE or DATETIME field, and as it is you are passing it a TIME field.
This is relatively easy but I've been staring at the code too long and making mistakes. I need to validate that a table has been populated for the day and the query simply takes today's date - curdate() - against the date_pulled column but I'm getting Error Code: 1111. Invalid use of group function. Where am I messing up.
select * from test where max(date(date_pulled))=curdate();
you dont need max here. you already telling the query to choose the date which is equal to current date.
try that:
select * from test where date(date_pulled)=curdate();
I have MySQL 5.6.17_1 and have query that is used for 5.1.xx
select
schtermid,idfptnsubid,d.idfptnid,idflinksetid,
cast(count(distinct cthr) as real)/cast(count(distinct s.schid) as real)*100 as ctr, cast(sum(status) as real)/cast(count(*) as real)*100 as pfiled,
count(distinct s.schid) as schcount
from source.kdm_session as
This complains
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'real)/cast(count(distinct s.schid) as real)*100 as ctr, cast(sum(status) as' at line 3
Which part is wrong? What should I change to get the same thing to happen?
As per the docs, real is not a valid type:
The type for the result can be one of the following values:
BINARY[(N)]
CHAR[(N)]
DATE
DATETIME
DECIMAL[(M[,D])]
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]
Try this for your calcs:
(count(distinct cthr) * 1.0)/(count(distinct s.schid) * 1.0)*100 as ctr,
(sum(status) * 1.0)/(count(*) * 1.0)*100 as pfiled,
This is rather a mess - you are mixing up the arguments and inventing types. You're also trying to use AS both as a seperator within a call to CAST() and as an alias operator. Since its meaning is contextual it's good practice not to mix and match in the same query.
Formatting part of your query...
cast(
count(distinct cthr) as real)
/ cast(
count(distinct s.schid) as real
)*100 as ctr
, cast(sum(status) as real)
/cast(count(*) as real)*100 as pfiled
Sorry - but this is so messed up I haven't a clue what you are trying to do here. You seem to be trying the cast the result of a calulation to a type defined by the result of another calculation. Since the can't all have the same alias I guess you must think that 'real' is a type in MySQL (it isn't). Even if you meant a floatling point number - this is just silly - the result of a count is always an integer. You don't even have the same number of opening and closing brackets.
In addition to NOT using 'AS' within CAST(), and using the valid MySQL types, if you formatted your quesry better and provided examples of inputs and outputs we might have a chance at helping you.
In MySQL using phpMyAdmin I am trying out this simple query to fetch rows that satisfy a certain date criteria:
select *
from student_invoice
where date_generated < '2012-01-01'
The date_generated is of date type. I get an error in phpMyAdmin that says:
ERROR: Unclosed quote # 64 STR: '
I have closed all quotes so its not making sense. The phpMyAdmin version is 2.11.9.6
Adding a new answer, as it's unrelated to my other one.
According to this bugzilla post here, your version suffers from this bug!
Upgrading to 2.11.11 or higher should fix this issue.
This may sound silly, but have you tried wrapping the date in double quotes?
SELECT *
FROM sometable
WHERE somedatecolumn < "2012-01-01"
Make sure that those are actually single quotes surrounding the date, not backticks.
Not familiar with the specific error. But you could try casting your static date to a date format, just to make sure it jives with the datecolumn format. Or even casting both? I.e:
where cast(somedatecolumn as DATE) < cast('2012-01-01' as DATE)
I have a feeling that won't work though. So maybe this?:
where somedatecolumn < cast('2012-01-01' as DATE)