如何在SQL中拆分数字值 [英] How to split number values in SQL

查看:240
本文介绍了如何在SQL中拆分数字值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



如何在SQL查询中拆分下面的整数值



月/年份

012013

022013

122013



我可以拆分122013截至2013年12月但我无法拆分012013​​和022013​​值。



查询我过去常常拆分。



选择左(ID,2)作为月,右(ID,4)作为年份从表格



问候,

Abhishek

解决方案

当您将数字读取为整数值时,将删除前导零。您需要将它们作为字符串(varchar)进行操作。


要拆分整数值,您需要将其视为文本:

  SELECT   LEFT  CONVERT  VARCHAR  10 ),ID), 2  as 月, RIGHT  CONVERT  VARCHAR  10 ),ID), 4 作为
FROM


是的,当我把列作为 Varchar 时,它运行正常。

演示



http: //sqlfiddle.com/#!6/2d4 45/2/0 [ ^ ]

Hi All,

How to split the below integer value in SQL query

Month/Year
012013
022013
122013

I am able to split 122013 as 12 2013 but am not able to split "012013" and "022013" values.

Query i used to split.

SELECT left(ID, 2) as Month , Right(ID, 4) As Years FROM table

Regards,
Abhishek

解决方案

Leading zeros are dropped when you read the number as an integer value. You would need to operate on them as strings (varchar).


TO split integer values, you need to treat it as a text:

SELECT LEFT(CONVERT(VARCHAR(10),ID), 2) as Month , RIGHT(CONVERT(VARCHAR(10),ID), 4) As Years
FROM table


Yes, when I took the column as Varchar, it is working fine.

Demo


http://sqlfiddle.com/#!6/2d445/2/0[^]


这篇关于如何在SQL中拆分数字值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆