How to find the weekday from the date for an entire column of dates - sql

I know that you can use the DATEPART(), FORMAT(), WEEKDAY() functions to find the weekday of a single date, but I need to use these functions to create a new column that finds the weekday for an entire column of dates in date_dim_id. How do I do this?
Below is my date_dim_id column. I want to add a column beside it that tells me which weekday each date is.

Try this:
select date_part(dow, '20210805') as dayoftheweek

Related

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.

Creating a new column for 'week ending date' based on existing date column (MYSQL)

I have a table with a date column but would like to add a another column that shows the week ending date for each day in the table so that I can aggregate values by week ending. I would like to have the week end on a Thursday and the data begins 2017-01-01.
This would be where something like virtual/computed columns would come in to play. There isn't such a thing in PostgreSQL yet however.
One way around it is to use functions and function-based indexes. An example is here: http://bernardoamc.github.io/sql/2015/05/11/postgres-virtual-columns/

How to get records between from and to date, when dates are for the same month/year?

I am trying to create a query to that can get some records in a table that is between a from and to date, with the dates being in month/year only. The problem that I am having is trying to get the records when the from and to dates are for the same month/year.
Here is a example of the issue that I am having:
select start_date
from job
where trunc(start_date) between to_date('05-2016','mm-yyyy') and to_date('05-2016','mm-yyyy')
In the job table, there are records with start_date in the month of May, but in order to see them I need to set the to date to '06-2016'. Is there way to get all of the records with a start_date in the month of May by just specifying that the from and to dates is 05-2016?
In your example you are selecting all start_date's between 5/1/2016 and 5/1/2016.
It seems like you want to capture everything in a month, but you are not specifying a format to truncate. Without specifying 'Month' in the Trunc() you are truncating to the day. When you truncate to Month you can now, actually just set it equal to the to_date() rather than between:
select start_date
from job
where trunc(start_date,'Month') = to_date('05-2016','mm-yyyy')
Here is some information on Trunc(date, [fmt]). when the fmt argument is left blank Trunc() defaults to 'round' to the nearest day but there are many other options.
If you want to specify ranges greater than a Month you can use between (but note, this is from the first day of the first to_date() to the first day of the second 'to_date()':
select start_date
from job
where trunc(start_date)
between to_date('06-2016','mm-yyyy')
and to_date('09-2016','mm-yyyy')
In this example all Start_dates would populate between 6/1/2016 and 9/1/2016.
I think the most accurate way to do this would be:
select start_date
from job
where trunc(start_date)
between to_date('06-01-2016','mm-dd-yyyy')
and to_date('09-30-2016','mm-dd-yyyy')
How about?
select start_date
from job
where trunc(start_date, 'mm') between
to_date('05-2016', 'mm-yyyy')
and to_date('05-2016', 'mm-yyyy')
Oracle has no way of interpolating the date on the right end of your between as falling at the end of the month. When to_date() is missing a day value in the format it simply supplies the 1st as the default value. Not sure if that's the behavior your were anticipating.
If instead you truncate dates by month (rather than day) then the comparisons will only involve dates falling on the first of the month. With the dates collapsed that way you can then treat a start and end of the two identical values as an inclusive range. Presumably you'll replace the hard-coded dates with parameters of some form and this works for wider ranges as well. Of course, the between can be just an equality test if you're always querying on a single month. And this isn't going to use an index on your column so it's not necessarily the most efficient.
I think this is what you're looking for. Is there a reason you need to supply the date value(s) in that format? Perhaps there's a better way to approach this using a little date math.
Try like this:
select start_date
from job
where trunc(start_date) between to_date('05-2016','mm-yyyy')
and dateadd('d', -1, dateadd('m', 1, to_date('05-2016','mm-yyyy')))
In order to see an entire month worth of dates, your date range in the between clause must be a full month long.

Calendar control date not known (ASP.Net)

Sometimes we have dates that are unknown, eg: We won't know the date but just the month and year.
eg: --/Dec/2015, the date is not known? How do we accept these kind of values?
Any ideas on can this be stored on SQL Server database?
I would store it as a date in the database. Because with a date you have benefits like:
Calculation function like DATEADD and DATEDIFF.
You will be able to sort on one column
Index will be one column
When you need to get the year and the month. You just use MONTH and YEAR

Filtering rows based off of relation to date column

I am trying to find a good way to filter data based off of the date column (Column I in my actualy spreadsheet). I can copy the rows and all of that to another sheet just fine, but the date thing is new to me. Basically I need to know how to filter based on three situations:
If the current date is within 3-6 months of the date in Column I;
If the current date is within 0-3 months of the date in Column I;
If the current date is past the date in Column I.
Thank you for any assistance.
Sounds like you want to use the DATEDIFF VBA function. Here is an example: http://www.techonthenet.com/excel/formulas/datediff.php.
In addition to DateDiff you can use:
Date()
Year()
Month()
Day()
Functions to determine this information.