当前位置:首页 » 编程语言 » sql中一列拆分多列
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql中一列拆分多列

发布时间: 2022-09-18 01:04:34

sql server表中数据一列再分两列,如时间本来是一列,现在要将其分为3列

select YEAR(时间) as 年,MONTH(时间) 月,DAY(时间) as 日
from table

不删除时间行:这个做不到,那个是Excel中做表的时候,手工做出来的。

② 在SQL中如何把一列字符串拆分为多列,请高手

--首先,你是按什么规则拆? 我举个例子 你要按字段中的逗号拆开,假设字段名叫text--用charindex和substring这2个函数 select substring(text,1,charindex(',',text)-1) as [before], substring(text,charindex(',',text),len(text)) as [after] from table

③ sql一列如何分成多列在视图中显示出来

分隔字符串代码 用','分隔
用下面的函数
create function f_splitstr(@SourceSql varchar(8000),@StrSeprate varchar(100)) returns @temp table(F1 varchar(100)) as begin declare @ch as varchar(100) set @SourceSql=@SourceSql+@StrSeprate while(@SourceSql<>'') begin set @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1) insert @temp values(@ch) set @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'') end return end

④ sql 一列如何按条件分成多列

------------------------------------------------------------------------
-- author:jc_liumangtu(【DBA】小七)
-- date: 2010-03-26 09:37:30
-- version:
-- Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
-- Oct 14 2005 00:33:37
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
------------------------------------------------------------------------use test
set nocount on
if object_id('test4','U') is not null
drop table test4
go
create table test4
(item int ,date char(8),type char(2),quty int)
insert into test4
select 1000 , '20100101', 'A' , 100 union all
select 2000 , '20100101' ,'B' , 200 union all
select 1000 , '20100101' ,'C' , 100 union all
select 2000 , '20100101' , 'D' , 100
select * from test4select item ,date
,max(case type when 'A' then quty else 0 end) [typeA]
,max(case type when 'B' then quty else 0 end) [typeB]
,max(case type when 'C' then quty else 0 end) [typeC]
,max(case type when 'D' then quty else 0 end) [typeD]
from test4 group by item,date
item date type quty
----------- -------- ---- -----------
1000 20100101 A 100
2000 20100101 B 200
1000 20100101 C 100
2000 20100101 D 100item date typeA typeB typeC typeD
----------- -------- ----------- ----------- ----------- -----------
1000 20100101 100 0 100 0
2000 20100101 0 200 0 100

⑤ sql 一列如何按条件分成多列

有些乱啊··

select case 列名 when '条件' then 值 end as 别名 from 表名
例:
select case name when 'aaa' then 200 end as ccc from people

自己试试吧

⑥ sql如何将一张表中的一列数据分为两列显示

目前找到一个办法。

select id,max(money),sum(money)-max(money)
from tt
group by id

⑦ 在SQL中怎么把一列字符串拆分为多列,请高手赐教

--首先,你是按什么规则拆?我举个例子你要按字段中的逗号拆开,假设字段名叫text
--用charindex和substring这2个函数
selectsubstring(text,1,charindex(',',text)-1)as[before],substring(text,charindex(',',text),len(text))as[after]fromtable

⑧ 在sql中如何把一列的值拆分成多列,求高手指教

其实可以理解为,多列取同一个字段的值。

⑨ sql查询中,如何将某列 分成 两列。

SELECT PAccM33g02,

CASE PAccM33g02

WHEN 0 THEN PAccM33g02 END PAccM33g02_J,

CASE PAccM33g02

WHEN 1 THEN PAccM33g02 END PAccM33g02_C

FROM PAccM3307