Concatenate Year and Month - sql

I have 2 date fields, surv_year (int) and surv_month (text).
Example outputs:
surv_year surv_month
2022 January
2022 August
Is there anyway to concatenate the 2 and change the month from a text to a number and have my final output as YYYYMM with my example above showing 202201 and 202208?

You can convert the two values to a proper date using the to_date() function:
select to_date(concat(surv_month, ' ', surv_year), 'Month yyyy')
from the_table
will return a date on the first day of that month.
This date can be formatted using the to_char() function to the format that you want:
select to_char(to_date(concat(surv_month, ' ', surv_year), 'Month yyyy'), 'yyyymm')
from the_table

Related

Get the month name from a timestamp column [duplicate]

How to fetch month name from a given date in Oracle?
If the given date is '15-11-2010' then I want November from this date.
select to_char(sysdate, 'Month') from dual
in your example will be:
select to_char(to_date('15-11-2010', 'DD-MM-YYYY'), 'Month') from dual
Try this,
select to_char(sysdate,'dd') from dual; -> 08 (date)
select to_char(sysdate,'mm') from dual; -> 02 (month in number)
select to_char(sysdate,'yyyy') from dual; -> 2013 (Full year)
to_char(mydate, 'MONTH') will do the job.
In Oracle (atleast 11g) database :
If you hit
select to_char(SYSDATE,'Month') from dual;
It gives unformatted month name, with spaces, for e.g. May would be given as 'May '. The string May will have spaces.
In order to format month name, i.e to trim spaces, you need
select to_char(SYSDATE,'fmMonth') from dual;
This would return 'May'.
If you are trying to pull the value from a field, you could use:
select extract(month from [field_name])
from [table_name]
You can also insert day or year for the "month" extraction value above.
if you are taking system date:
--Full month name :
select to_char(trunc(sysdate,'MONTH'),'MONTH') as month from dual; --MARCH
--Short month name:
select to_char(trunc(sysdate,'MON'),'MON') as month from dual; --MAR
--Month number:
select to_char(trunc(sysdate,'MM'),'MM') as month from dual; --03
if you are taking a specific date:
--Full month's name:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MONTH'),'MONTH') as month from dual; --MARCH
--Short month's name:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MON'),'MON') as month from dual; --MAR
--Month's number:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MM'),'MM') as month from dual; --03
Try this
select to_char(SYSDATE,'Month') from dual;
for full name and try this
select to_char(SYSDATE,'Mon') from dual;
for abbreviation
you can find more option here:
https://www.techonthenet.com/oracle/functions/to_char.php

Given day, month and year, calculate weekending date

Given day, month and year as integer columns in the table, calculate the date and weekending date from these values.
I tried the following but it gives me an incorrect result for the derived date as '2022-08-05 00:00:00'
select to_date(2020||03||20,'YYYYMMDD')
Even tried below but results in a string without leading zeros for month and day
select (cast (2020 as varchar)+cast (03 as varchar(2))+cast (02 as varchar(2)))
Result for above is : 202032
The problem is that you have numbers -- so leading zeros are missing. You can try:
select to_date('2020'||'03'||'20', 'YYYYMMDD')
Or, if these have to be numbers:
select to_date( (2020 * 10000 + 03 * 100 + 20)::text, 'YYYYMMDD')

How to get month and year from date in SQL

I need help to get month and year from a date which is in yyyymmdd format.
The dates look like this, '20150102', the output should be Jan 2015.
I want the output as on single value, currently using datepart function Im getting output in 2 different columns for month and year
This may help:
SQL Server:
SELECT FORMAT (GETDATE(), 'MMM yyyy') -- Jul 2019
SELECT FORMAT (GETDATE(), 'MMMM yyyy') -- July 2019
SELECT RIGHT(CONVERT(VARCHAR(11), GETDATE(), 106), 8) -- Jul 2019
For more details: https://www.tutorialgateway.org/sql-date-format/
MySQL:
SELECT DATE_FORMAT("20150102", "%M %Y"); -- January 2015
SELECT DATE_FORMAT("20150102", "%b %Y"); -- Jan 2015
SELECT YEAR(date) AS 'year', MONTH(date) AS 'month'
For more details: http://www.sqlines.com/mysql-to-oracle/date_format
MySQL will recognize the date string as a date, so you can use date_format() to get the format you want:
select date_format('20150102', '%b %Y')
there is a list of all differnet sql date formats sql date formats

Know the week number of a month in bigquery

I want to know the week number of a month for a date in bigquery standard sql.In PostgreSQL if I write:
select To_char(current_date, 'YYYY-MM-W')<br>
It works for the date '25-04-2018' as 2018-04-4.
Here 2018 is the year, 04 is the month and 4 is the fourth week of the month in which the date falls.
I want something similar in bigquery standard sql.
If I write:
select format_date("%Y-%m",current_date())
It gives only 2018-04
I also want to know the week number of month.
Thank you in advance.
Here is solution (defining a UDF that you can use in a query) along with an example.
CREATE TEMP FUNCTION DateWithWeekOfMonth(date DATE) AS (
CONCAT(
FORMAT_DATE('%Y-%m-', date),
CAST(DIV(EXTRACT(DAY FROM date), 7) + 1 AS STRING)
)
);
SELECT date, DateWithWeekOfMonth(date)
FROM (
SELECT DATE '2018-04-01' AS date UNION ALL
SELECT DATE '2018-04-07' UNION ALL
SELECT DATE '2018-04-08' UNION ALL
SELECT DATE '2018-04-30'
);

first date and last date of month from month and year

How to get first date and last date of month in oracle by giving input parameter as month.
For eg. if i give input month as 'Jan' and Year as '2016' it should give first date and last date of the month.
You can use TRUNC for that:
First day:
TRUNC(your_date, 'MM')
Last day:
ADD_MONTHS(TRUNC(your_date, 'MM'), 1) - 1
You basically TRUNC the month to its first day, add one month to get the first day of the next month, and then go back a day.
If you do not have a date in the month but only the month and the year you can simply use the first of each month to construct a date:
TO_DATE('1.' || your_month || '.' || your_year, 'DD.MM.YYYY')
ADD_MONTHS(TO_DATE('1.' || your_month || '.' || your_year, 'DD.MM.YYYY'), 1) - 1
You can use the TRUNC and LAST_DAY functions for this purpose.
select TRUNC(d,'MM'), LAST_DAY(d)
from (select to_date('01-2016','MM-YYYY') as d from dual);
01-JAN-2016 00:00:00 31-JAN-2016 00:00:00