简体   繁体   English

将月份数转换为日期字符串

[英]Converting a number of months to date string

I have a number of months. 我有好几个月了。

I wish to represent them as a number of years and months in the format YYMM 我希望用YYMM格式将它们表示为若干年和几个月

eg 例如

5 -> 0005
13 -> 0101
24 -> 0200

Does anyone know of a non-convoluted way to do this in Oracle SQL? 有人知道在Oracle SQL中采用非卷积方式进行此操作吗?

A little bit of maths and some formatting are the way to go: 一些数学和一些格式化是要走的路:

with sample_data as (select 5 num from dual union all
                     select 13 num from dual union all
                     select 24 num from dual union all
                     select 1400 num from dual)
select to_char(floor(num/12), 'fm9999909')||to_char(mod(num, 12), 'fm09') yrmn
from   sample_data;

YRMN       
-----------
0005       
0101       
0200       
11608    

I included one that had more than 100 years just to show you how it might look; 我写了一篇已有100多年历史的书,只是为了向您展示它的外观。 I don't know if that's a possibility in your case, or if you'd want the other values to be zero-padded since you didn't say. 我不知道您是否有这种可能,或者您是否想将其他值填充为零(因为您未说)。

select lpad(trunc(yourValue/12),2,'0')||lpad(mod(yourValue,12),2,'0') result
  from dual

try this; 尝试这个;

select right("0" + convert(varchar(20), convert(numeric(10,0), 37/12)), 2)
                 + right("0" + convert(varchar(20), 37%12), 2)

0301 

I used "37" for example. 我以“ 37”为例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM