How can I separate a single column into 3 separate columns - mysql

Want to execute a query to view single date-month-year time column to separate date column, month column and year column.
eg
joining_date
01-JAN-22 12.00.00AM
to
joining_date|joining_month|joining_year
01 | JAN | 22

You have some ways of doing this:
If your data is always in this 01-JAN-22 12.00.00AM format , no matter what comes after 22, you can use substring.
select substring('01-JAN-22 12.00.00AM',1,2) as joining_date,
substring('01-JAN-22 12.00.00AM',4,3) as joining_month,
substring('01-JAN-22 12.00.00AM',8,2) as joining_year;
Result:
joining_date joining_month joining_year
01 JAN 22
Another option is converting the string to proper date datatype an use MySQL functions, like :
select DAY(str_to_date('01-JAN-22 12.00.00AM', "%d-%b-%y")) as joining_date,
MONTH(str_to_date('01-JAN-22 12.00.00AM', "%d-%b-%y")) as joining_month,
YEAR(str_to_date('01-JAN-22 12.00.00AM', "%d-%b-%y")) as joining_year ;
Result:
joining_date joining_month joining_year
1 1 2022
Fiddle

Use YEAR, MONTH and DAY syntax:
SELECT
YEAR(`joining_date`) as joiningYear,
MONTH(`joining_date`) as joiningMonth,
DAY(`joining_date`) as joiningDay
FROM tableName
If you want your month name, then use MONTHNAME:
SELECT
YEAR(`joining_date`) as joiningYear,
MONTHNAME(`joining_date`) as joiningMonth,
DAY(`joining_date`) as joiningDay
FROM tableName

Related

How to search SQL records by date of birth?

I have a SQL-table where I have records of my clients such as first name, last name, date of birth etc.
Now I try to find all clients by date of birth (for cron, sending holiday and birthday greetings).
Therefore, I need to find all existing clients with a specific date of birth.
For example:
In the SQL table I have a few records with these birth dates.
+==================+
| Date_of_birthday |
====================
| 1981-06-30 |
--------------------
| 1972-06-30 |
--------------------
| 1966-10-07 |
====================
Now I need to return all clients who were born on June 30th.
I tried:
SELECT * FROM `table`
WHERE DATE(Date_of_birthday) = '####-06-30';
But my pattern does not seem to work properly, it doesn't return any lines.
The hashtag character '#' should represent any numeric character, but it doesn't work.
Where is my mistake? Or do I have to write the SQL query differently?
With DATE_FORMAT():
SELECT * FROM `table`
WHERE DATE_FORMAT(Date_of_birthday,'%m-%d') = '06-30';
This query should do:
select *
from YourTable
where datepart(day, date_birth) = 30
and datepart(month, date_birth) = 06
SELECT * FROM `table`
WHERE right(Date_of_birthday, 5) = '06-30'

MySQL query to get that row which has the last day of first month of date field

In my database I have a field named DateLastSaved:
Suppose the values are:
1. 2016-05-12 08:07:00,
2. 2016-05-22 09:06:00,
3. 2016-05-22 09:06:00,
4. 2016-06-13 09:00:00,
5. 2016-06-13 09:00:00
I wan't such query that would return me that row whose DateLastSaved field has the minimum month, in above case "5" and the maximum date of that month, which is 2, 3, but my query should return one result, i.e either 2 or 3.
I am using the following query:
SELECT MIN(LAST_DAY(DateLastSaved))FirstMonth
FROM InitialLog
WHERE FileName='Dr. Adam Kotowski Patient Names.doc'
But it is returning me the first date, that is, minimum, not the maximum one. Any suggestions?
Try this:
SELECT *
FROM InitialLog
WHERE MONTH(DateLastSaved) = (SELECT MIN(MONTH(DateLastSaved)) FROM InitialLog)
ORDER BY DAY(DateLastSaved) DESC LIMIT 1
Demo here

Inserting Custom Auto-increment Field

I need a query to insert an auto-increment field in the following format:
150001 (15 is last two digit of year 2015 and an id which increments like 0001, 0002, 0003 etc)
Till year 2016 March it need to show as 2015 only (15) after March it should change to 2016 (16). Because that's when our financial year ends. Is it possible to achieve the same with a query:
150001
150002
160001 etc
It can be something like that(first assumption):
SELECT CONCAT(
IF(DATE_FORMAT(NOW(), '%m')<3,
DATE_FORMAT(t.created, '%y')-1,
DATE_FORMAT(t.created, '%y')),
LPAD(
id + 1 - (
SELECT MIN(id) FROM Tbl t2 WHERE
IF(DATE_FORMAT(NOW(), '%m')<3,
DATE_FORMAT(t2.created, '%y')-1,
DATE_FORMAT(t2.created, '%y')) =
IF(DATE_FORMAT(NOW(), '%m')<3,
DATE_FORMAT(t.created, '%y')-1,
DATE_FORMAT(t.created, '%y'))
),
4,
'0') super_id
FROM Tbl t;
Here's the trick...
-->> 1st: Get the last 2 digit of the year
SELECT RIGHT(YEAR(NOW()),2);
-->> 2nd: Pad 4 digit zeros
SELECT RIGHT(CONCAT('0000',1),4);
-->> 3rd: Concat the two query above, assuming that the column name is `ID_Column`
SELECT CONCAT(RIGHT(YEAR(NOW()),2), RIGHT(CONCAT('0000',ID_Column),4));
You can now insert the result of the 3rd query into your id.
Note: if your id is integer, you have to convert it into varchar
sample:
-->> Concat the two query above, replacing the column name value as 1
SELECT CONCAT(RIGHT(YEAR(NOW()),2), RIGHT(CONCAT('0000',1),4));
result: 150001

MySQL Select data and manipulate

I have a table whose content like this :
If I use :
SELECT
DATE_FORMAT(tgl_kecelakaan,'%M') AS NamaBulan,
COUNT(DATE_FORMAT(tgl_kecelakaan,'%M')) AS total
FROM
tbl_kecelakaan_kapal
WHERE
DATE_FORMAT(tgl_kecelakaan,'%M')='January'
The result is : January 2
But how to keep my query produces output like this (count all data) :
January 2
February 1
Without should I include WHERE condition as above ??
Thanks
Try GROUP BY:
SELECT
DATE_FORMAT(tgl_kecelakaan,'%M') AS NamaBulan,
COUNT(1) AS total
FROM
tbl_kecelakaan_kapal
GROUP BY NamaBulan # or DATE_FORMAT(tgl_kecelakaan,'%M')

Cannot use REPLACE function properly in MySQL

I have an SQL table whose columns are State_Code, School_Code and School_Type.
Sample Data:
State_Code School_Code School_Type
01 01014874 1
01 01018790 2
1 10189900 1
1 10277689 1
*(Note: 1st and 2nd digit of School_Code is the actual state-code while its 3rd and 4th digit is the District_Code of that particular state)*
Now, if I fire this query:
Select COUNT(School_Code) from Table1 where State_Code='1'+0;
It will return the number of schools present in the state, with code '1'; i.e. 4;
But if I want to retrieve the number of schools present in a state DISTRICT-WISE; I would fire this query:
Select State_Code,SUBSTRING(School_Code,3,2) AS District_Code,COUNT(School_Code) AS Number_of_Schools_In_District FROM Table1 GROUP BY State_Code+0;
This query returns me this output:
State_Code District_Code Number_of_Schools_In_District
01 01 2
1 18 1
1 27 1
But the correct output is:
State_Code District_Code Number_of_Schools_In_District
01 01 3
1 02 1
This is due to the incorrect data entered by user in SQL table for column School_Code. State '1' and '01' are actually the same state i.e. '01'. But since School-Code in the 3rd and 4th row does not start with '0', this lead to the incorrect behavior in the output.
So to resolve this problem, I will have to use this logic:
*If number of characters in State_Code is 1 and School_Code does not start with '0', then a '0' should be prefixed to the School_Code value to get the correct District_Code value.*
I tried this:
Select State_Code,SUBSTRING(School_Code,3,2) AS District_Code,COUNT(School_Code) AS Number_of_Schools_In_District FROM Table1 WHERE IF CHARACTER_LENGTH(State_Code)=1 AND School_Code NOT LIKE '0%' THEN REPLACE(School_Code,School_Code,CONCAT(0,School_Code)) END IF GROUP BY State_Code+0, SUBSTRING(School_Code,3,2)+0;
My expectation was if any value in State_code is a single digit and its corresponding School Code does not start with '0', then this will append '0' to it and then calculate district code taking the 3rd and 4th digit. But this does not happen. Still I get the error.
Note: I don't want to update original SQL table values.
Any help will be appreciated. Thanks in Advance!
Try below query
Select State_Code, IF(CHARACTER_LENGTH(State_Code)=1, SUBSTRING(School_Code,2,2), SUBSTRING(School_Code,3,2)) AS District_Code,COUNT(School_Code) AS Number_of_Schools_In_District FROM Table1 WHERE GROUP BY State_Code+0, SUBSTRING(School_Code,3,2)+0;