久久久久久久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观看| 午夜精品久久久久久久久久久久久 | 久久精品日 | 亚洲欧美综合 | 亚欧精品一区 | 黄色一级毛片免费看 | 久久久妇女国产精品影视 | 亚洲欧美一区二区三区情侣bbw | 亚洲精品一区二区三区中文字幕 | 久久久久国产精品一区二区 | 国产精品国产三级国产aⅴ入口 | 精品国产99 | 涩涩导航 | 成人av播放 | 精品久久久久久中文字幕 | 日韩中文字幕在线不卡 | 风间由美一区二区三区在线观看 | 日韩高清不卡 | 91免费在线视频 | 亚洲女人天堂成人av在线 | 国产精品高清在线 | 99这里只有精品 | 亚洲精品视频免费观看 | 久久精品中文字幕 | 91porn国产成人福利 | 亚洲精品永久免费 | 亚欧洲精品在线视频免费观看 | 亚洲五码久久 | av中文在线播放 | 九九久久久 | 人人爽人人草 | 高清视频一区二区三区 | 综合国产第二页 | 一级免费视频 | 一本一道久久a久久精品蜜桃 | 国产午夜在线 | 五月综合久久 |