string split function in postgreSQL 17.01 or SQL Server2019 or mySQL 9.0

sql 复制代码
SELECT 'S0001' AS STU,
  unnest(string_to_array('G,E,O,V,I,N,D,U,B,,A,B,C,D,A,B,D,B,C,C,B,A,B,D,A,C,D,A,B,D,A,D,C,B,D,B,D,B,A,C,D,A,C,D,A,A,C,B,A,D', ',')) AS parts;
  
delete from Answer where AnswerSudentId='S0001';
drop table IF EXISTS TempSubString;
CREATE TABLE TempSubString(
id SERIAL,
studentid varchar(5),
subname VARCHAR(150),
PRIMARY KEY(id) 
);

insert into TempSubString(studentid,subname) 
SELECT 'S0001' AS STU,unnest(string_to_array('A,,B,C,D,A,B,D,B,,A,B,C,D,A,B,D,B,C,C,B,A,B,D,A,C,D,A,B,D,A,D,C,B,D,B,D,B,A,C,D,A,C,D,A,A,C,B,A,D', ',')) AS parts;
--  每题的得分 考虑,是否存在记录,存在删除,不存在,直接添加 Geovin Du
--  delete from  Answer where AnswerSudentId=@studentid;
insert into Answer(AnswerSudentId,AnswerQuestionId,AnswerStudentResult,AnswerScore) select studentid,id,subname,f_GetAnswerScore(id,subname) as score from TempSubString ; 

SELECT * FROM Answer;

sql server 2019:

sql 复制代码
-- 测试结果
DECLARE @studentid CHAR(5),@result NVARCHAR(1000)
set @studentid='S0001';  -- 学生编号
set @result='A,C,D,B,A,D,C,B,C,D,A,B,C,D,A,B,D,B,C,C,B,A,B,D,A,C,D,A,B,D,A,D,C,B,D,B,D,B,A,C,D,A,C,D,A,A,C,B,A,C';
EXEC procImportStudentGrade @studentid,@result;

mysql 9.0

sql 复制代码
set @studentid='S0001';  #学生编号
set @result='A,,B,C,D,A,B,D,B,,A,B,C,D,A,B,D,B,C,C,B,A,B,D,A,C,D,A,B,D,A,D,C,B,D,B,D,B,A,C,D,A,C,D,A,A,C,B,A,D';
insert into temptable(studentid,subname)
SELECT @studentid,SUBSTRING_INDEX(SUBSTRING_INDEX(@result,',',help_topic_id+1),',',-1) AS num 
FROM mysql.help_topic 
WHERE help_topic_id < LENGTH(@result)-LENGTH(REPLACE(@result,',',''))+1;


SELECT SUBSTRING_INDEX('A,,B,C,D',',',1);
SELECT SUBSTRING_INDEX('A,,B,C,D',',',2);
SELECT SUBSTRING_INDEX('A,,B,C,D',',',-2);
SELECT SUBSTRING_INDEX('A,,B,C,D',',',-1);

SELECT SUBSTRING_INDEX('Ann Smith', ' ', 1);
SELECT LOCATE('.', 'www.dusystem.com');

set @skills='MySQL,PostgreSQL,SQLite';
SELECT SUBSTRING_INDEX(@skills, ',', 1) AS skill_1,
    SUBSTRING_INDEX(SUBSTRING_INDEX(@skills, ',', 2), ',', -1) AS skill_2,
    SUBSTRING_INDEX(SUBSTRING_INDEX(@skills, ',', 3), ',', -1) AS skill_3;
    
相关推荐
zhangshengqiang1683 分钟前
Linux下安装mysql8.0版本
linux·mysql
Karoku06616 分钟前
【企业级分布式系统】ELK-企业级日志分析系统
运维·数据库·redis·mysql·elk·缓存
小小宇宙中微子18 分钟前
mysql 命名约束
数据库·sql·oracle
linweidong34 分钟前
MariaDB面试题及参考答案
linux·运维·数据库·负载均衡·dba·mariadb·后端面试
FLGB37 分钟前
Docker 安装单机版mysql 并持久化数据
mysql·docker·容器
PGCCC3 小时前
【PGCCC】Postgresql 缓存替换算法
数据库·缓存·postgresql
盖盖衍上3 小时前
4. SQL视图
数据库·sql
枫哥和java5 小时前
python serializer, model drf通过序列化器, 模型获取mysql 一张表某个字段数据库现存的最大值
数据库·python·mysql
敲敲敲-敲代码6 小时前
【SQL实验】索引操作(菜单操作和命令操作)
数据库·sql·sqlserver