How do I know if any date belongs to the first half?

Dúvidas, dicas e truques de SQL, Select, Update, Delete, cláusulas, operações com joins, Funções em SQLs, etc
Post Reply
User avatar
Porva
Rank: DBA Sênior
Rank: DBA Sênior
Posts: 342
Joined: Mon, 29 Jan 2007 7:36 am
Location: São Paulo/SP
Rafael S. Nunes
São Paulo/SP

Guys, beleza?

I need to do a SELECT where a date is between the period 1/01 (from any year) and 31/05 (from any year), I will return an indicator '1th semester'

and between 01/06 to 31/12 (from any year) = '2nd semester'.
User avatar
adrianoturbo
Moderador
Moderador
Posts: 393
Joined: Thu, 20 Mar 2008 4:09 pm
Location: Brasília
Adriano Alves
---Para cada problema dificil existe uma solução simples.----

Brother, converts the month in number with the function to_number, will actually need the function to_char as well to do the conversion into string and then in number, in the sequence place the filters and be happy, follow the codes of how to do.

Select all

 
 
select  to_number(to_char(campo_data ,'mm') mês from qualquer_coisa  
where  mês <=6 --- primeiro semestre 
 

Select all

 
 
select  to_number(to_char(campo_data ,'mm') mês from qualquer_coisa  
where  mês >6 --- segundo semestre semestre 
 
User avatar
Porva
Rank: DBA Sênior
Rank: DBA Sênior
Posts: 342
Joined: Mon, 29 Jan 2007 7:36 am
Location: São Paulo/SP
Rafael S. Nunes
São Paulo/SP

Thanks Adriano,

The solution is even simple, I did not call, I also saw another function here in the forum that I found interesting, the function extract :

Select all

SELECT EXTRACT(MONTH FROM SYSDATE) mês FROM dual;

can use for days and years as well:

Select all

SELECT EXTRACT(DAY FROM SYSDATE) mês FROM dual; 
SELECT EXTRACT(YEAR FROM SYSDATE) mês FROM dual;
Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 15 guests