SQL在等于之后将所有内容放在引号周围

时间:2016-12-18 23:11:01

标签: sql sql-server

我有一个SQL字符串,我希望在等于之后将所有内容放在引号周围。

目前:@Condition = N'Sev = 300 OR EVC = 102 AND SRC = 380P064

之后:@Condition = N'Sev = '300' OR EVC = '102' AND SRC = '380P064'

有快速解决方法可以获得我想要的东西吗?感谢

enter image description here

2 个答案:

答案 0 :(得分:2)

惭愧我之前没有看到这个

Declare @Condition nvarchar(max) = N'Sev = 300 OR EVC = 102 AND SRC = 380P064 AND XXX = HELLO'

Set @Condition = Replace(Replace(Replace(@Condition,'= ','= '''),' OR ',''' OR '),' AND ',''' AND ')+''''

Select @Condition

返回

Sev = '300' OR EVC = '102' AND SRC = '380P064' AND XXX = 'HELLO'

或者如果你想要原件

Set @Condition=@Condition+''''
Select @Condition = Replace(@Condition,MapFrom,MapTo)
 From  (values ('= ','= ''')
              ,(' OR ',''' OR ')
              ,(' AND ',''' AND ')
       ) A (MapFrom,MapTo) 


Select @Condition

答案 1 :(得分:0)

我不明白你的问题,但我会试一试......

如果你想用'表示一个字符串,你需要在你的第一个N'Sev中逃脱它。像这样

import numpy as np
import pandas as pd
import csv
from sklearn import linear_model, naive_bayes
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.preprocessing import FunctionTransformer
from sklearn.pipeline import Pipeline
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.base import TransformerMixin
from sklearn import cross_validation



logistic_clf = Pipeline([('vect', CountVectorizer(ngram_range=(1, 2))),
                     ('ft_vec', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)),
                     ('tfidf', TfidfTransformer()),
                     ('ft_tfid', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)),
                     ('clf', linear_model.LogisticRegression(penalty='l2',solver='lbfgs',max_iter=1000, multi_class='ovr',warm_start=True)),
                    ])

gnb_clf = Pipeline([('vect', CountVectorizer()),
                     ('ft_vec', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)),
                     ('tfidf', TfidfTransformer()),
                     ('ft_tfid', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)),
                     ('clf', naive_bayes.GaussianNB()),
                    ])


import csv
from pandas import *

df = read_excel('trump_labeled.xlsx')

#Collect the output in y variable

y = df['Sentiment']
X = df['SentimentText']
from sklearn.cross_validation import train_test_split
#cross validation
X_train, X_test,y_train, y_test = train_test_split(X,y,test_size=0.25, random_state=42)
X_train = np.array(X_train)
y_train = np.array(y_train)
X_test = np.array(X_test)
y_test = np.array(y_test)



log_clf = logistic_clf.fit(X_train, y_train)
gnb_clf = gnb_clf.fit(X_train, y_train)

log_predicted = logistic_clf.predict(X_test) # predict labels for test data with logistic regression classifier
gnb_predicted = gnb_clf.predict(X_test) # predict labels for test data with naive bayes classifier

# PRINT SOME RESULTS FOR THE DATASETS PART
print("\nDATASET RESULTS")
print('\nLogistic Regression Results:\n\tNegative tweets: %.2f\n\tPositive tweets: %.2f' %(np.mean(log_predicted == 0), np.mean(log_predicted == 1)))
print('\tAccuracy: %.2f'% (np.mean(log_predicted == y_test)))
print('\tPositive Precision: %.2f' %(precision_score(y_test, log_predicted,pos_label=1)))
print('\tPositive Recall: %.2f' %(recall_score(y_test, log_predicted,pos_label=1)))
print('\tPositive F-measure: %.2f' %(f1_score(y_test, log_predicted,pos_label=1)))
print('\tNegative Precision: %.2f' %(precision_score(y_test, log_predicted,pos_label=0)))
print('\tNegative Recall: %.2f' %(recall_score(y_test, log_predicted,pos_label=0)))
print('\tNegative F-measure: %.2f' %(f1_score(y_test, log_predicted,pos_label=0)))

您也可以考虑使用“而不是

DATASET RESULTS

Logistic Regression Results:
        Negative tweets: 1.00
        Positive tweets: 0.00
        Accuracy: 0.72
        Positive Precision: 0.00
        Positive Recall: 0.00
        Positive F-measure: 0.00
        Negative Precision: 0.72
        Negative Recall: 1.00
        Negative F-measure: 0.84
C:\Users\My\Anaconda2\lib\sitepackages\sklearn\metrics\classification.py:1074: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples.'precision', 'predicted', average, warn_for)
C:\Users\My\Anaconda2\lib\sitepackages\sklearn\metrics\classification.py:1074: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 due to no predicted samples.'precision', 'predicted', average, warn_for)