How to make a new column for days of the week - sql

I am working with a FitBit daily activity dataset within BigQuery. I have a date column (stored as a date type), and I'm trying to make a new column that will reflect the days of the week (Saturday, Sunday, etc) alongside the original date column. I'm still fairly new to SQL, so I'm not sure how to do this. This is what I've tried:
Attempt #1. I also tried to use the DATENAME function, but I kept getting an error message. Anyone who can point me in the right direction will be much appreciated!

Use below
select ActivityDate,
format_date('%A', ActivityDate) as day_of_week
from your_table
with output like

Related

SQL - Automatically adjust Where clause to previous month in YYYY-MM format

(This is all steps in containers within an Alteryx flow that is connecting to a Teradata source)
My SQL is incredibly rusty as it's been almost 8 years since I've needed to use it. I know this is a quite basic question. Basically I have several queries that need to be manually adjusted monthly to shift the month. in YYYY-MM format. They look like this:
Is the main one where I just adjust one backwards one month
select DB.TABLE.field1, DB.TABLE.Year_month
from DB.TABLE
where DB.TABLE.Year_month = '2023-01'
This is the secondary one where I adjust one backwards one month, and the others are same month or plus a month or more.
and A.B_MONTH in ('2022-12-01', '2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01','2023-05-01')
and B.Year_month = '2023-01'
How do I adjust the where clause to always be the needed relative references?
Any help is greatly appreciated
I tried using concat but it choked for some reason.
You can try this:
select DB.TABLE.field1, DB.TABLE.Year_month
from DB.TABLE
where DB.TABLE.Year_month = DATE_FORMAT( NOW() - INTERVAL 1 MONTH, '%Y/%m')
I don't understand your second need, but you can do it similar to my response.
Just play with the NOW() - INTERVAL X.
Pretty basic stuff. ADD_MONTHS to move your month around, TO_CHAR for your desired format.
To get the previous month:
select to_char(add_months(current_date,-1), 'YYYY-MM')

Formatting Day Time X Axis in SSRS Charts

I am working on a SSRS report which compares data of 2 given months. I'm categorising the chart based on Day and time in the following format 1, 6:00 AM. I get this column from the T-SQL itself. But the axis does not come properly. It looks like below now which doesn't make sense. I want it to be in order from 1st to 30th with the time component.
I guess I need some kind of sorting on X-axis with respect to date time. Please help!
After deleting from sorts from chart I'm getting some extra repitive dates after comparing all 30 days of both months. Data from the query looks alright to me!
Thank you!
Ok. Large query
You X-axis are show value in format date,hh mm tt Right ?
Then you want to sort them with day number 1 - 30.
From your query I suggest you add 1 field is like this CAST(SampleCollected AS DATE) [orders] and use this field in Order in Query or Sort on SSRS (not recommend ) and if you use Order in Query must delete sort condition on chart sort.
But if result still not you want try to add MONTH(SampleCollected) As MonthG to order again like this
ORDER BY MONTH(SampleCollected),CAST(SampleCollected AS DATE)
Hope it's Help.

How to select dates with same day and month (ignoring year) using firebird

I have a database where users stores their birthday. y-m-d, and I'm trying to get every user that has the same birthday; the year can be different between each user.
So how do I turn this to a working script:
SELECT username FROM table_name WHERE birthday='$birthday'
$birthday gets its data from a form, where the inputs is example: 2002-02-02. And if users have this birthday it should echo it out. But the problem is that it checks with the year, and I'm trying to only get month and day, not year.
I have tried with EXTRACT(MONTH FROM ...) but didn't get it to work. What am I missing?
You should store your data as DATE. Then the date/time like EXTRACT functions will work.
I recommend adding new column and filling it with data from the original one using CAST. Then dropping the original column. Also consider using parameters instead of string concatenation to prevent SQL injection.
select
extract(MONTH from cast('2002-02-02' as date))
from rdb$database

How to extract dates with datatye DATETIME from colum A in table X and put them into Table Y while changing datatype into DATE

Long title, easy meaning:
How is it possible to extract from a date like "2014-04-04 10:47:30.000", which is stored in one column, it's components like year, month and day?
I'm not interested in the time.
For example, I have a table called "Incidents". Inside the table we got a column called "IncidentID" and a column called "ReportingDate", in which dates like the above-mentionend are stored. Let's say we have about 50k Incidents, therefore we have also 50k dates.
A year has 365 days. I want to query for the count of the Incidents, which were reported on different dates - for instance on the 5th of October 2013.
So: How can I get the components of the date and put them into another table while having own columns for the components and how can I query for the Incidents as well?
I guess at first I have to change the datatype of the date from DATETIME to DATE, but I'm not quite sure how to go further. May anyone help me while giving me a code and explains me what it does for a sql-noob? :-)
To achieve this
I want to query for the count of the Incidents, which were reported on
different dates - for instance on the 5th of October 2013.
you haven't do this:
I guess at first I have to change the datatype of the date from
DATETIME to DATE, but I'm not quite sure how to go further.
Just query
SELECT
IncidentID
FROM incidents
WHERE ReportingDate >= '20131005'
AND ReportingDate < '20131006'

PostgreSQL - GROUP BY timestamp values?

I've got a table with purchase orders stored in it. Each row has a timestamp indicating when the order was placed. I'd like to be able to create a report indicating the number of purchases each day, month, or year. I figured I would do a simple SELECT COUNT(xxx) FROM tbl_orders GROUP BY tbl_orders.purchase_time and get the value, but it turns out I can't GROUP BY a timestamp column.
Is there another way to accomplish this? I'd ideally like a flexible solution so I could use whatever timeframe I needed (hourly, monthly, weekly, etc.) Thanks for any suggestions you can give!
This does the trick without the date_trunc function (easier to read).
// 2014
select created_on::DATE from users group by created_on::DATE
// updated September 2018 (thanks to #wegry)
select created_on::DATE as co from users group by co
What we're doing here is casting the original value into a DATE rendering the time data in this value inconsequential.
Grouping by a timestamp column works fine for me here, keeping in mind that even a 1-microsecond difference will prevent two rows from being grouped together.
To group by larger time periods, group by an expression on the timestamp column that returns an appropriately truncated value. date_trunc can be useful here, as can to_char.