SQL将字符串MMM.YY转换为日期 [英] SQL Converting string MMM.YY to date

查看:176
本文介绍了SQL将字符串MMM.YY转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换/转换列包含字符串Jan.08,Feb.08 ..进入日期格式,以便我可以对它们进行排序?

how do i convert/cast a column contains strings e.g. Jan.08,Feb.08.. into date format so that i can sort them?

最好的谢谢!

推荐答案

我将格式化为相关月份的第一个可转换字符串,然后转换为日期时间,例如

I'd just format as a convertible string for the first of the relevant month, and then cast to datetime, e.g.

CAST('1.' + YourMonthAndYearColumnName AS DATETIME)

...是一个表达式,将产生应该可排序的日期时间,因此:

...is an expression that will yield a datetime that should be sortable, so:

SELECT
  YourMonthAndYearColumnName
FROM
  YourTable
ORDER BY
  CAST('1.' + YourMonthAndYearColumnName AS DATETIME)

...应该做你想要的。

...should do what you're looking for.

这篇关于SQL将字符串MMM.YY转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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