在数据库字段中用逗号替换逗号

时间:2016-07-04 05:50:13

标签: sql sql-server sql-server-2008

我在表格中有一个字段,用于存储以逗号分隔的多个标记,例如

tag1, tag2, tag3,tag4

对于波斯语版本,我需要用،替换单个逗号 所以标签看起来像

tag1، tag2، tag3،tag4،

我在数据库中有超过三千条记录,标签与،,一起存储,但我希望所有这些记录都与،分开。

如何使用SQL查找并用,替换所有،

我使用SQL Server作为我的数据库。

我们说表结构是

表BLOG

博客表格字段

ID int
Title nVarchar(300)
Description nVarchar(500)
Details nVarchar(max)
Date DateTime
Tags nVarchar(300)
Image varchar(60)
CategoryID int
Updated DateTime
Created DateTime
Published Boolean

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用import numpy as np import pandas as pd import datetime as dt import matplotlib.pyplot as plt import calendar # frame from get_year_deltas import get_year_deltas from constant_short_rate import constant_short_rate from market_environment import market_environment from plot_option_stats import plot_option_stats # simulation from sn_random_numbers import sn_random_numbers from simulation_class import simulation_class from geometric_brownian_motion import geometric_brownian_motion from jump_diffusion import jump_diffusion from square_root_diffusion import square_root_diffusion # valuation from valuation_class import valuation_class from valuation_mcs_european import valuation_mcs_european from valuation_mcs_american import valuation_mcs_american from derivatives_position import derivatives_position from derivatives_portfolio import derivatives_portfolio #import os #path = os.getcwd() url = 'http://www.stoxx.com/download/historical_values/h_vstoxx.txt' vstoxx_index = pd.read_csv(url, index_col=0, header=2,parse_dates=True, dayfirst=True) vstoxx_index = vstoxx_index[('2013/12/31' < vstoxx_index.index) & (vstoxx_index.index < '2014/4/1')] vstoxx_futures = pd.read_excel('./vstoxx_march_2014.xlsx', 'vstoxx_futures') del vstoxx_futures['A_SETTLEMENT_PRICE_SCALED'] del vstoxx_futures['A_CALL_PUT_FLAG'] del vstoxx_futures['A_EXERCISE_PRICE'] del vstoxx_futures['A_PRODUCT_ID'] columns = ['DATE', 'EXP_YEAR', 'EXP_MONTH', 'PRICE'] vstoxx_futures.columns = columns def third_friday(date): day = 21 - (calendar.weekday(date.year, date.month, 1) + 2) % 7 return dt.datetime(date.year, date.month, day) set(vstoxx_futures['EXP_MONTH']) third_fridays = {} for month in set(vstoxx_futures['EXP_MONTH']): third_fridays[month] = third_friday(dt.datetime(2014, month, 1)) #third_fridays tf = lambda x: third_fridays[x] vstoxx_futures['MATURITY'] = vstoxx_futures['EXP_MONTH'].apply(tf) #vstoxx_futures.tail() vstoxx_options = pd.read_excel('./vstoxx_march_2014.xlsx', 'vstoxx_options') #vstoxx_options.info() del vstoxx_options['A_SETTLEMENT_PRICE_SCALED'] del vstoxx_options['A_PRODUCT_ID'] columns = ['DATE', 'EXP_YEAR', 'EXP_MONTH', 'TYPE', 'STRIKE', 'PRICE'] vstoxx_options.columns = columns vstoxx_options['MATURITY'] = vstoxx_options['EXP_MONTH'].apply(tf) #vstoxx_options.head() vstoxx_options['STRIKE'] = vstoxx_options['STRIKE'] / 100.0 save = False if save is True: import warnings warnings.simplefilter('ignore') h5 = pd.HDFStore('./vstoxx_march_2014.h5', complevel=9, complib='blosc') h5['vstoxx_index'] = vstoxx_index h5['vstoxx_futures'] = vstoxx_futures h5['vstoxx_options'] = vstoxx_options h5.close() pricing_date = dt.datetime(2014, 3, 31) # last trading day in March 2014 maturity = third_fridays[10] # October maturity initial_value = vstoxx_index['V2TX'][pricing_date] # VSTOXX on pricing_date forward = vstoxx_futures[(vstoxx_futures.DATE == pricing_date) & (vstoxx_futures.MATURITY == maturity)]['PRICE'].values[0] tol = 0.20 option_selection = vstoxx_options[(vstoxx_options.DATE == pricing_date) & (vstoxx_options.MATURITY == maturity) & (vstoxx_options.TYPE == 'C') & (vstoxx_options.STRIKE > (1 - tol) * forward) & (vstoxx_options.STRIKE < (1 + tol) * forward)] me_vstoxx = market_environment('me_vstoxx', pricing_date) me_vstoxx.add_constant('initial_value', initial_value) me_vstoxx.add_constant('final_date', maturity) me_vstoxx.add_constant('currency', 'EUR') me_vstoxx.add_constant('frequency', 'B') me_vstoxx.add_constant('paths', 10000) csr = constant_short_rate('csr', 0.01) # somewhat arbitrarily chosen here me_vstoxx.add_curve('discount_curve', csr) # parameters to be calibrated later me_vstoxx.add_constant('kappa', 1.0) me_vstoxx.add_constant('theta', 1.2 * initial_value) vol_est = vstoxx_index['V2TX'].std() * np.sqrt(len(vstoxx_index['V2TX']) / 252.0) me_vstoxx.add_constant('volatility', vol_est) # vol_est vstoxx_model = square_root_diffusion('vstoxx_model', me_vstoxx) me_vstoxx.add_constant('strike', forward) me_vstoxx.add_constant('maturity', maturity) payoff_func = 'np.maximum(maturity_value - strike, 0)' vstoxx_eur_call = valuation_mcs_european('vstoxx_eur_call',vstoxx_model, me_vstoxx, payoff_func) option_models = {} for option in option_selection.index: strike = option_selection['STRIKE'].ix[option] me_vstoxx.add_constant('strike', strike) option_models[option] = valuation_mcs_european( 'eur_call_%d' % strike, vstoxx_model, me_vstoxx, payoff_func ) def calculate_model_values(p0): ''' Returns all relevant option values. Parameters p0 : tuple/list, tuple of kappa, theta, volatility Returns model_values : dict, dictionary with model values ''' kappa, theta, volatility = p0 vstoxx_model.update(kappa=kappa, theta=theta, volatility=volatility) model_values = {} for option in option_models: model_values[option] = option_models[option].present_value(fixed_seed=True) return model_values # calculate_model_values((0.5, 27.5, vol_est)) i = 0 def mean_squared_error(p0): ''' Returns the mean-squared error given the model and market values. Parameters p0 : tuple/list, tuple of kappa, theta, volatility Returns MSE : float, mean-squared error ''' global i model_values = np.array(calculate_model_values(p0).values()) market_values = option_selection['PRICE'].values option_diffs = model_values - market_values MSE = np.sum(option_diffs ** 2) / len(option_diffs) # vectorized MSE calculation if i % 20 == 0: if i == 0: print( '%4s' % i, '%6s' % "kappa", '%6s' % "theta", '%6s —>' % "vola", '%6s' % "MSE") print( '%4d' % i, '%6.3f' % p0[0], '%6.3f' % p0[1], '%6.3f —>' % p0[2], '%6.3f' % MSE ) i += 1 return MSE mean_squared_error((0.5, 27.5, vol_est))

REPLACE()

查找并替换为Unicode字符