如何将 varchar 字符串解析为三列,年、月和日 [英] How can I parse a varchar string into three columns, Year, Month and Day

查看:56
本文介绍了如何将 varchar 字符串解析为三列,年、月和日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出一种解决格式不佳的表的方法是将日期"列解析为三列,之后我可以正确运行查询.日期格式为ddmmmyyyy"样式 (31DEC2013).

如何将此字符串解析为三个不同的列日"、月"、年".<​​/p>

提前致谢

解决方案

DATEPART 命令可以为您做到这一点.例如:

select datepart(day, '31DEC2013')选择日期部分(月,'31DEC2013')选择日期部分(年份,'31DEC2013')

我建议您只将日期保留在一列中,并在查询中即时使用 datepart.

I figured one way to solve my poorly formatted table would be to parse the "date" column in to three columns after which i can properly run queries on. The date format is 'ddmmmyyyy' style (31DEC2013).

How can I parse this string into three distinct columns "day", "month", "year".

Thanks in advance

解决方案

The DATEPART command can do that for you. For example:

select datepart(day, '31DEC2013')
select datepart(month, '31DEC2013')
select datepart(year, '31DEC2013')

I would recommend just leaving your date in one column, and using datepart on the fly in your queries.

这篇关于如何将 varchar 字符串解析为三列,年、月和日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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