久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

MSSQL - 將一個字段拆分為 3 個字段

MSSQL - Split a field into 3 fields(MSSQL - 將一個字段拆分為 3 個字段)
本文介紹了MSSQL - 將一個字段拆分為 3 個字段的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個由 1 列組成的結(jié)果集,在本例中為 2 行,單列 [ProductDescription] 是一個 varchar 字段,其中包含 3 條信息(我沒有設(shè)計(jì)它)我需要獲取這三條信息使用查詢分為 3 個附加字段

之前

<前>/------------------------------\|產(chǎn)品描述 ||------------------------------||DB1 - DB2 - DB3 ||數(shù)據(jù)位1 - 數(shù)據(jù)位2 - 數(shù)據(jù)位3|\------------------------------/

之后

<前>/---------------------------------------------------------\|字段 1 |字段 2 |字段 3 |產(chǎn)品描述 ||---------------------------------------------------------||DB1 |DB2 |DB3 |DB1 - DB2 - DB3 ||DataBit1|DataBit2|DataBit3|DataBit1 - DataBit2 - DataBit3|\---------------------------------------------------------/

我曾嘗試使用 Substring 和 charindex 的組合,但未能完全正確,字段的每個部分都可以是任意長度,因此使用硬編碼偏移不起作用.

解決方案

這并不漂亮,但它確實(shí)有效,并且確實(shí)為您提供了您正在尋找的內(nèi)容,針對您的特定情況...如果您有一個可變數(shù)字ProductDescription 中的令牌數(shù)量,您可能需要創(chuàng)建一個存儲過程來在解析字符串時管理您的狀態(tài),因?yàn)檫@會很快變得難以管理.

create table #table(productdescription varchar(255))走/* 演示它在漂亮"的情況下工作 */INSERT INTO #TABLE (ProductDescription) 值 ('abc - def - ghi')走/* 演示它在丑陋"的情況下工作 */插入 #table (ProductDescription) 值 ('jklsaf -mnoa-psdfaqr')走SELECT RTRIM(LTRIM(SUBSTRING(ProductDescription, 0, CHARINDEX('-', ProductDescription)-1))) 作為 Field1,RTRIM(LTRIM(SUBSTRING(ProductDescription, CHARINDEX('-', ProductDescription)+1, (CHARINDEX('-', ProductDescription, CHARINDEX('-', ProductDescription)+1)) - (CHARINDEX('-', ProductDescription)+1)))) 作為 Field2,RTRIM(LTRIM(SUBSTRING(ProductDescription, CHARINDEX('-', ProductDescription, CHARINDEX('-', ProductDescription)+1)+1, LEN(ProductDescription)))) as Field3從#table走

我希望這會有所幫助!

I have a resultset consisting of 1 column and in this case 2 rows the single column [ProductDescription] is a varchar field that hold 3 pieces of information (I didn't design it) i need to get those three pieces of information out into 3 additional fields using a query

before

/------------------------------\
|ProductDescription            |
|------------------------------|
|DB1 - DB2 - DB3               |
|DataBit1 - DataBit2 - DataBit3|
\------------------------------/

After

/---------------------------------------------------------\
|Field1  |Field2  |Field3  |ProductDescription            |  
|---------------------------------------------------------|  
|DB1     |DB2     |DB3     |DB1 - DB2 - DB3               |  
|DataBit1|DataBit2|DataBit3|DataBit1 - DataBit2 - DataBit3|  
\---------------------------------------------------------/

I have tried using combinations of Substring and charindex but haven't been able to get it quite right, each part of the field could be any length so using hardcoded offsets doesn't work.

解決方案

This isn't pretty, but it works and it does give you what you are looking for, for your specific case... If you had a variable number of tokens in your ProductDescription, you would probably need to create a stored proc to manage your state while parsing the string, as this would quickly grow unmanageable.

create table #table(productdescription varchar(255))
go
/* Demonstrate it working in a 'pretty' case */
INSERT INTO #TABLE (ProductDescription) values ('abc - def - ghi')
go

/* Demonstrate it working in a 'ugly' case */
insert into #table (ProductDescription) values ('jklsaf -mnoa-psdfaqr')
go

SELECT RTRIM(LTRIM(SUBSTRING(ProductDescription, 0, CHARINDEX('-', ProductDescription)-1))) as Field1,

RTRIM(LTRIM(SUBSTRING(ProductDescription, CHARINDEX('-', ProductDescription)+1, (CHARINDEX('-', ProductDescription, CHARINDEX('-', ProductDescription)+1)) - (CHARINDEX('-', ProductDescription)+1)))) as Field2,

RTRIM(LTRIM(SUBSTRING(ProductDescription, CHARINDEX('-', ProductDescription, CHARINDEX('-', ProductDescription)+1)+1, LEN(ProductDescription)))) as Field3
FROM #table
go

I hope this helps!

這篇關(guān)于MSSQL - 將一個字段拆分為 3 個字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標(biāo)記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚洲毛片网 | 91精品国产综合久久久久久 | 久久人体视频 | 国产精品美女久久 | 日本免费网站 | 欧美成人a | 欧美日韩专区 | 久久av红桃一区二区小说 | 国产伦精品一区二区三区视频网站 | 国产一级视频在线观看 | 日韩成人一区二区 | 免费av小说| 视色网| 日韩高清不卡 | 国产乱码久久久久久 | 欧美日韩亚洲视频 | 狠狠干狠狠插 | 一级大片免费看 | 成人aa| 波多野结衣乳巨码无在线观看 | 亚洲第一色 | 久久一二三区 | 毛片视频网站 | 黄色网址av | 免费在线成人网 | 色婷婷国产 | 三级av片 | 黄色一级视频网站 | 午夜免费观看视频 | aaa一级片| 欧美亚洲国产精品 | 国产精品日韩在线 | 免费啪视频 | 色婷婷网 | 久久九九免费视频 | 亚洲国产中文字幕 | 一区二区三区四区在线视频 | 精品一区在线 | 国产欧美日韩在线视频 | 成人精品免费视频 | 国产日韩欧美日韩大片 |