how do i select the dates with a specific month? - sql

How do I select the date with a specific month?
For example I have in my table:
1-mar-2015
16-mar-2013
12-feb-2016
14-apr-2014
And I want to get only the dates from march.
The result should be:
1-mar-2015
16-mar-2013

Databases have ways of extracting date parts. The ANSI standard method is:
where extract(month from date) = 3
Other databases support functions such as month() or to_char() to achieve the same purpose.

Related

Big query sql: month to date query

I have daily data and the code im using creates a report between some dates. I want to replace the date interval with month to date. So every time it will generate a month to date report based on which month we are in. Is there a simple way to do it? thanks :)
An example using BigQuery Standard SQL
SELECT
*
FROM
your_table
WHERE
your_date_field BETWEEN
DATE_TRUNC(CURRENT_DATE(), month) --to get start date of current month
AND
CURRENT_DATE()
You should be able to use that in a WHERE clause and

SQL query between two dates using regex

I'm doing a sql query to extract orders from date to date.
I think I need to use LIKE because dates are strings and has days, but I have to consider only year and month, for example:
order date -> 2019-01-01
regex -> LIKE '%2019-01-%' (all orders in done in january)
and then I have to to the same thing for another date (let's say '%2019-03-%'). After that I use the two dates to extract time with BETWEEN.
The problem is that I don't know how to put these two things together.
You can use the substring and then do comparison as follows:
select * from orders
where substring(date, 1, 7) between '2019-01' and '2019-03'
Or you can convert your date column into date and then compare it with dates.

How to write a SQL query to retrieve all those customers whose age in months is more than 200 months?

How to write a SQL/Oracle query to retrieve all those customers whose age in months is more than 200 months?
I have a exam on Monday but I am having some confusion with months and dates calculation.
You can use a Query like this for MySQL:
SELECT *
FROM yourTable
WHERE bithdayField <= NOW() - INTERVAL 200 MONTH;
The logic is the same (the date is older than today minus 200 months), but the actual SQL is usually different, because DBMSes have a large variation of syntax in the date/time area.
Standard SQL & MySQL:
WHERE datecol < current_date - interval '200' month
Oracle:
WHERE datecol < add_months(current_date, -200)
In fact Oracle also supports the Standard SQL version, but it's not recommended, because you might get an invalid date error when you do something like '2018-03-31' - interval '1' month. This is based on a (dumb) Standard SQL rule which MySQL doesn't follow: one month before March 31 was February 31, oops, that date doesn't exists.
In Oracle DB, there are two nice functions : months_between and add_months
been used for these type date calculations. For your case, you may use one of the following :
select id, name, surname
from customers
where months_between(trunc(sysdate),DOB)>200;
or
select id, name, surname
from customers
where add_months(trunc(sysdate),-200)>DOB;
demo

splitting a datetime column into year, month and week

I want to split a datetime column so that the year and the month both have their own column in a select statement output. I also want to have a column by week of the year, as opposed to specific date.
Basically, I want separate year, month, and week columns to show up in my select statement output.
Try using the DatePart function as shown in the following:
select
datepart(year,Mydate),
datepart(month,Mydate),
datepart(week,Mydate)
From
MyTable
Note: If you need to calculate the week number by ISO 8601 standards then you'll need to use datepart(iso_week,Mydate)
You could also look at the DateName function
select
datename(month,Mydate)
From
MyTable
Here is another way. Use SQL Servers YEAR() and MONTH() functions. For week, I use datepart(week,Mydate) as noted by #DMK.
SELECT YEAR(MyDate), MONTH(MyDate), DATEPART(WEEK,Mydate) From YourTable
check this
select datepart(Month,DateColumn) Mnth,datename(Month,DateColumn) MnthName,datepart(Year,DateColumn) Year1,((day(DateColumn)-1) / 7) + 1 week from dbo.Table

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.