How to get month name from partition time in Bigquery - sql

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

Related

How to get value only for the current month

I would like to know if we can got values from date if the date is in the current month
For example, If I have :
date value
28/06/2021 50
02/07/2021 100
05/07/2021 18
Then I search to have :
02/07/2021 100
05/07/2021 18
because 28/06/2021 is not in the current month
thanks in advance !
In Mysql you can use now() and month() functions to get the current date ang extract the month.
Select *
from table
where <your filters>
having MONTH(NOW()) = MONTH( table.date_field)
Different databases like oracle , postrest change the name of this functions , so you must find it
TSQL
select * from tbl
where MONTH(date)=month(getdate())
You'll want to match the YEAR along with the MONTH if you will have data that spans more than a year.
SELECT date, value
FROM table
WHERE YEAR(date) = YEAR(GETDATE()) AND MONTH(date) = MONTH(GETDATE())

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.

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;

Oracle SQL: selecting date field without day (Only Month and Year)

I need to select values from a Database where I have a complete Date. Now I have to get this Date without the Day because I have to group and count them per Month.
I did it like this, but this will get me the Month like for January with 1 and I need 01...
(extract(YEAR,Month from ak.date ) || '.' ||extract(Month from ak.date) ) as Datum
Use the TO_CHAR function for this:
TO_CHAR(ak.date, 'YYYY.MM') as Datum
Another way:
TRUNC(ak.date, 'MM')
Advantage of this is that date sorting and date arithmetic still work.

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.