我有一个名为“Sentence varchar(100)”的专栏 它包含数据
"first name, last name : is Activated on 01/01/2017 "
"first name, last name : is Downloaded on 01/01/2017 "
"first name, last name : is Deleted on 01/01/2017 "
"first name, last name : is Deactivated on 01/01/
"first name, last name : is Born on 01/01/2017 "
这里我想要一个sql查询来显示每个字符串为 “名字,姓氏:已激活” “名字,姓氏:出生”
我想从“on the ...... so”中删除字符串
你能帮我解决这个问题。如何为此编写一个sql server查询。
答案 0 :(得分:5)
你很幸运,因为要删除的字符串部分是固定长度的。所以,你可以这样做:
url
" 14"是select left(sentence, len(sentence) - 14)
的长度。
答案 1 :(得分:2)
这不是规范化表格,因此非常难以使用。
你应该有一张像
这样的表Event: Id | firstName | lastName | date | eventTypeId
其中EventType
代表Activated
,Downloaded
,Deleted
,代表2列:
EventType: Id | eventName
然后,您可以通过以下方式重建数据:
SELECT CONCAT(firstName," , ",lastName," : is ",EventType.eventName," on ",date)
FROM EVENT
JOIN EventType ON EventType.id=Event.eventTypeId
或者,要获取没有日期的数据:
SELECT CONCAT(firstName," , ",lastName," : is ",EventType.eventName)
FROM EVENT
JOIN EventType ON EventType.id=Event.eventTypeId
答案 2 :(得分:0)
您可以使用替换
EVENT_TRACE_REAL_TIME_MODE
答案 3 :(得分:0)
类似于:UPDATE Table SET Sentence = SUBSTR(Sentence, 0, LOCATE(' on ', Sentence))
答案 4 :(得分:0)
这是一个有效的解决方案。
-- Sample data
DECLARE @yourdata TABLE (sentence varchar(100));
INSERT @yourdata VALUES
('first name, last name : is Activated on 01/01/2017'),
('first name, last name : is Downloaded on 01/01/2017'),
('first name, last name : is Deleted on 01/01/2017'),
('first name, last name : is Deactivated on 01/01/'),
('first name, last name : is Born on 01/01/2017');
--Solution
SELECT sentenceNew = SUBSTRING(sentence, 1, PATINDEX('% on [0-9][0-9]/%', sentence)-1)
FROM @yourdata;
结果:
-----------------------------------------
first name, last name : is Activated
first name, last name : is Downloaded
first name, last name : is Deleted
first name, last name : is Deactivated
first name, last name : is Born