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

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

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

問題描述

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

之前

<前>/------------------------------\|產品描述 ||------------------------------||DB1 - DB2 - DB3 ||數據位1 - 數據位2 - 數據位3|\------------------------------/

之后

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

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

解決方案

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

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!

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

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

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
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屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 久久久不卡网国产精品一区 | 免费观看一级特黄欧美大片 | 国产午夜影院 | 免费黄视频网站 | 日韩综合网 | 国产精品精品视频一区二区三区 | 色婷婷亚洲| av中文在线 | 国产福利在线 | 国产伦精品一区二区三区四区视频 | av网站免费看 | 精品久久99 | 亚洲成人黄色 | 祝你幸福电影在线观看 | 色婷婷综合久久久中文字幕 | 国产精品永久久久久 | 少妇一级淫片免费放播放 | 99视频免费在线 | 我爱操| 国产成人一区二区三区精 | 粉嫩高清一区二区三区 | 人人玩人人添人人澡欧美 | 日韩a在线| 国产一级片在线观看视频 | 国产亚洲一区二区三区在线 | 综合五月| 精品国产乱码久久久久久88av | 日韩在线小视频 | 亚洲图片一区二区三区 | 中文字幕一区二区三区在线观看 | 久久国内 | 精品久久久久一区二区国产 | 日韩视频在线播放 | 精品久久久久久久久久久久 | 麻豆av免费观看 | 国产日韩欧美激情 | 美女天堂 | 在线播放国产视频 | 亚洲欧美日韩在线 | 国产成人福利 | 白浆在线 |