split the date then divide it into three columns - sql

I have a column for a date in this format 2/21/2020, and 11/2/2020
what I mean if it the month 2 it will be one digit if it is 11 two digits
the result that I need from the above examples should be like 20200221 and 20201102
so I decided to split the date and divide them into three columns be like year:2020 month:02 day:21
then I will combine them
year || month || day
now I know how to split the year
select
regexp_substr(date1, '[^/]+$', 1, 1) as year
from (select '2/11/2020' as date1 from dual)
but if I need the month or the day what I should change in the code
In addition, If the month contains one digit, the 0 should be added beside the month number like 02. do I use case when condition?
Is there any easier way to solve this problem

Try this hope you like it.
declare #TargetDate datetime='2/21/2020'
SELECT REPLACE(CONVERT(varchar,(SELECT #TargetDate),23), '-', '') as Date;

If your column is of date datatype then
To_char(your_date_column, 'yyyymmdd')
If your column is of varchar datatype then
To_char(to_date(your_date_column_in_string,'mm/dd/yyyy'), 'yyyymmdd')
Dont worry about one or two digit in day or month as oracle is smart enough to recognize it.
See this db<>fiddle demo

Related

Oracle concatenate two int columns, make them date and select a date from them

I have column REF_YEAR and column REF_MONTH. Both are int columns.
I want to select a range of dates between these two ranges.
for example
select *
from table
where date between '05/2020' and '12/2021'
Can i do something like that but with similar results for int columns?
If I understood you correctly, you'd
select *
from your_table
where to_date(ref_year || lpad(ref_month, 2, '0'), 'yyyymm')
between date '2020-05-01' and date '2021-12-01'
i.e. you'd have to "convert" (to_date) ref_year concatenated with ref_month (left-padded with zero up to 2 characters in length because May 2021 isn't 20215 but 202105), using appropriate format mask (yyyymm) and compare it to valid date values, e.g. date literals I used.
You should be using a single proper date column here, rather than separate columns for the month and year. That being said, integer month and year columns is still workable. Using your example:
SELECT *
FROM yourTable
WHERE REF_YEAR = 2020 AND REF_MONTH >= 5 OR REF_YEAR = 2021;
Note that the above logic would include the entire 2021 calendar year, including the entire month of December. This is what I imagine you want; if you instead want to exclude December, we would have to change the logic slightly. That being said, it would be much better to maintain a single date column REF_DATE:
SELECT *
FROM yourTable
WHERE REF_DATE >= date '2020-05-01' AND REF_DATE < date '2022-01-01''

Combine and convert nvarchar month field + float year field to a single date field

I have a table in SQL Server 2012 with a month column stored as nvarchar(255):
"January", "February", "March"
And another column in this table with year stored separately as float
"2012","2013,"2014".
I do not have a day column so I want to create a combined month date column with the day starting as 1.
So for month and year fields January 2012. I want to show '2012-01-01'
How can I do such and add that into my current table?
I want to find the maximum row for a record in my table for each employee.
so for an [employee #], [month],[year]. what is latest record so for example below:
1. 102, Jan, 2019
2. 102, feb, 2019
I want to only see the second record which is the latest.
SQL Server has pretty flexible conversion to date. So, just convert the columns to a date:
select convert(date, month + ' ' + year)
You can get the maximum as:
select empid, max(convert(date, month + ' ' + year))
from t
group by empid;
If you really like, you can change the format for output purposes. I would advise you to stick with a date, though.
Note: This assumes that your internationalization settings are set to English -- which seems reasonable if you are storing month names in English.
Fix your design! The way you store data makes it really inefficient to interpret it. Here, I think the simplest option is datefromparts() and a 12-branches case expression.
Assuming that the (float) year is stored in column col_year and the (string) month is in col_month:
select t.*,
datefromparts(
cast(col_year as int),
case col_month
when 'January' then 1
when 'February' then 2
...
when 'December' then 12
end,
1
) as date_col
from mytable t

current month record should be display from table

please assist me to correct it.i want to show current month data i tried this but its showing error the literal is too long :
number date
10 20-Jan-2018
20 30-Oct-2018
30 24-Sep-2018
24 01-Oct-2018
select number
from table
where date <= to_char(sysdate,'mm');
It looks like your date column contains text. You should ideally always store date information in proper date columns. As a workaround, we can go in the other direction and use TO_DATE on your date column, to compare it to the first of the current month:
SELECT number
FROM yourTable
WHERE TO_DATE(date, 'dd-mon-yyyy') < TRUNC(sysdate, 'mm');

Informix - extract day of month from date, and select only odd days

how to extract day of month from date, and select only odd days in Informix SQL?
Day of month is simply the DAY(date_or_datetime_column) function, related to the MONTH() and YEAR() functions. Getting only odd numbers is done with a simple modulo 2 expression.
So I think you only need:
SELECT date_col, DAY(date_col)
FROM table
WHERE MOD(DAY(date_col), 2) = 1
Hope that's useful.

how to know if between 2 date's it past 5 days - Oracle 10g?

I have two date's:
1) 01/05/2009
2) 06/05/2009
How can I know if there's 5 days between them?
You can subtract two dates to get the difference in days between them (the unit for date columns in Oracle is 1 day).
In your example, I assume date is spelled in DD/MM/YYYY format. So you could do this:
select case when
abs(to_date('01/05/2009','DD/MM/YYYY') - to_date('06/05/2009','DD/MM/YYYY')) = 5
then 'YES'
else 'NO'
end as ARE_DATES_5_DAYS_APART
from
dual;
If the two dates are two columns in a table, then use table name instead of "dual" in query above.
First, get your dates into variables. Then you can use simple addition to determine if the required number of days have passed as Oracle treats addition and subtraction with dates as adding or subtracting days.
For instance Date1, + 5 will equal the date plus 5 days.
In PL/SQL your block may end up looking something like this:
declare
date1 date := to_date('01/05/2009', 'DD/MM/YYYY');
date2 date := to_date('06/05/2009', 'DD/MM/YYYY');
begin
if date1 + 5 >= date2 then
dbms_output.putline('Date 2 is more than five or more days past date 1!');
else
dbms_output.putline('There is less than five days between date 1 and date 2!');
end if;
end;
First decide whether your date format is DD/MM/YYYY or MM/DD/YYYY