How to get all rows in certain month - sql

I have a column named DC34_DATE of type DATE (defined this way: DATE '2013-04-17'). I need to select all rows for a specific month (for example April). I have used WHERE MONTH(DC34_DATE)=04; but it doesn't work.

You can use the extract from date function to get the date. Try this.
WHERE EXTRACT(month from DC34_DATE) = '4';

select yourColumns
from yourTable
where datepart(month,yourDateColumn)=4
TSQL solution..don't know which dialect you're using.

Related

PostgreSQL: No operator matches the given name and argument types. You might need to add explicit type casts [duplicate]

I want to select rows according to the month of a date or timestamp column like this:
SELECT id, name, birthday
FROM employee.person
WHERE Month(birthday) > 10;
But I only get error messages in PostgreSQL.
How can this be done?
You can use EXTRACT function, like this:
SELECT id, name, birthday FROM employee.person
WHERE EXTRACT(MONTH FROM birthday) > 10;
Your problem comes from the fact that there is no such thing as Month function in PostgreSQL. Check online documentation here to see what you can get instead. Extract should be enough.
If you want you can also extract the month name using the following function.
SELECT TO_CHAR(DATE(REPORT_DATE), 'Month') FROM TABLE_NAME

Getting not valid month in Oracle sql

I have a table called Transactions that has a column called trans_date. I am just trying to do a simple query in the SQL*Plus command window
The query is
SELECT * FROM transactions WHERE
trans_date BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
When I run this query I get not valid month and there is a little * under trans_date. Most of what I have read suggests the query is right but I am not sure what the problem is. The data type is varchar2(20).
Since trans_date is a varchar and you're trying to query whether it's between two dates, you need to convert it to a date too. Assuming it has the same format as the literals in your query:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyy') BETWEEN
to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
Seems like problem is columns data type, Try convert it to date,
SELECT * FROM transactions
WHERE to_date(trans_date,'mm/dd/yyyy') BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
You need to convert trans_date to a date. However, you can use date constants for the comparisons:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyyy') BETWEEN DATE '2021-09-11' AND DATE '2021-09-12';
You should fix your data model so the dates are stored correctly, using Oracle's built-in data type.

Oracle SQL: Transpose Columns to Rows After Using Extract

I have a table with column DATE. Date is 'dd/mm/yyyy' and I want only days. So I try with extract and return what I need, but I what using transpose for column to row.
The select statement is:
select EXTRACT (DAY FROM "DATE") DAY
from people;
Is this thing possible?
Thank you!
If you have a string, then just use the leftmost two characters:
select substr("DATE", 1, 2) as day
That said, you should not be storing dates as strings. It is wrong, wrong, wrong. You cannot use the built-in date/time functions. You cannot use inequality comparisons either. Fix your data model.
The date format doesn't matter. It is linked to your NLS local settings and this is how you see this.
To have it generic and extract DAY from the date do this:
select to_char(sysdate, 'DD') from dual;
Would return 07 since it's September 7th 2020.

Parsing the value of a specific column in a database

I have a column called cost_calc_date of type Date in my database table named as account.
The value in a particular cell is 2018-03-20 which is in YYYY-MM-DD format. I need to return only the month, that is 03 as the output.
Please let me know how I can achieve this.
You can try the following code:
SELECT MONTH(cost_calc_date) as 'Month'
FROM account
Or this code:
SELECT DATEPART(month, cost_calc_date) AS 'Month'
from account;
You can use extract() to get parts of a date (or timestamp) column.
To get the month, use:
extract(month from cost_calc_date);
This will return a number, e.g. 3
select datepart(mm,cost_calc_date) from account

How to obtain records of a specific month in SQL by date

I'm using Postgres 9.1 and try to get records from a table knowing the given month and should know if the date type date field contains the month.
This is my query:
SELECT consejo.numero,consejo.fecha FROM consejo WHERE MONTH(consejo.fecha) = 2
but I get the following error:
ERROR: There is no month function (date)
LINE 1: ... T consejo.numero, consejo.fecha council FROM WHERE MONTH (cons ...
HINT: No function matches the name and types of arguments. You may need to add explicit type conversion.
might be wrong?
In Postgresql there is no MONTH function available. You can use EXTRACT instead:
WHERE EXTRACT(MONTH FROM consejo.fecha) = 2
Another option is to use PostgreSQL's date_part() function:
WHERE date_part('month', consejo.fecha) = 2
Look at the reference guide:
http://www.postgresql.org/docs/9.5/static/functions-datetime.html