Convert date into individual numerical columns for year month and day SQL - 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;

Related

How to get month name from partition time in Bigquery

I need to extract month number from current_date() in BigQquery. I am using below sql statement that extracts month name .How to get month number:
Select MONTH(current_date())
Any suggestion?
Try EXTRACT to extract month number from date:
SELECT EXTRACT(MONTH FROM CURRENT_DATE())
Try FORMAT_TIMESTAMP to extract month name from timestamp:
SELECT FORMAT_TIMESTAMP('%B', _PARTITIONTIME) FROM tablename

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

How to get year, month and day from seconds in PostgreSql?

I'm trying to create three columns based on date in seconds format.
My user.updated_at = 1521533490
I would like to get year, month and day separately and put these formatted values to columns for example:
year -> 2018, month -> 11, day -> 23
Does someone know how can I do that in pgSQL?
I would like to get year, month and day separately and put these formated values to columns
Don't do that.
Use a single column of type date or timestamp, depending on your application. Not every combination of your three columns will be a valid date. But every value in a single column of type date will be a valid date.
If you need the parts of a date separately, use PostgreSQL's date/time functions.
Try this approche to get differents arguments, then you can do whatever you want:
SELECT to_timestamp(1521533490); //2018-03-20T08:11:30.000Z
SELECT to_char(to_timestamp(1521533490), 'HH'); // 08 Hour
SELECT to_char(to_timestamp(1521533490), 'MI'); // 11 Minutes
SELECT to_char(to_timestamp(1521533490), 'SS'); // 30 Seconds
SELECT to_char(to_timestamp(1521533490), 'DD'); // 20 Day
SELECT to_char(to_timestamp(1521533490), 'Mon'); // MAR Month
SELECT to_char(to_timestamp(1521533490), 'YYYY'); // 2018 Year
Use the EXTRACT function.
SELECT to_timestamp(updated_at) "Date",
EXTRACT(YEAR FROM (to_timestamp(updated_at))) "Year",
EXTRACT(MONTH FROM (to_timestamp(updated_at))) "Month",
EXTRACT(DAY FROM (to_timestamp(updated_at))) "Day"
FROM users
Output
Date Year Month Day
2018-03-20T08:11:30Z 2018 3 20
SQL Fiddle: http://sqlfiddle.com/#!15/afe0e/15/0
More information on the EXTRACT function.

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

Extracting Day of Week as an Integer with Netezza SQL

This should be doable, but how can I extract the day of the week from a field containing data in date format with Netezza SQL? I can write the following query:
SELECT date_part('day',a.report_dt) as report_dt
FROM table as a
but that gives me the day of the month.
thanks for any help
The below queries give day numbers for any week,month,year for a particular date.
--Day of Week
SELECT EXTRACT(dow FROM report_dt) FROM table;
--Day of Month
SELECT DATE_PART('day', report_dt) FROM table;
--Day of Year
SELECT EXTRACT(doy FROM report_dt) FROM table;
Netezza is just ANSI SQL, originally derived from PostgreSQL. I'd expect this to work.
select extract(dow from a.report_dt) as report_dt
from table as a
Returns values should range from 0 to 6; 0 is Sunday. You might expect that to be an integer, but in PostgreSQL at least, the returned value is a double-precision floating point.
If you want to extract directly the day name :
Select to_char(date, 'Day') as Day_Name From table;
In Netezza SQL, SELECT EXTRACT(dow FROM report_dt) would return values 1 to 7. 1 is Sunday, 7 is Saturday.