sql remove seconds and milliseconds - mysql

I have 2 problems (similar to each other)
Problem 1:
I have a column in my table named dateofstart.
2017-09-13 09:55:02.000
2017-09-03 09:33:12.000
I need to convert it to
2017-09-13 09:55
2017-09-03 09:33
The problem 2 is the date format is a little bit different.(another table/column)
2017-9-13 09:55:02.000
2017-9-3 09:33:12.000
how can I convert those to without seconds and ms
I have tried some related questions on SO. but failed
some of them removed last numbers or allowed certain chars (the problem is I have 2 different formats)
There is a reason why they use different formats(something with the application) but now I have a problem where I have 2 find an answer that will work for both of them

probably this can help you i guess.
Write this query in the inner query,
SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM tablename;

Related

Sum columns with similar names in SQL

Relatively new to SQL and want to shorten a query I’m using.
The goal is to add the total spent in one year and compare it to the next year. However, the column names are all formatted “Spend_YYYYMM” so “Spend_202102.”
Currently, my solution is just to add all 12 columns up:
SELECT
“Full_Name”,
(“Spend_202001”+”Spend_202002”...) AS “2020 Total”,
(“Spend_201901”+”Spend_201902”...) AS “2019 Total”
FROM “Customers”
WHERE “2019 Total” > “2020 Total”;
So is there a way to look for columns where it starts with “Spend_2019” and add them up without having to type all 12 columns out? Or is what I have the only way we can really do this?
(Sorry for all the superfluous quotes, it’s apparently how our DB works with SQL.)
Thank you for your help!!
First, do not use identifiers that need to be escaped.
Second, your data model is weak. You should have separate rows for the different years.
But, the answer to your question is a MySQL extension of the HAVING clause:
SELECT Full_Name,
(Spend_202001 + Spend_202002 ...) AS Total_2020,
(Spend_201901 + Spend_201902 ...) AS Total_2019
FROM Customers
HAVING Total_2019 > Total_2020 ;

Trying to convert inconsistently formatted price strings to cents

I'm migrating data over from one database to another, and am writing the appropriate scripts. I want to start on a clean slate and fix a lot of the inconsistent formatting allowed by the previous app, specifically with prices, the following all being examples of prices currently stored in the previous database:
-100.00
700.00
0.01
3,200.00
3200
1,750.5
0
500/hour
I would like to convert everything into cents, so the above would be:
-10000
70000
1
320000
320000
175050
0
50000
I was hopeful when FORMAT(price, 2) * 100 seemed to work on a lot of them, including (!) 500/hour:
select format('500/hour', 2) * 100;
-> 50000
But for some reason, I'm getting weird results for 3200.00:
select format('3200.00', 2) * 100;
-> 300
While writing this, It appears that it doesn't work for any numeric strings above 1,000, and I'm guessing it has something to do with the presence/lack of a comma. Is there any intelligent way to parse the above examples into cents? If it is simple enough, I'd love to just incorporate it into the select query, but a user defined function is also fine.
You could first remove the comma with this sentence:
replace(data,',','')
This could be because you have a varchar type instead of an int type. If you do
select cast(replace('3,200.00',',','') as signed) * 100
It should work

what is wrong with this query using str_to-date function?

SELECT STR_TO_DATE(SUBSTRING_INDEX(`REPORTDATETIME`,' ',1),'%m/%d/%y')
FROM crimes
where REPORTDATETIME like '%1/12/2001%'
it is the query which iam using
reportdatetime(varchar) is the column name of table
reportdatetime
1/12/2001 1:30
12/23/2003 1:09
11/12/2001 1:30
5/23/2003 1:09
the result which query gives
2020-1-12
but the result iam expecting is 2001-1-12
Your STR_TO_DATE format should be '%m/%d/%Y' for a four-digit year.
Other peripheral issues to note... I would expect your WHERE clause to catch two different rows (always best to avoid using a leading '%' in LIKE whenever you can). Also, a time of 1:90 looks very strange.

Mysql Like characters [a-z] or Zero

In a mysql table i have column whit this info..
Col.
tr10
tr210
zbr10
00010
10010
tr 10
The question is simple, i need to find in a mysql query all the records number 10.. but as you can see in the example not 10010 etc..
Result:
tr10
zbr10
00010
tr 10
I know is a mess but the records had to be load in that form..
so you have characters at the begining, in some cases spaces, or zeros..
An option could be extract (by hand) hundred of characters to another column to keep the things less complex, but at the same time i still having problems with the 000010 values..
Use regular expressions
select * from table where col regexp '^[a-z]+10$'
Play with the regex until you get your desired results, i didnt fully understand you criteria so I just made one up but the one in my example will pull all the rows with any alpha characters proceeded by 10

datetime difference shows wrong values in mysql

In my website I show some data according to the date it added to the database. It will place a tag "NEW" to the product which added with in last 7 days. The code works perfectly till today. Now it shows wrong values. I am using the below code to get the difference
DATE(stored_date_time) - DATE_SUB(CURDATE(), INTERVAL 7 DAY) AS days
and the output of this code is 77. But today date is 2014-07-01
while echoing the value of DATE(stored_date_time) it gives the output
2014-07-01
and echoing of DATE_SUB(CURDATE(), INTERVAL 7 DAY) gives the output
2014-06-24
I cant find what is wrong with my code. Please help..
Arun,
Since you asked what is wrong with your code.
select date1 - date2 as days;
It is not actually meaningful at all. The values of dates in number format
are just formed as "yyyymmddhhmmss". So it contains the same information
as the string, but instead of using characters, each digit is actually
an integer. That format might be useful for someone, but you can't use
that format to calculate differences.
Basically what i am trying to say is, if the query above is run for say
'2014-07-02' and '2014-06-22' you'll get 80 as result. Which is in fact
20140702-20140622 =80.
So like you have already been adviced on the other post, use the datediff() function.
Instead of subtracting those date, it would be better if you use DATEDIFF, like below:
SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate
which will return 61.. for more info func_datediff
Try with DATEDIFF function.
SELECT DATEDIFF(DATE(stored_date_time),DATE_SUB(CURDATE(), INTERVAL 7 DAY)) AS days
Just encountered similar issue. I was using CAST((1000000 * (moment2 - moment1)) as UNSIGNED), where moment1 and moment2 are DATETIME(6). And it was working perfectly until once I got enormous value for one of thousands values. What is ridiculous, why it was working all the time, and only one pair of values gave invalid result. Now changed request to TIMESTAMPDIFF(MICROSECOND, moment1, moment2), it works OK.