Using CONCAT in combination with SUBSTR - sql

I want to write a select that would return me a distinct years only (2018, 2017,2016).
I have column AS_OF_DATE in table HISTORY.
Here are some example values of AS_OF_DATE:
31-05-18,
31-04-17,
31-07-16,
...
This is what I tried, but it doesn't work:
SELECT CONCAT('20',DISTINCT SUBSTR(AS_OF_DATE, 7, 2) FROM HISTORY
I used CONCAT to add 20 in front of the result and SUBSTR that would start at the 7th string and would be 2 strings long (so I get 18,17,16...)

try like below
SELECT DISTINCT '20' || SUBSTR(AS_OF_DATE, 7, 2) FROM HISTORY

Normally, you would do this with extract() or to_char():
select extract(year from as_of_date) as yyyy
or
select to_char(as_of_date, 'YYYY') as yyyy
This assumes that as_of_date is a date, which is should be.
You can add select distinct if you want a result set with the distinct years.

You can use
SELECT CONCAT('20', SUBSTR(AS_OF_DATE, -2) ) as "Years"
FROM HISTORY
GROUP BY SUBSTR(AS_OF_DATE, -2)
ORDER BY "Years"
Demo

this will work:
select distinct extract(year from as_of_date) from History;
the extract function takes off the as_of_date provided it is a date datatype and distinct select only one for dates which are multiple times in the extract.

Related

Extracting data from only the year

I have data in a table in SQL with dates, but how do I select only those that happen in 2021. (The dates look like 31-oct-2020) in the table. The dates are the actual date variable, not just text.
You should avoid storing your dates as text, but rather should use a proper date column. That being said, you may check the right 4 characters of the date string:
SELECT *
FROM yourTable
WHERE RIGHT(date_col, 4) = '2021';
If the column be an actual date type then use:
SELECT *
FROM yourTable
WHERE date_col >= '2021-01-01' AND date_col < '2022-01-01';
I suspect that your DB is Oracle after checking out your previous post. Then you can use
SELECT *
FROM yourTable
WHERE EXTRACT(year FROM dt) = 2021
or
SELECT *
FROM yourTable
WHERE TRUNC(dt,'YYYY') = date'2021-01-01'
or
SELECT *
FROM yourTable
WHERE dt BETWEEN date'2021-01-01' AND date'2021-12-31'
You can benefit the index if there's one on the date column(namely dt) by using the last SELECT statement
If using MSSQL, you can leverage the YEAR(...) to extract the year from a date.
Replace and , with the table name and date column name respectively.
select * from <tablename> where year(<datecolumn>) = 2021

How to filter date with where on SQLite with '2021-07-31 13:53:26' format?

I wanted to take just the year and month from '2021-07-31 13:53:26' and group them based on count values.
i tried the date, datetime, strftime functions.
Date and Datetime resulting null. strftime result something, but i cant group the Year and Month i get with the count i want, resulting null again
Here is the preview of the data.
expected result example is like '2021-07' with the count of how many times this year and month occurs
This is the syntax i tried with strftime:
select strftime('%Y%m', started_at) year_month, count(year_month) from bike_trip
group by year_month
Thank You
Sqlite doesn't have a date data type so you will need to do string comparison to achieve this.
with d as (
select '2021-07-31 13:53:26' as d, 'A' val union all
select '2021-08-30 13:53:26' as d, 'B' val
)
select substr(d,1,4) as yyyy, substr(d,6,2) as mm, count(*)
from d
group by substr(d,1,4), substr(d,6,2)
in your query:
select substr(started_at,1,4) as yyyy, substr(started_at,6,2) as mm, count(*)
from bike_trip
group by substr(started_at,1,4), substr(started_at,6,2)
Use a CTE to get your answer.
with
-- uncomment to test
/*bike_trip(started_at) as (
values
('2021-07-31 13:53:26'),
('2021-07-17 19:06:01'),
('2021-08-30 13:53:26')
),*/
bike_months(year_month) as (
select strftime('%Y-%m', started_at) year_month from bike_trip
)
select year_month, count(year_month) count_year_month from bike_months
group by year_month;
Output:
year_month|count_year_month
2021-07|2
2021-08|1

SQL query for multiple values of a column

I have db with names etc with date of birth. How can I get count of columns for all 12 months of the dates?
Exact code depends on the database you use; you should, somehow, "extract" month from date of birth in order to GROUP BY it.
In Oracle, you might have done it as
select to_char(date_of_birth), 'mon') dob_month,
count(*)
from your_table
group by to_char(date_of_birth, 'mon');
or
select extract(month from date_of_birth) dob_month,
count(*)
from your_table
group by extract(month from date_of_birth);

DB2 SQL extract only month and year from date column

I trying to group by month and year from MGMOV00F table - date column is DTMOMM, trying the syntax below but still getting error: argument 1 of function year not valid, anybody help please ?
Are you sure your dealing with an actual date data type?
Given the WHERE DT01MM > '20160101'
It would appear to me you're dealing with character columns; since AFAIK DB2 won't implicitly cast '20160101' to date like it would '2016-01-01'.
That being the case, your task is easy. Simply substring the first 6 characters..
Selects count of month-year (m-yyyy) ordered by number of matches:
SELECT valid_from, COUNT(1) FROM (
SELECT (
EXTRACT(MONTH FROM valid_from)
CONCAT '-' CONCAT
EXTRACT(YEAR FROM valid_from)
) AS valid_from
FROM some_table NOLOCK
) as s
GROUP BY s.valid_from
ORDER BY 2 DESC;
Result:

Oracle date extract and concatenate

I have to take a date (dd-mon-yyyy) and change the year based on a user input. How do I extract the day and month and add the year while keeping the format?
For example, it is currently 01-JAN-2015, and the user wants it to be 01-JAN-2016. I currently am doing:
SELECT EXTRACT(DAY FROM contract_year) || EXTRACT(MONTH FROM contract_year) || '2016'
FROM table
WHERE program = programid
And my output is 112016, but I want 01-JAN-2016. Any suggestions?
Try this
SELECT to_char(add_months('01-JAN-2015', 12*1),'dd-MON-yyyy') -- replace 1 with n where n = years
from dual
If you want to add use given year to current date/month, use this
with your_table as(
select '01-JAN-2015' as contract_year from dual
)
select to_date(substr(to_char(contract_year),1,7)||'2017','DD-MON-YYYY') from your_table
If your are not seeing output as required, it means that you have to change your client setting. Try running this in SQL Plus window.
Combine to_date and to_char:
SELECT to_date(to_char(contract_year,'dd-mm-') || '2016','dd-mm-yyyy')
FROM table
WHERE program = programid
It's not entirely clear to me whether contract_year is a date or a string.
If it's a date:
select to_char(contract_year, 'DD-MON-') || '2016'
from table
If it's a string in the format DD-MON-YYYY then:
select to_char(to_date(contract_year, 'DD-MON-YYYY'), 'DD-MON-') || '2016'
from table