How can I convert this to DATETIME SQL? - mysql

Trying to convert this value to DATETIME and unsuccessful so far. Thoughts and ideas are welcome.
03MAR2020:02:45:58.977000

You can use the function STR_TO_DATE() with the proper format:
select str_to_date('03MAR2020:02:45:58.977000', '%d%b%Y:%k:%i:%s.%f')
See the demo.
Result:
2020-03-03 02:45:58.977000

in SQL Server
03MAR2020:02:45:58.977000 is failing to convert tot datetime because, first there is an extra : sign after the year and the fractional seconds is 6 digits instead of max 3.
If the string is always the same length and the same format, then you can use the following to solve this problem:
DECLARE #example varchar(25) = '03MAR2020:02:45:58.977000'
SET #example = LEFT(#example, 22) --- remove last 3 zeros
SET #example = STUFF(#example, CHARINDEX(':', #example), LEN(':'), ' ') ---repalce the first ':'
SELECT convert(datetime, #example) --- convert to datetime
More details: Here are the parts of datetime data type in SQL Server.
YYYY is four digits from 1753 through 9999 that represent a year.
MM is two digits, ranging from 01 to 12, that represent a month in the specified year.
DD is two digits, ranging from 01 to 31 depending on the month, that represent a day of the specified month.
hh is two digits, ranging from 00 to 23, that represent the hour.
mm is two digits, ranging from 00 to 59, that represent the minute.
ss is two digits, ranging from 00 to 59, that represent the second.
n* is zero to three digits, ranging from 0 to 999, that represent the fractional seconds.
Taken from the SQL server documentation
https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql?view=sql-server-ver15

Related

sql incorrect date output incorrect

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.

How do you round floats conditionally?

I am writing a query that is used by report generating software.
Part of this is querying for the hours needed to complete a project. We record this a 2 decimal float so that we can estimate to the quarter hour.
However, if we are using it in our report and the hour we recorded is something like 8.00, I want to query it and format it so that 8.00 is just 8. However any hours with something past the decimal, like 8.25, should remain as 8.25. How can I make this work?
hours Queried Result
====== -> My Query -> ==============
8.00 8
8.25 8.25
I am using MySQL 5.6
You can use the REPLACE() function to remove .00:
REPLACE(hours, '.00', '') AS hours
You can convert it to a string and check the rightmost 2 characters and trim those if they are '00'.
SELECT TRIM(TRAILING '.00' FROM CAST(column_name AS VARCHAR));
SELECT REPLACE(Round(8.00), '.00', ' ');
I will give more example so you can clear your Logic:
MySQL ROUND() rounds a number specified as an argument up to a number specified as another argument.
Syntax:
ROUND(N,[D]);
Where 'N' is rounded up to D decimal places.
and 'D' is indicating up to how many decimal places N will be rounded.
Example 1:-
SELECT ROUND(4.43);
Output :-
4
The above MySQL statement will round the given number 4.43. No decimal places have been defined, so the default decimal value is 0.
Example 2:-
SELECT ROUND(-4.53);
Output:-
-5
The above MySQL statement will round the given number -4.53. No decimal places have been defined, so the default decimal value is 0.

mysql convert bigint to datetime

The CreateDateTime column in a table is in the format bigint(20). For example it looks like: 131037078373067074. I didn't create this table, it is passed on to me.
Using the example here Convert BigInt timestamp to a real Date with row aggregation and operations in mySQL, I have tried dividing CreateDateTime by POW(10,8) or POW(10,9) .
SELECT FROM_UNIXTIME(CreateDateTime/POW(10,8)) AS due_date
FROM images;
results look like '2011-07-11 02:53:03.730671'.
SELECT FROM_UNIXTIME(CreateDateTime/POW(10,9)) AS due_date
FROM images;
results look like '1974-02-25 09:11:18.373067'.
I am expecting a date in 2016. However dividing CreateDateTime by POW(10,7) gives null.
What should I do?
I am expecting a date in 2016.
Dividing by 89000000 gives a date in 2016
(To discover that I converted a date in the middle of 2016 to seconds since unix epoch and divided your number by that).
What should I do?
If you have some way to get the real datetime (like from an existing and known working user interface) you can probably reverse engineer the formula. Take several samples and keep track of the number in CreateDateTime and the correct date time. The number may represent microseconds or nanoseconds since a different epoch. Unix uses 1970-01-01 UTC, Excel uses Jan 0, 1900 (no, not Jan 1 but Jan 1 - 24 hours), NTP uses Jan 1 1900, IBM BIOS (and ZIP) uses Jan 1, 1980.
The number of "fractional seconds" in one second may be a multiple of 60 instead of 10.
Use the data you gather from the user interface to solve for
(CreateDateTime + a) / b = UnixSecondsSince1970_01_01
OR
(CreateDateTime / b) + a = UnixSecondsSince1970_01_01
where "a" is the offset needed to adjust for non-unix epoch and "b" is the number of units needed to add up to one second in the "CreateDateTime" representation.
Others epoch choices referenced here:
https://en.wikipedia.org/wiki/Epoch_(computing)#Notable_epoch_dates_in_computing
( 131037078373067074 / 10000000000 + ( 24 * 60 * 60 * 17000 ) )
Also gives a date in 2016

Column type for saving very different number of decimals

I need to store numbers like
21000
1.0002
0.00230235
12323235
0.2349523
This is sensordata so it is important to keep the exact value.
THere are many options.
My solution would be to multiply all values by 1 million, and store them as a bigint. Would that make sense?
That makes sense but I'd recommend that you just use the decimal datatype: https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html
If you were to multiply by million and if a dataset you receive has one more decimal than you'd expect, you'd end up multiplying that number by 10 million and all other numbers by 10. Instead, using the decimal datatype will give you 30 numbers to the right of the decimal.
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The
ranges of values for the arguments in MySQL 5.7 are as follows:
M is the maximum number of digits (the precision). It has a range of 1
to 65.
D is the number of digits to the right of the decimal point (the
scale). It has a range of 0 to 30 and must be no larger than M.
and
The SQL standard requires that the precision of NUMERIC(M,D) be
exactly M digits. For DECIMAL(M,D), the standard requires a precision
of at least M digits but permits more. In MySQL, DECIMAL(M,D) and
NUMERIC(M,D) are the same, and both have a precision of exactly M
digits.
For a full explanation of the internal format of DECIMAL values, see
the file strings/decimal.c in a MySQL source distribution. The format
is explained (with an example) in the decimal2bin() function.
To format your numbers, you could do formatting like this answer describes: Format number to 2 decimal places
Example
create table test (
price decimal(40,20)
);
-- all the above insertions will succeed cleanly
insert into test values (1.5), (1.66), (1.777), (1.12345678901234567890);
-- notice we have 21 digits after decimal
-- MySQL will insert data with 20 decimal and add a warning regarding data truncation
insert into test values (1.123456789012345678901);
Data
select * from test
price
1.50000000000000000000
1.66000000000000000000
1.77700000000000000000
1.12345678901234567890
1.12345678901234567890
select cast(price as decimal(40,2)) from test
price
1.50
1.66
1.78
1.12
1.12

Load numbers in MySQL field

I have a MySQL table with some Decimal(4,2) fields.
I have a CSV file with numbers like 1485.23 to load in those fields.
After loading the file, the values in those fields is 99.99, like a max value to put inside.
Just alter your table in order to receive decimals more than 4 digits in total.
DECIMAL(4,2) means a number with 4 digits in total, 2 of them after floating point, so 99.99 will be maximum allowed.
DECIMAL(9,3) will receive numbers with 9 digits in total, 3 of them after floating point.
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.7 are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65.
D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.
The maximum value of 65 for M means that calculations on DECIMAL values are accurate up to 65 digits. This limit of 65 digits of precision also applies to exact-value numeric literals, so the maximum range of such literals differs from before.
Example
In a DECIMAL column declaration, the precision and scale can be (and usually is) specified; for example:
salary DECIMAL(5,2)
In this example, 5 is the precision and 2 is the scale. The precision represents the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored following the decimal point.
Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99.
for more info https://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html