Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to store month(string) and year(numeric) in Sql database Eg : January 2013. Is there any predefined data type available in sql server. If not how can I achieve this? Thank you.
Posted

You should store the date/time value into a field with one of these sql date type based on your requirement: Date and Time Data Types and Functions (Transact-SQL)[^]
These date values can then be retrieved and formatted using Custom Date and Time Format Strings[^] for presentation using code like c# or vb.net.
 
Share this answer
 
Comments
partha143 5-Jun-15 4:53am    
Thank you Sir. I have now solved my problem.
Peter Leow 5-Jun-15 5:03am    
You are welcome.
Don't.
Instead, look at using a DATETIME (or even a DATE) datatype instead. Neither of them store a formatted date, but they store the dat in a format it can be used for a lot more than just presentation.
Store it as the first of the month, in a DATETIME, at midnight, and convert it to the appropriate string for presentation in your presentation software (which is normally pretty trivial). That way, it works seamlessly regardless of which language the user wants to display, and you can do comparisons and groupings a lot more easily. (You also don't have to cope with potential corrupt dates in your DB like "2013 January" or "Junuary 2013" and so on - which can be a nightmare to fix later).
 
Share this answer
 
Comments
Peter Leow 5-Jun-15 4:02am    
Your answer is not only faster but also better.
partha143 5-Jun-15 4:52am    
Thank you Sir. As you suggested I used DATE datatype and I solved my problem by converting month number to month name. I just needed another information on saving date for fields such as Academic Year which value will be like "2013-14". Thank you.
OriginalGriff 5-Jun-15 5:09am    
That's more complicated, because it depends on what it's going to be used for. Almost certainly, I'd store at as a "starts in" rather than as a year period - and probably use a DATE again, storing the actual day the term starts. You can very easily extract the year portion from that, and generate the academic year as a string if you need it, but the actual start day probably needs to be stored somewhere anyway!

If you are going to store this with every record though, I'd use a second table:
ID (INTEGER, IDENTITY, PK)
TermStart (DATE)
AcademicYears NVARCHAR(7)
and use a foreign key to link that into my main table.
That way it keeps the size of each record down, and is almost trivial to use via an SQL JOIN when you need presentation information.
Use Date with a convention that you leave the day as 1 so January 2013 is stored as 1 Jan 2013. Any code or SQL that uses this variable has to simply ignores the day part.
 
Share this answer
 
you can use Date[^] column type. then you can insert directly Date from your .net application ( use default date like 1st day of month) and when you display the year/month from database you can use format strings like datefromDb.ToString("MMMM yyyy")
 
Share this answer
 
there is no datatype in sql that can store single record with multiple data types in a single column you have to use varchar or other string storage data types for your desired data (January 2013)

Solution: save month in separate column (varchar) and year in separate column (numeric)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900