How can I generate an ID that will reset every year? - vb.net

I need help in VB.net I'm a newbie in programming.
I have a problem in generating an ID with the format
`yyyy` = now.year
`mm` = now.month
`0000` = auto number
I want to increment the auto number everytime I add records
so it goes like this
2014090001
and also it will reset back to 0000 when year changes.

thank you for all your replies, I took all your advice and I got a solution CONSIDER THIS THREAD AS CLOSE!!
my final solution is creating another table in database with the fields ID, counter, and YEAR
I created a condition that will test the current year, so if the year = now.year the counter will just increment by 1 and if year <> to now.year then the counter will back to 0. it will add up another records in that table so the use of ID is to delete the previous records.

Related

I need to convert total date to month only

I set up a google forms form for my work where my employees pass information and this information is recorded in a spreadsheet. The information, when recorded, automatically inserts a date and time in the first column of form responses. However, when I enter the code = month (a1), it always returns the answer "1" or "January" and this information does not match the date entered in the column. How do I fix this?
If you are entering '=month(a1)' for every row, then you are always taking the month of the top left cell in the sheet. You would need to adjust the row number for the row you are in.
You could use something like '=month(now())' to ensure you are always getting the month of the current date.

Podio Age Calculation

My organisation has a Podio calculation field (see code below) to calculate the age of a person.
In order to get this field to update daily (because we need up to date data), the associated field #Date Today is updated with a Globiflow flow with today's date.
At the moment, there are ~1800 records and growing. So if this is run every day we are going way over the limits set by Globiflow (http://www.globiflow.com/help/globiflow-plans-and-limits.php).
Any ideas on more elegant ways to achieve this without using up our workflows? Essentially I need to calculate the age of each person, each day.
JS in Podio 'Age' field:
var date = #Date Of Birth;
var dateCalc = moment(date);
var currentDate = moment(#Date Today);
var calc = currentDate.diff(dateCalc, 'y','m');
date != null ? calc : null
Thanks
Shaun
One idea is to have an app with single item to keep the today's date. Update this single item everyday using GlobiFlow with today's date.
This item should be referred in all the 1800 Person items. Now use the today date from this single item in age calculation in person app. You can have a GlobiFlow to refer this single item automatically when ever a new Person is created.

Is it possible to return part of a field from the last row entered into a table

I am proposing to have a table (the design isn't settled on yet and can be altered dependent upon the views expressed in reply to this question) that will have a primary key of type int (using auto increment) and a field (ReturnPeriod of type Nchar) that will contain data in the form of '06 2013' (representing in this instance June 2013).
I would simply like to return 06 or whatever happens to be in the last record entered in the table. This table will never grow by more than 4 records per annum (so it will never be that big). It also has a column indicating the date that the last entry was created.
That column seems to my mind at least to be the most suitable candidate for getting the last record, so essentially I'd like to know if sql has a inbuilt function for comparing the date the query is run to the nearest match in a column, and to return the first two characters of a field.
So far I have:
Select Mid(ReturnPeriod,1,2) from Returns
Where DateReturnEntered = <and this is where I'm stuck>
What I'm looking for is a where clause that would get me the last entered record using the date the query is run as its reference point(DateRetunEntered of type Date contains the date a record was entered).
Of course there may be an even easier way to guarantee that one has the last record in which case I'm open to suggestions.
Thanks
I think you should store ReturnPeriod as a datetime for example not 06 2013 as a VARCHAR but 01.06.2013 as a DATETIME (first day of 06.2013).
In this case, if I've got your question right, you can use GETDATE() to get current time:
SELECT TOP 1 MONTH(ReturnPeriod)
FROM Returns
WHERE DateReturnEntered<=GETDATE()
ORDER BY DateReturnEntered DESC
If you store ReturnPeriod as a varchar then
SELECT TOP 1 LEFT(ReturnPeriod,2)
FROM Returns
WHERE DateReturnEntered<=GETDATE()
ORDER BY DateReturnEntered DESC
I would store your ReturnPeriod as a date datatype, using a nominal 1st of the month, e.g. 1 Jun 2013, if you don't have the actual date.
This will allow direct comparison against your entered date, with trivial formatting of the return value if required.
Your query would then find the latest date prior to your date entered.
SELECT MONTH(MAX(ReturnPeriod)) AS ReturnMonth
FROM Returns
WHERE ReturnPeriod <= #DateReturnEntered

How to have a database counter that resets monthly?

I have an application that needs to generate an unique identifier per item, this identifier is a mix of some initials, the date and 4 digits that increment for each new item. The tricky part is that the last 4 digits should reset monthly.
As an example, lets say we have UID "SP-20121218-0001", the last 4 digits "0001" will increment up until January, and then the first item created in January should have "SP-20130101-0001".
Any thoughts?
create a table with fields Year, Month, CurrentCount. write a stored procedure that selects a row for the current year and month, if it does not exists creates it and increments the number.
To me this sounds like the perfect use case for a stored function.
In postgresql you would do this by having a sequence and then using a function to select a value from the sequence in combination with working out the current date and when the date turned over, resetting the sequence.
The best way though would be to not bother resetting the sequence and to just go with a unique number supplemented by the date.
One possible option is to base the last 4 digits on a database sequence and then have a separate process that will reset that database sequence when a new month starts.

storing data ranges - effective representation

I need to store values for every day in timeline, i.e. every user of database should has status assigned for every day, like this:
from 1.1.2000 to 28.05.2011 - status 1
from 29.05.2011 to 30.01.2012 - status 3
from 1.2.2012 to infinity - status 4
Each day should have only one status assigned, and last status is not ending (until another one is given). My question is what is effective representation in sql database? Obvious solution is to create row for each change (with the last day the status is assigned in each range), like this:
uptodate status
28.05.2011 status 1
30.01.2012 status 3
01.01.9999 status 4
this has many problems - if i would want to add another range, say from 15.02.2012, i would need to alter last row too:
uptodate status
28.05.2011 status 1
30.01.2012 status 3
14.02.2012 status 4
01.01.9999 status 8
and it requires lots of checking to make sure there is no overlapping and errors, especially if someone wants to modify ranges in the middle of the list - inserting a new status from 29.01.2012 to 10.02.2012 is hard to implement (it would require data ranges of status 3 and status 4 to shrink accordingly to make space for new status). Is there any better solution?
i thought about completly other solution, like storing each day status in separate row - so there will be row for every day in timeline. This would make it easy to update - simply enter new status for rows with date between start and end. Of course this would generate big amount of needless data, so it's bad solution, but is coherent and easy to manage. I was wondering if there is something in between, but i guess not.
more context: i want moderator to be able to assign status freely to any dates, and edit it if he would need to. But most often moderator will be adding new status data ranges at the end. I don't really need the last status. After moderator finishes editing whole month time, I need to generate raport based on status on each day in that month. But anytime moderator may want to edit data months ago (which would be reflected on updated raports), and he can put one status for i.e. one year in advance.
You seem to want to use this table for two things - recording the current status and the history of status changes. You should separate the current status out and move it up to the parent (just like the registered date)
User
===============
Registered Date
Current Status
Status History
===============
Uptodate
Status
Your table structure should include the effective and end dates of the status period. This effectively "tiles" the statuses into groups that don't overlap. The last row should have a dummy end date (as you have above) or NULL. Using a value instead of NULL is useful if you have indexes on the end date.
With this structure, to get the status on any given date, you use the query:
select *
from t
where <date> between effdate and enddate
To add a new status at the end of the period requires two changes:
Modify the row in the table with the enddate = 01/01/9999 to have an enddate of yesterday.
Insert a new row with the effdate of today and an enddate of 01/01/9999
I would wrap this in a stored procedure.
To change a status on one date in the past requires splitting one of the historical records in two. Multiple dates may require changing multiple records.
If you have a date range, you can get all tiles that overlap a given time period with the query:
select *
from t
where <periodstart> <= enddate and <periodend> >= effdate