Click here to Skip to main content
15,888,106 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
What is the simplest way to store an array of DateTime values in SQL Server?
The length of the array can vary of course.

One option I thought of is using an xml column... Other suggestions?
Posted

Its an array. So use two tables.

In the first table mark the id of the datetime values.
In the second table, repeat the id and store the array items as rows one by one.
Table 1
ColA ColB DateId
..   ..   1
..   ..   2
...

Table 2
DateId DateValue
1      Date1
1      Date2
2      Date3
2      Date4
2      Date5
....
 
Share this answer
 
v4
Comments
Tomas Takac 22-Mar-15 4:33am    
I think you need to store the index of the element within the array too. Otherwise you won't be able to restore it to its original state. Unless of course the OP is happy with an unsorted array.
Abhinav S 22-Mar-15 4:49am    
Fair point. Index is needed if order is important.
Herbisaurus 22-Mar-15 6:16am    
धन्यवाद
Multi-valued columns in a relational database is never a good idea.
The recommended way is to have a separate table with a datetime field and a reference field to the record in the original table it belongs to.
SQL
CREATE TABLE ORIGINAL (
   OriginalId int NOT NULL IDENTITY PRIMARY KEY
  ,--
)

CREATE TABLE YOURDATETIMES (
   YourDateTimeId int NOT NULL IDENTITY PRIMARY KEY
  ,OriginalId int NOT NULL REFERENCES Original.OriginalId
  ,Value datetime2 NOT NULL
)
 
Share this answer
 
Comments
Herbisaurus 22-Mar-15 6:15am    
спасибо
phil.o 22-Mar-15 6:17am    
You're welcome :)

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