Update birthday with full year in MySQL - mysql

I have a columns that is for birthday and it's varchar type, I want to change in into date and add full year instead for only 2-digit.
if someone born on 05061985 the MySQL remove first 0 and show as 50685
Change 50685
To ==> 05061985
All users birthday are from 1900 until 1999

Lets do that step by step
We can have strings with len 5 or 6 so we ensure we have a len 6 string left padded with zero
select LPAD('50685', 6, '0');
Now we insert the '19' in the string between the 4th and 5th position
select CONCAT(LEFT(LPAD('50685', 6, '0'), 4), '19', RIGHT(LPAD('50685', 6, '0'), 2));
Now the last step we are going to update all the BIRTHDAY fields in the table FOOBAR
update FOOBAR set BIRTHDAY=CONCAT(LEFT(LPAD(BIRTHDAY, 6, '0'), 4), '19', RIGHT(LPAD(BIRTHDAY, 6, '0'), 2));
Anyway in this case you still have a string field, I suggest to modify the format even more to do a proper date field conversion, something like YYYY-MM-DD
update FOOBAR set BIRTHDAY=LPAD(BIRTHDAY, 6, '0');
update FOOBAR set BIRTHDAY=CONCAT('19' ,
RIGHT(BIRTHDAY, 2),
'-',
SUBSTR(BIRTHDAY, 3, 2),
'-',
LEFT(BIRTHDAY, 2));
alter table FOOBAR modify BIRTHDAY date;

Related

The query returns wrong result when i want to find who is the shortest player in a NBA Database

I am working with a NBA script in MySQL and I have to find out who is the shortest player in database. I am using feet as measurement and after executing the query i found out that the player the query was giving me was not the right answer.
The query is
select * from players where height=(select min(height) from players);
And it gaves me:
'420', 'Carlos Arroyo', 'Florida International', ' 6-2', '202', 'G', 'Magic'
where 6-2 is the height.
Instead of giving me one of these results
'26', 'Brevin Knight', 'Stanford', '5-10', '170', 'G', 'Clippers'
'113', 'Nate Robinson', 'Washington', '5-9', '180', 'G', 'Knicks'
'182', 'Earl Boykins', 'Eastern michigan', '5-5', '133', 'G', 'Bobcats'
'372', 'Damon Stoudamire', 'Arizona', '5-10', '171', 'G', 'Spurs'
'482', 'Chucky Atkins', 'South Florida', '5-11', '185', 'G', 'Nuggets'
And if I order by height players, the result it's a bit annoying:
'Carlos Arroyo', ' 6-2'
'Shareef Abdur-Rahim', ' 6-9'
'Louis Amundson', ' 6-9'
'Brevin Knight', '5-10'
'Damon Stoudamire', '5-10'
'Chucky Atkins', '5-11'
'Earl Boykins', '5-5'
'Nate Robinson', '5-9'
'Aaron Brooks', '6-0'
'Allen Iverson', '6-0'
'Kyle Lowry', '6-0'
'Jammer Nelson', '6-0'
'Sebastian Telfair', '6-0'
'Chris Paul', '6-0'
Convert the height-string to a number which you can use for numeric comparison.
select player, height
from players
where cast(substring_index(height, '-', 1) as unsigned)*100+
cast(right(concat('0', substring_index(height, '-', -1)),2) as unsigned)
in (
select min(cast(substring_index(height, '-', 1) as unsigned)*100+
cast(right(concat('0', substring_index(height, '-', -1)),2) as unsigned))
from players
)
See dbfiddle
...
where 6-2 is the height. Instead of giving me one of these results
...
You tell that all values '5-xx' are equivalent to each other, i.e. only value before the dash is taken into account.
Also you tell that you need in only one output row, and any row of shown 5 rows matches - i.e. you do not need in secondary sorting.
If so then you may simply do
SELECT *
FROM players
ORDER BY CAST(height AS UNSIGNED) LIMIT 1

MySQL returns 0 results when comparing datetime column

I have a datetime column named date_time in my table. When I try to get all entries for a date before a given date, I always get 0 results even though there are thousands in the DB.
For example, these are 3 rows from my table:
INSERT INTO `fixtures` (`id`, `opta_id`, `competition_id`, `season_id`, `status`, `round`, `date_time`, `timezone`, `created_at`, `updated_at`) VALUES
(825, 'g1011331', 8, 3, 1, 14, '2018-12-02 19:30:00', 'GMT', '2019-04-03 14:39:50', '2019-05-02 13:51:49'),
(890, 'g1011405', 8, 3, 1, 21, '2019-01-26 17:00:00', 'GMT', '2019-04-03 14:39:50', '2019-05-02 13:53:06'),
(891, 'g1011396', 8, 3, 1, 21, '2019-01-26 14:00:00', 'GMT', '2019-04-03 14:39:50', '2019-05-02 13:51:49');
And this query returns 0 results:
update fixtures set status=1 where date_time < '2019-04-30 00:00:00';
What am I doing wrong?
You can try to use STR_TO_DATE let date_time to datetime then compare.
update fixtures
set status=1
where STR_TO_DATE(date_time, '%Y-%m-%d %T') < STR_TO_DATE('2019-04-30 00:00:00', '%Y-%m-%d %T');

Mysql month number to month name conversion

I have month value like "22018" in my column I need it like Feb-2018 in mysql workbench
You need to first extract the month from the date (considering it will have one or two digits), e.g.:
SELECT LPAD(SUBSTRING('22018', 1, LENGTH('22018') - 4), 2, '0');
This will give you 02. Now, you can extract the year with similar logic, e.g.:
SELECT SUBSTRING('22018', LENGTH('22018') - 4 + 1, LENGTH('22018'));
Finally, you can concatenate all these to get a string like 2018-02-01:
SELECT CONCAT(SUBSTRING('22018', LENGTH('22018') - 4 + 1, LENGTH('22018')),
'-',
LPAD(SUBSTRING('22018', 1, LENGTH('22018') - 4), 2, '0'), '-01');
Once this is done, you can use DATE_FORMAT function to get the required output:
SELECT DATE_FORMAT(CONCAT(SUBSTRING('22018', LENGTH('22018') - 4 + 1,
LENGTH('22018')),
'-',
LPAD(SUBSTRING('22018', 1, LENGTH('22018') - 4), 2, '0'), '-01'), '%M-%Y');

SSRS: How to return last day of a Custom/Financial Month (Not Calendar) through expression

This is a real hair puller so any help, much appreciated!
I want to be able to determine the:
First day of the current custom/financial month
Last day of the current custom/financial month
And use these new columns Start_Date and end_Date as between Filters in the Matrix.
Note: I understand that if this was calendar Month, then that will be "quite" straightforward.
But in this case its quite different.
Please see image which might help with the context i am trying to work with:
I'm sure this is possible using expressions in SSRS but I don;t have time to investigate. In case it's useful, here's how I would do it in SQL.
Again there is probably a more elegant solution but this was what came to me.
I'll reproduced your data plus a few more dates either end for testing which I guessed based on your sample.
DECLARE #t TABLE(Custom_Date date, Custom_Day int)
INSERT INTO #t VALUES
('2017-10-26', 28),
('2017-10-27', 29),
('2017-10-28', 30),
('2017-10-29', 1),
('2017-10-30', 2),
('2017-10-31', 3),
('2017-11-01', 4),
('2017-11-02', 5),
('2017-11-03', 6),
('2017-11-04', 7),
('2017-11-05', 8),
('2017-11-06', 9),
('2017-11-07', 10),
('2017-11-08', 11),
('2017-11-09', 12),
('2017-11-10', 13),
('2017-11-11', 14),
('2017-11-12', 15),
('2017-11-13', 16),
('2017-11-14', 17),
('2017-11-15', 18),
('2017-11-16', 19),
('2017-11-17', 20),
('2017-11-18', 21),
('2017-11-19', 22),
('2017-11-20', 23),
('2017-11-21', 24),
('2017-11-22', 25),
('2017-11-23', 26),
('2017-11-24', 27),
('2017-11-25', 28),
('2017-11-26', 1),
('2017-11-27', 2),
('2017-11-28', 3)
Then two queries to pull out the correct dates which you could combine if required.
SELECT MAX(Custom_Date) FROM #t WHERE Custom_Date < getdate() AND custom_day = 1
SELECT MAX(Custom_Date)
FROM #t
WHERE
Custom_Date > getdate()
AND DATEDIFF(d, getdate(), Custom_Date)<=31
AND Custom_Day = (
SELECT MAX(custom_day)
FROM #t
WHERE
Custom_Date > getdate()
AND datediff(d, getdate(), Custom_Date)<=31
)
FYI: This would be a lot easier if you had a custom month/period and year in your dates table as then you could just look custom_day 1 and max(custom_day) where the custom month and year are the same as the current date.

SQL: value higher than percentage of population of values

I wish to calculate the value which is higher than a percentage of the population of values, this per group.
Suppose I have:
CREATE TABLE project
(
id int,
event int,
val int
);
INSERT INTO project(id,event,val)
VALUES
(1, 11, 43),
(1, 12, 19),
(1, 13, 19),
(1, 14, 53),
(1, 15, 45),
(1, 16, 35),
(2, 21, 22),
(2, 22, 30),
(2, 23, 25),
(2, 24, 28);
I now want to calculate for each id what is the val that will be for example higher than 5%, or 30% of the val for that id.
For example, for id=1, we have the following values: 43, 19, 19, 53, 45, 35.
So the contingency table would look like this:
19 35 43 45 53
2 1 1 1 1
and the val=20 (higher than 19) would be chosen to be higher than 5% (actuall 2 out of 6) of the rows.
The contengency table for id 2 is:
22 25 28 30
1 1 1 1
My expected out is:
id val_5p_coverage val_50p_coverage
1 20 36
2 23 26
val_5p_coverage is the value val needed to be above at least 5% of val in the id.
val_50p_coverage is the value val needed to be above at least 50% of val in the id.
How can I calculate this with SQL ?
I managed to do it in HiveQL (for Hadoop) as follows:
create table prep as
select *,
CUME_DIST() OVER(PARTITION BY id ORDER BY val ASC) as proportion_val_equal_or_lower
from project
SELECT id,
MIN(IF(proportion_val_equal_or_lower>=0.05, val, NULL)) AS val_5p_coverage,
MIN(IF(proportion_val_equal_or_lower>=0.50, val, NULL)) AS val_50p_coverage
FROM prep
GROUP BY id
Although this is not MySQL nor SQL per se, it might help to do it in MySQL or SQL.