2

我的column表格中有 a ,其中包含文本(字符串),例如19.57.14,但也只有1911

我想将文本拆分为2 列1 与“。”之前的所有文本。和 1 在“。”之后的所有文本。.

对于没有. _ 在字符串中,表的第二列必须有00。也是之后的所有数据必须是 2 个字符长(用 0 填充)。

例如19.5必须给column1: 19 column2: 5011必须给column1: 11 column2: 00 任何人都可以帮助我正确的 tsql 代码吗?

4

3 回答 3

4

请试试:

select 
    YourCol, 
    SUBSTRING(YourCol, 0, CHARINDEX('.', YourCol+'.')) Col1, 
    LEFT(SUBSTRING(YourCol, CHARINDEX('.', YourCol+'.')+1, 2)+'00', 2) Col2 
from YourTable

或者

select
    YourCol,
    CAST(YourCol AS INT) Col1,
    RIGHT(PARSENAME(YourCol,1), 2) Col2
from
(
    select 
        CONVERT(NUMERIC(18,2), YourCol) YourCol
    from YourTable
)x

样本:

declare @tbl as table(txt nvarchar(10))
insert into @tbl values ('19.5'), ('11'), ('7.14')

select 
    txt, 
    SUBSTRING(txt, 0, CHARINDEX('.', txt+'.')) Col1, 
    LEFT(SUBSTRING(txt, CHARINDEX('.', txt+'.')+1, 2)+'00', 2) Col2 
from @tbl

或者

select
    txt,
    CAST(txt AS INT) Col1,
    RIGHT(PARSENAME(txt,1), 2) Col2
from
(
    select 
        CONVERT(NUMERIC(18,2), txt) txt
    from @tbl
)x
于 2013-05-06T09:02:31.257 回答
0
select cast(cast(number as float) as int) column1,
right(cast(number as numeric(9,2)), 2) column2
from
(VALUES ('19.5'),('7.14'),('19'),('11') ) t(number)
于 2013-05-06T09:04:08.090 回答
0

尝试这个,

Declare @InputList VARCHAR(8000), @Delimiter VARCHAR(8000);
Declare @1stvalue nvarchar(20), @2ndValue Int;

Set @InputList = '19.5';
Set @Delimiter = '.';

set @1stvalue = (select RTRIM(LTRIM(SUBSTRING(@InputList,1,CHARINDEX(@Delimiter,@InputList,0)-1))));

set @2ndValue = (Select RTRIM(LTRIM(SUBSTRING(@InputList,CHARINDEX(@Delimiter,@InputList,0)+LEN(@Delimiter),LEN(@InputList)))) )
set @2ndValue = (select case when @2ndValue < 10 then convert (varchar(10) ,convert (varchar(10),@2ndValue) + '0') else convert (varchar(10), @2ndValue) end);

SELECT @1stvalue AS Column1, @2ndValue AS Column2;

您可以将其用作功能..

于 2013-05-06T09:08:41.530 回答