Published on

March 4, 2025

Collecting and Managing Time Series Data in SQL Server

Time series data is becoming increasingly common in SQL Server databases. This type of data is defined by its association with specific time periods, such as days or weeks. Examples of time series data include daily temperature and rainfall measurements, inventory and order tracking, and security prices.

In this article, we will explore how to collect time series data from the internet and insert it into SQL Server. We will also discuss how to add new data to an existing set of time series data.

Collecting Time Series Data

One approach to collecting time series data from the internet is to use Python. Python provides an easy-to-use interface for extracting data from web sources. In this example, we will use Python to extract security price data from Yahoo Finance.

Here is a Python script that demonstrates how to collect time series data for a set of stock symbols:

# Import necessary libraries
import pandas_datareader.data as web
import datetime

# Read ticker symbols from a file
symbol = []
with open('MSSQLTips_4.txt') as f:
    for line in f:
        symbol.append(line.strip())

# Set start and end dates for the time series data
start = datetime.date(2019, 9, 17)
end = datetime.date(2019, 9, 24)

# Loop through ticker symbols and collect data
for sym in symbol:
    try:
        df = web.DataReader(sym, 'yahoo', start, end)
        # Process and insert data into SQL Server
        # ...
    except:
        print("Error collecting data for symbol:", sym)

This script uses the pandas_datareader library to extract data from Yahoo Finance. It reads a list of ticker symbols from a file, sets the start and end dates for the data, and then loops through each symbol to collect the corresponding time series data.

Inserting Time Series Data into SQL Server

Once you have collected the time series data, you can insert it into SQL Server for further analysis and reporting. One way to do this is by using the bulk insert command.

Here is an example script that demonstrates how to insert the collected data into a SQL Server table:

USE [YourDatabase]
GO

-- Create table for time series data
CREATE TABLE [dbo].[TimeSeriesData](
    [Date] DATE,
    [Symbol] NVARCHAR(10),
    [Open] MONEY,
    [High] MONEY,
    [Low] MONEY,
    [Close] MONEY,
    [Volume] INT
)

-- Bulk insert data from CSV file
BULK INSERT [dbo].[TimeSeriesData]
FROM 'C:\path\to\csv\file.csv'
WITH (
    FIRSTROW = 2,
    FIELDTERMINATOR = ',',
    ROWTERMINATOR = '\n'
)

-- Query the table
SELECT *
FROM [dbo].[TimeSeriesData]
ORDER BY [Symbol], [Date]

This script creates a table called TimeSeriesData to store the time series data. It then uses the BULK INSERT command to insert the data from a CSV file into the table. Finally, it queries the table to display the inserted data.

Managing Time Series Data

As time goes on, you may need to manage and update your time series data. This can involve adding new symbols or appending fresh data for more recent time periods.

To add new symbols to the existing time series data, you can follow a similar process as the initial load. Collect the data for the new symbols using Python, save it to a CSV file, and then use the bulk insert command to insert the data into the SQL Server table.

To append fresh data for more recent time periods, you can modify the Python script to collect data for the desired time range and then use the bulk insert command to add the new data to the table.

Conclusion

Collecting and managing time series data in SQL Server can be a valuable skill for SQL Server professionals. By using Python and the bulk insert command, you can easily collect and insert time series data from the internet into SQL Server. With the ability to add new symbols and append fresh data, you can keep your time series data up to date and ready for analysis.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.