Convert double timeformat with php MSSQL [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have here a problem to convert a time who is double with php. I´ve never seen it before.
Is this the timeformat from excel?
Is it possible to do it with the mssql server where the data is stored in?
Here are some time examples.
0.291666666666667 = 07:00:00
0.229166666666667 = 05:30:00
0.320833333333333 = 07:42:00

Guessing those are times? Use CONVERT:
WITH VTE AS(
SELECT *
FROM (VALUES(0.291666666666667),(0.229166666666667),(0.320833333333333))V(T))
SELECT T, CONVERT(time(0), CONVERT(datetime, t)) AS TimeValue
FROM VTE;
(2 conversions, as you can't convert straight from a decimal to a time)
Edit: Datetime values in Excel (and sql server) are actually stored as numerics. The value 0 for a datetime in SQL Server being 19000101 (if I recall correctly, 0 is 19000100 in Excel/Access and 1 is 19000101; just to make things a little confusing). Each full integer adds an extra day to the datetime 0. Thus a value of 1 is 19000102 and 5 would be 19000106, etc, etc.
The same is true for the time portion, however, these are stored as the decimal part. Thus, 0.5 represents 12 hours. Thus, the value 0.5 represents 19000101 12:00:00.000. 6.75 would be 19000107 18:00:00.000. With Excel and Access, they also stores times as a decimal (between 0 and 1). Hence, why the above works, as it firstly converts the decimal to a datetime (for the first value 19000101 07:00:00.000) and then to a time(0) (0 represents 0 points of millisecond accuracy); resulting in 07:00:00.

Related

How should I correct ill-formatted lat-long pairs to be valid for PHP computation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have access to a dataset that contains latitude and longitude pairs, but they are ill formatted and are not properly represented in the data set.
For example, a lat-long pair might look like this: 31333445, 105530865 when it should be 31.333445, -105.530865. Given the data set I am working with I know that the min value for latitude is 31.0 and the max is 37.0, and the min/max of longitude is -103 to -109.
If I was given a piece of paper and a pencil I could easily correct these myself, but the correction needs to happen on the fly when we receive input from a different program. We have no control over how the data is formatted until it hits our system and then we can make changes and corrections, and the lat-long pairs are all in a integer format listed above rather than a float.
What would be the best way to go about manually correcting this error? I am using PHP for our processing system.
If they're the same length then just divide by 1000000 and make negative where needed:
echo $lat / 1000000;
echo -$lon / 1000000;
If not then get the number of numbers at the start (2 and 3 here) making negative if needed, then insert a decimal and the remaining:
echo substr($lat, 0, 2) . '.' . substr($lat, 2);
echo -substr($lon, 0, 3) . '.' . substr($lon, 3);
You can use floatval() on the results if needed.
If the number of digits is always the same fixed size then use the solution suggested by #AbraCadaver in his comment ( just divide by 1000000 and multiply with -1)
If the number of digits can be different you need a different solution
and I have got a weird idea (at 0:24am)
I would convert the number to a string resulting in "31333445"
then concatenate "0." with the "31333445" resulting in "0.31333445"
then convert it back to a double resulting in 0.31333445
and then multiply it with 100 resulting in 31.333445 (and multiply the other value with -1 )
:-B
With this solution it does not matter if the number you get from outside has 3 or 14 digits
May sound weird but should work.
If this sounds to be a useful solution i will put into code tomorrow.

MySQL mod vs PHP mod [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Edited
Sorry guys I want PHP mod to display decimals
In MySQL select mod(15.6,1) result 0.6 but in PHP 15.6 % 1 result 0?
My purpose is to restrict calculation only for exact integer which does not allow decimals
In PHP, the % modulus operator works on integers. The operands 15.6 and 1 are cast to int (15 and 1) before being worked on. Therefore, 15 % 1, as you may expect, gives 0. Therefore "exact integer which does not allow decimals" matches the behaviour in PHP.
In MySQL, as the documentation says:
MOD() also works on values that have a fractional part and returns the exact remainder after division
Therefore, you should TRUNCATE(operand, 0) the operands before using them in MySQL to obtain the integer behaviour. This is consistent with PHP's behaviour of casting to int -- numbers are truncated, not floored (which is a significant difference with negative numbers).

PHP mysql query taking too long for using like to timestamps [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using this query :
select field from table1 where time LIKE '%2016-03%' order by time asc limit 1
My problem is that the table has thousands of rows and so it takes too long to search for 1 row. Is it normal?
LIKE queries are always going to be slower than looking for a specific value.
It'll help immensely to add an INDEX on the field and change your query to LIKE '2016-03%' (there won't ever be anything before the year in a timestamp so drop that first %). It'll be able to take a couple shortcuts, at least.
If you use LIKE and starts with % MySQL must make a FULL TABLE SCAN to find the correct Dates, but if you start direct with the year they can use a index : ... KIKE '2016-03-%'
Try adding an INDEX to your time column and as other have pointed out, remove the leading % from the query.
See this post for more info

Cannot get number to increment above 10 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Ok, so a strange one that I've never seen before. The bit of script you are about to see gets a value from a table, explodes it and then increments the number stored in the array, so I can then use it to create a unique product code. However, the number refuses to increment once it hits 10. Here is the script:
$query = $this->mysqli_link->query("SELECT custom_id FROM sjs_custom_orders ORDER BY custom_id DESC LIMIT 1")->fetch_array();
$custom_id_old = $query['custom_id'];
$custom_id_new_split = explode("/", $custom_id_old);
$custom_id = $custom_id_new_split[2] + 1;
$custom_id_new = "SJS/CUSTOM/" . $custom_id;
The table itself has the custom_id column set as a varchar(100).
Any help appreciated.
The problem is in your query.
ORDER BY custom_id DESC
Because the (unfortunately named) custom_id column is character type, this value:
SJS/CUSTOM/9
will sort before (in descending order) this value:
SJS/CUSTOM/10
because the comparison is character to character. The '9' is greater than '1'. As humans, we may perceive ten to be greater than nine. But MySQL is just comparing character to character. If we added a '0' character before the '9', so the database was comparing '09' to '10', we'd be closer to getting the result we expect.
If the value of custom_id always contains two forward slashes, and you want the numeric value after the last forward slash, you can parse that out with a SQL expression
SUBSTRING_INDEX(custom_id,'/',-1)+0
(Add a zero converts the string to numeric value. MySQL is wonky the way it does that, it won't throw an error, it converts the leading portion, as much as it can, into a numeric.
If there's always two slashes, you can get the part before that second slash with an expression as well:
SUBSTRING_INDEX(custom_id,'/',2)
To get the "maximum" value, you could do this...
ORDER
BY SUBSTRING_INDEX(custom_id,'/',2) DESC
, SUBSTRING_INDEX(custom_id,'/',-1)+0 DESC
LIMIT 1
NOTE: This "works" as long as custom_id always contains two forward slashes. If there's any values of custom_id that aren't like that, the behavior of these expressions is well defined, but unlikely to return the result you want.
MySQL will not be able to make effective use of an index to satisfy this query; those expressions will need to be evaluated for every row in the table. If you're stuck with this unfortunate design, then you're stuck with it. We'd prefer to see the results from these expressions stored separately, in two separate columns.

data type used to store discount in sql php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to create a table for product management in that table field include like price , image , discount so any one can help me that which datatype is used for all of them.
It isn't a simple question as it seems at first look. I suggest you to read first how floats is implemented in different databases and programming languages. It will help you understand why you never shoud use floats to sotre money.
This article Always use decimal for money may help you.
Well you just have to ask yourself what the data is.
If its words of fixed length its CHAR(n) ( such as a SKU number), VARCHAR(n) is variable length up to n, TEXT is for things longer such as a description.
If it's money, like a price, it's a float, double, or decimal in the database ( they are all essentially the same thing, depends on your DB ). If it's a whole number it's an integer. For an image, if it's the link ( the src ) then use a VarChar, but for image data itself its a BLOB.
Words "hello" = strings, varchar, char, text
Numbers "1" = integers
floats "1.01" = float, double, decimal etc.
binary data - such as images its called a BLOB or Binary Large Object
lists such as a status, "paid, due, late" you could use a ENUM type field.
Here is some reading meterial on types in MySql.
https://dev.mysql.com/doc/refman/5.0/en/data-type-overview.html

Categories