splitting a datetime column into year, month and week - sql

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

Related

Convert date into individual numerical columns for year month and day SQL

I have a date column in the format YY-MON-DD, e.g. 25-JUN-05. Is it possible to isolate this into 3 separate columns for year, month and day? Where month is converted from text to numerical, e.g. Year: 25, Month: 06, Day: 05?
MS SQL SERVER
As Nebi suggested, you can use DATEPART and extract each part and store it into different columns.
SELECT DATEPART(DAY,'2008-10-22'); -- Returns DAY part i.e 22
SELECT DATEPART(MONTH,'2008-10-22'); -- Returns MONTH part i.e 10
SELECT DATEPART(YEAR,'2008-10-22'); -- Returns YEAR part i.e 2008
Try with the below script,if you are using SQL Server.
SELECT 'Year: '+CAST(LEFT(YourdateColumn,2) as VARCHAR(2))+', Month: ' +CAST(MONTH('01-'+SUBSTRING(YourdateColumn,4,3)+'-15')as VARCHAR(2))+', Day:'+CAST(RIGHT(YourdateColumn,2)as VARCHAR(2))
FROM Yourtable
sample output :
You didn't specify your DBMS.
The following is standard SQL assuming that column really is a DATE column
select extract(year from the_column) as the_year,
extract(month from the_column) as the_month,
extract(day from the_column) as the_day
from the_table;

how do i select the dates with a specific month?

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.

PL/SQL to subtract two dates

I use SQL Server 2012, and in my table I have columns id, date, days.
Column date is formatted dd-mm-yyyy.
I need PL/SQL code to fetch today date and find difference between date column and store it in days column
Example:
fetch today date and find difference of stored date from the table
id date days
1 01-12-2015 1
2 30-11-2015 2
I need PL/SQL code.
You may use DATEDIFF:
SELECT DATEDIFF(day,'2015-06-05','2015-08-05') AS DiffDate
For your case it should be:
INSERT INTO table_name (days)
SELECT DATEDIFF(day,table_name.date,GETDATE());
SQL Server Date Functions
Use DATEDIFF and the LEAD function to compare against the next row.
SELECT DATEDIFF(d,date,LEAD(date) OVER (ORDER BY id))
FROM yourtable
For comparison against today's date
SELECT DATEDIFF(d,date,GETDATE())
FROM yourtable

How to get last and first date of every week from predefined date table (oracle SQL)

I have Table D_date in which all dates of a year, week number,quarter number etc attributes are defined. I just want to get first and last date of every week of year 2015.
Sample D_date tabe attached.
It is simple min/max if I understand you right
SELECT calendar_year_nbr, week, min(actual_date),max(actual_date)
FROM D_date
GROUP BY calendar_year_nbr, week
I just want to get first and last date of every week of year 2015.
Since you have precomputed values already stored in the table, you could directly use MIN and MAX as aggregate functions along with GROUP BY.
For example,
SELECT MIN(actual_date) min_date,
MAX(actual_date) max_date,
calendar_week_nbr
FROM d_date
WHERE calendar_year_nbr = 2015
GROUP BY calendar_week_nbr
ORDER BY min_date;
Another way is to use ROWNUM() OVER() analytic function.

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.