My birth year- 1975
Month- 08
Day- 28
Hour- 19
Minute- 10
Second- 22
I want to add 1.746387366 year to my(birth year, month, day, hour, minute, second) to get a new year month day hour minute second
How can I
Thanks.
You create a datetime of your birth moment:
Dim birthdate = new DateTime(1975, 8, 28, 19, 10, 22)
And you add that number of years to it:
Dim result = birthdate.AddYears(1.746387366)
Related
I am trying to create a lag function to return current month and last month streams for an artist.
Instead of returning streams for Feb vs Jan, I wan the function to use the last 30 days as a period for current month, and the previous 30 days as the previous month.
The query that I am currently using is this:
SELECT
DATEPART(month, date) AS month,
artist,
SUM([Streams]) AS streams,
LAG(SUM([Streams])) OVER (PARTITION BY artist ORDER BY DATEPART(month, date)) AS previous_month_streams
FROM combined_artist
WHERE date > DATEADD(m, -2, DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()-7), CAST(GETDATE()-7 AS DATE)))
GROUP BY DATEPART(month, date), artist;
While this works, it is not giving me the data I need. This is returning the sum of streams for February vs the Streams for the month of January. February seems very low because we only have one week worth of data in February.
My goal is to get the last 30 days from the max date in the table using a lag function. So if the max date is Feb. 7 2023, I want the current month to include data from Jan. 7 2023 - Feb. 7 2023, and the previous month to include data from Dec. 7 2022 - Jan. 7 2023. I am thinking to create a custom month date part that will start from the max date and give a month number to the last 30 days . (2 for Jan 7 - Feb 7, 1 for Dec 7 - Jan-7...) I am not sure how to go about this. This is in SQL Server and I am looking to use the lag function for performance reasons.
I think you could probably use something like datediff(d, date_you_care_about, max_date)/30 in your group by and partition by clauses.
The basic idea is that integer division rounds down, so if the difference between the dates is < 30, dividing it by 30 is 0. If the difference is >=30 but less than 60, dividing it by 30 is 1. And so forth.
You can see a proof of concept in this Fiddle.
I wanna order the data in an sqlite3 database by date. (Day and Month to be precise)
I have a table,
the data in the table are in the format YYYY-MM-DD
2003-02-20, 2005-07-16, 2008-11-18, 1998-01-02, 1996-08-27
Here, I wanna find all the data after a certain date(Current date- 'now') and in order.
The data is birthdays, so the order should be just based off of Month and Day and shouldn't care about the year.
For example, the data here is
Feb 20, Jul 16, Nov 18, Jan 1, Aug 27
current day= July 28
I want the output to look like
Aug 27, Nov 18, Jan 1, Feb 20, Jul 16
I've looked through many examples and documentations and tried some methods
SELECT * FROM table WHERE birthdays>date('now')
*birthdays are the column where dates are stored*
This gives all the data after ('now') as an output, but it orders it by year as well. Hence, the output will be none since none of the years are greater than current year. I wanna take the year out of the equation and just order it by Month and Day.
How can I do it?
You don't need a WHERE clause because all rows of the table should be returned.
What you want is a proper ORDER BY clause:
SELECT *
FROM tablename
ORDER BY strftime('%m-%d', birthdays) > strftime('%m-%d', 'now') DESC,
strftime('%m-%d', birthdays);
See the demo.
According to the sample data and the expected output you posted, you want to find all birthdays that will occur this year after the date of today. You may use the strftime function to extract month and day as the following:
Select user_id, DOB
From your_table
Where strftime('%m-%d',DOB) > strftime('%m-%d',date())
Order By strftime('%m-%d',DOB)
See a demo from db-fiddle.
you can select the dates, by taking he day of birth, adding the current year ( or the next one if it is smalerer tan the current date)and then selecting the dates that are bigger than the current date limiting 10
SELECT user_id, DOB
FROM your_table
ORDER BY
CASE WHEN date(strftime('%Y', date('now')) || strftime('-%m-%d', DOB)) > DATE() then date(strftime('%Y', date('now')) || strftime('-%m-%d', DOB))
ELSE date(strftime('%Y', date('now','+1 years')) || strftime('-%m-%d', DOB)) END
LIMIT 10;
user_id DOB
5 1996-08-27
10 1996-08-27
15 1996-09-27
13 2008-10-18
3 2008-11-18
8 2008-11-18
4 1998-01-02
9 1998-01-02
14 1998-01-02
1 2003-01-31
db<>fiddle here
I have to find the average price of iPhone Xs from this table within 1 August 2021 and 31 August 2021.
So the records that I have narrowed down are 8, 9, 11 and 12.
However records 8 and 9's StartDateTime and EndDateTime are outside of 1st and 31st August 2021, but still run through August 2021.
So far, I am only able to query for records 11 and 12.
Which is more straightforward:
SELECT *
FROM dbo.PriceHistory
WHERE
PName = 'iPhone Xs'
AND StartDateTime BETWEEN '2021-08-01 00:00:00.000' AND '2021-08-31 00:00:00.000';
How should I query so that I can get records 8 and 9 as well?
Based on the desired behaviour you've described, you probably want to check if the start date is before September 1, and the end date is after August 1. This will get all items whose date ranges overlap with August.
So:
SELECT *
FROM dbo.PriceHistory
WHERE
PName = 'iPhone Xs'
AND StartDateTime < '2021-09-01 00:00:00.000'
AND EndDateTime >= '2021-08-01 00:00:00.000';
(Note that the upper endpoint should be September 1 to ensure we include the last day of August.)
I've seen extractions of date, month and year from data format: "DD-MM-YYYY" and the like. (Where the month is numbered rather than named)
However, I have a dataset which has date values in the format: "Month_name date, year".
Eg. "August 30, 2019".
Assume that your DataFrame contains TxtDate column, with
date strings:
TxtDate
0 August 30, 2019
1 May 12, 2020
2 February 16, 2020
The first step is to convert the source column to datetime type and save it
in a new column:
df['Date'] = pd.to_datetime(df.TxtDate)
This function is so "clever" that you can do even without explicit
format specification.
Then extract partilular date components (and save them in respective
columns):
df['Year'] = df.Date.dt.year
df['Month'] = df.Date.dt.month
df['Day'] = df.Date.dt.day
And the last step is to drop Date column (you didn't write
that you need the whole date):
df.drop(columns='Date', inplace=True)
The result is:
TxtDate Year Month Day
0 August 30, 2019 2019 8 30
1 May 12, 2020 2020 5 12
2 February 16, 2020 2020 2 16
Maybe you should also drop TxtDate column (your choice).
all- I'm looking to automatically standardize a certain date field into the monday date of that week. Essentially, if an entry came in with a date in this field of Tuesday, July 30th, 2013- I would want to standardize it to Monday, July 30th, 2013. I'd like to be able to apply this to only dates in a certain column where entries may have more than one date in seperate columns.
Thank you!!
If you want Monday of the current week you can use the following in SQL Server:
SELECT DateAdd (Day, 2 - DatePart (dw, YourDate), YourDate)
Depending on desired behavior, you may have to use SET DATEFIRST 2 to adjust the behavior.
List<List<myRecord>> recordList = new List<<myRecord>>();
List<DateTime> mondays = new List<DateTime>();
DateTime first = new DateTime(2013, 1, 30);
DateTime last = new DateTime(2013, 7, 29);
for (Date mon = first; mon <= last; mon = mon.AddDays(7)) {
recordList.Add(new List<myRecord>([SELECT * FROM myRecord WHERE myRecord.Date >= mon AND myRecord.Date < mon.AddDays(7)] ))
}
foreach (List<myRecord> monList : recordList) {
//do stuff with each monday bucket
}
dunno what technology youre using but maybe something like this would work?