這應該工作,雖然它可能不是太有效率...
SQL Fiddle
MS SQL Server 2012的架構設置:
CREATE TABLE Table1
(the_string varchar(21))
;
INSERT INTO Table1
(the_string)
VALUES
('XYZ100320C00027500'),
('ABCDEF120410C00022540')
;
查詢1:
SET DATEFORMAT ymd
SELECT
SUBSTRING(the_string, 0, PATINDEX('%[0-9]%', the_string)) AS [Symbol],
CAST(SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string), 2) + '-' + SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string)+2, 2) + '-' + SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string)+4, 2) AS DATE) AS [Date],
SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string)+6, 1) AS [Call/Put],
CAST(SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string)+7, 5) AS FLOAT) + CAST(SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string)+12, 3) AS FLOAT)/1000 AS [Strike]
FROM table1
Results:
| SYMBOL | DATE | CALL/PUT | STRIKE |
|--------|------------|----------|--------|
| XYZ | 2010-03-20 | C | 27.5 |
| ABCDEF | 2012-04-10 | C | 22.54 |
來源
2014-04-05 12:02:14
jpw
什麼DB引擎你用? –
我正在使用SQL,我對它很新。我真的不知道該怎麼嘗試,這就是爲什麼我要求任何幫助或方向。如示例中提到的 – user3368353
,除符號之外的所有其他字段將包含固定長度值。意味着它會發生「罷工美元」限制高達5個字符,但它只有「27」被排除在「00027」之外。 – AK47