I'm trying to work with Year and Week in combination and would like to include leap years when they occur.
In my code I'm only adding year + week and I'm not getting what I desire. I would like to have '202101' if I change my code to '+3' because this year there are 53 weeks and the 54 week should 01.
(SELECT
LEFT(CONVERT(VARCHAR(10), GETDATE(), 120), 4) +
CAST((DATEPART(ISOWK, GETDATE()) + 3) AS NVARCHAR(2)))
I would like to be able to change my code like this
...GETDATE()) + 2) in my code should return 202053
...GETDATE()) + 3) in my code should return 202001
...GETDATE()) + 4) in my code should return 202002
etc...
Might it be that i should change my code and not have it as a 2 part YYYY and WW but rather combine it as a date? I would believe its a string now.
Related
I'm currently trying to understand some code and for the life of me I can't figure out what the negative/minus operator is doing inside of the CASE statement. When I try removing it, it breaks the visualization.
This is part of a bigger puzzle of figuring out exactly what this portion of the query does. I know it's a counter of some sort that checks the year, and if the year is 2017, then extracts the month. If it isn't 2017, then it extracts the month 12 months from now, but I'm not sure how it works beyond that. Any insight into the negative/minus operators in this CASE statement or this function in general would be appreciated.
MAX(
CASE WHEN (- CASE WHEN EXTRACT(year FROM premium_creation_date) = 2017 --?? some sort of counter
THEN EXTRACT(month FROM premium_creation_date)
ELSE
EXTRACT(month FROM premium_creation_date) + 12
END
+ CASE WHEN EXTRACT(year FROM user_creation_date) = 2017
THEN EXTRACT(month FROM user_creation_date)
ELSE
EXTRACT(month FROM user_creation_date) + 12
END
) = 1
THEN unique_user_count
ELSE
NULL
END
) AS restriction
The - changes the sign of the result of the CASE.
The CASE gives the month of the premium_creation_date when the year of that date is 2017. Otherwise it's the month plus 12.
So e.g. 2017-05-04 yields 5 in the CASE, which makes -5 after the - took effect. For 2018-07-06 that would be 7 + 12 = 19 and therefore -19.
Actually you could turn this whole thing around and subtract the first CASE from the second. That may look clearer.
Hi I am creating a table in MS Access to store the details of children in a school.
I have a field called YearGroup which needs to calculate the school year they are in based on their date of birth and whether they have been moved up or down a year.
I.e. if the expression deems they are six years old they should be placed in year 2. If they were moved down or up a year they should be in year 1 or 3 (this is based on another field in the table called YearModifier).
The code I have at the moment is this:
Year(Now()) - IIf(Month([DOB]) > 8, Year([DOB]) + 6 + [YearModifier], Year([DOB]) + 5 + [YearModifier])
My problem is that Year(Now()) is returning as invalid expression. Lots of websites have recognised using the Now() function and also I've tried Date() but nothing seems to be accepted by Access (The version is 2010).
What is going on? How can I get today's date in a calculated field expression?
Thanks
Try creating a query with all of the fields from your table, and then add an extra field YearGroup: Year(Now()) - IIf(Month([DOB]) > 8, Year([DOB]) + 6 + [YearModifier], Year([DOB]) + 5 + [YearModifier])
It appears that Date functions can't be used in calculated columns in tables.
You could use this to get the year, which might work in field expressions:
format(date(),"yyyy")
About your function (which I have re-written very slightly)
Year( Now() )
- Year([DOB])
- IIf( Month([DOB]) > 8
, 6
, 5 )
+ [YearModifier]
however!
I don't think you want to use now(). Which year they are in depend on their age at the 1st Sept at the start of the current academic year not now! Ok now will work until 31/dec/2015, so I must assume you will not be using the function after this date!
If you are you must use 2015 not Now().
Ok?
You can calculate the age of the children with a simple function:
Public Function AgeSimple( _
ByVal datDateOfBirth As Date) _
As Integer
' Returns the difference in full years from datDateOfBirth to current date.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of years to dates of Feb. 29.
' when the resulting year is a common year.
' After an idea of Markus G. Fischer.
'
' 2007-06-26. Cactus Data ApS, CPH.
Dim datToday As Date
Dim intAge As Integer
Dim intYears As Integer
datToday = Date
' Find difference in calendar years.
intYears = DateDiff("yyyy", datDateOfBirth, datToday)
If intYears > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of datDateOfBirth.
intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
End If
AgeSimple = intAge
End Function
Then your expression would be something like this (ignoring the modifier):
ClassYear: IIf(AgeSimple([DOB]) > 6, 2, 1)
I want to calculate age with birth day and current day. result should be "years-month-day".How can i do ? Please help.
i'm try this but not be great
DateDiff(“yyyy”,birthday,currentday)
if current date is 20/05/2015 and birth date is 1/06/1991, result = 24 but it should be 23 years 5 month 19 days
If (Not(isNull({#birthdate}))) Then
ToText(int(DateDiff('d',{#birthdate},CurrentDate())/365.25),0)+' Years '+
ToText(int(remainder(DateDiff('d',{#birthdate},CurrentDate()),365.25)/30),0)+' Months '+
If day(CurrentDate()) < Day({#birthdate}) Then
ToText(day(dateserial(year({#birthdate}), Month({#birthdate})+1,1-1)) - Day({#birthdate}) + Day(CurrentDate()),0) + ' days'
Else
ToText(day(CurrentDate()) - day({#birthdate}),0) + ' days'
No perfect but works nicely.. here is an article to read that might help as well for
calculating birthdays
I'm trying to format(in yyyymmdd) the previous month's first day within the SSIS 2005 expression builder. So far I have managed to get the year and month correctly represented. However, I'm stuck when it comes to the day part of the expression.
Below is the complete errorneous code which I'm trying to hack.
RIGHT((DT_WSTR, 4) YEAR( DATEADD( "mm",-1, getdate() )), 4) +
RIGHT("0" + (DT_WSTR,2) MONTH( DATEADD( "mm",-1, getdate() )), 2)+
RIGHT("0" + (DT_WSTR,2) DAY(DATEADD("mm", DATEDIFF("mm", 0, GETDATE())-1,0)),2)
The first line returns the year ( in yyyy format) the second line returns the month (mm format). However, I'm stuck at returning the day part (in format 01 or just 1) in the third line.
Any help is appreciated.
There's no need to do any fancy expression stuff for the day. The first day of the month is always 01 so your expression should be what you already had for year and month plus the concatenation of the literal string "01"
RIGHT((DT_WSTR, 4) YEAR( DATEADD( "mm",-1, getdate() )), 4) +
RIGHT("0" + (DT_WSTR,2) MONTH( DATEADD( "mm",-1, getdate() )), 2)+
"01"
This will return a 2 char day part for any date:
right('0'+cast(day(getdate()) as Varchar(3)),2)
I have a .NET 2010 app that bangs against a SQL db. On the app side, a user can search on Begin date and End Date. Bot of these are just Month + Year. I then format them so they are complete dates. So when they go to the stored proc they'll look like this...
Begin Date: 1/1/2011
End Date: 5/31/2011
But the date in the db is broken up into 3 int fields, Month,Day & Year, ...of which Day may or may not be filled in (0 if not). It would be ok for this to always default to one when running this query. So if the values in the db were Month=3, Day=0 Year=2011 I would like the sql statement to render as
Where FORMATTEDDATEHERE between '1/1/2011' and '5/31/2011'
I just can't figure out how to format sql fields in a where clause.
Did you try something like
WHERE CONVERT(DATETIME,
CONVERT(VARCHAR(4), [c_year]) + '/'
+ CONVERT(VARCHAR(2), [c_month]) + '/'
+ CONVERT(VARCHAR(2), [c_day]))
BETWEEN '2011/1/1' AND '2011/5/31'
Hope it helps
you could build the date using datadd functions within the WHERE clause EG.
SELECT ...
WHERE DATEADD(Day, field_days-1, DATEADD(Month, field_months-1, DATEADD(Year, field_years-1900, 0))) BETWEEN '1/1/2011' AND '5/31/2011'
Just replace field_days, field_months, field_years with the int fields on the table for days, months and year. This will return all records within the date range.
Is this what you require?
WHERE CAST(Year AS varchar) + RIGHT(100 + Month, 2) +
RIGHT(100 + COALESCE(NULLIF(Day, 0), 1), 2) BETWEEN ...