avatar
一个关于T-SQL的问题# Database - 数据库
B*a
1
Need to convert a varchar column to datetime format.
The column is like:
monthid
200811
200711
200610
200510
I need them to be like
2008/11/15
2007/11/15
2006/10/15
2005/10/15
i.e., convert it to the 15th date of the month...how can I do this in T-SQL?
thanks a lot!!!
avatar
j*n
2
SELECT convert(datetime, monthid + '15')
FROM table
avatar
B*a
3
this doesnot work...
e.g., it returns 2449-07-26 for 200710

【在 j*****n 的大作中提到】
: SELECT convert(datetime, monthid + '15')
: FROM table

avatar
j*n
4
en, then try this:
SELECT convert(datetime, convert(int, monthid + '15'))
FROM table

【在 B****a 的大作中提到】
: this doesnot work...
: e.g., it returns 2449-07-26 for 200710

avatar
B*a
5
thanks for your reply...but it is still the same...

【在 j*****n 的大作中提到】
: en, then try this:
: SELECT convert(datetime, convert(int, monthid + '15'))
: FROM table

avatar
j*n
6
shot, then go this way...
SELECT convert(datetime, left(monthid,4) + '/' + right(monthid,2) + '/15')
FROM table
avatar
B*a
7
OMG, it works this time!! :)
thanks a lot for your time and patience! Thanks!!!!

【在 j*****n 的大作中提到】
: shot, then go this way...
: SELECT convert(datetime, left(monthid,4) + '/' + right(monthid,2) + '/15')
: FROM table

avatar
n*6
8
substring()
updates: Use convert.
Sorry. Should use convert (to datetime format). Jackrun is right. Substring
will return varchar type.

【在 B****a 的大作中提到】
: Need to convert a varchar column to datetime format.
: The column is like:
: monthid
: 200811
: 200711
: 200610
: 200510
: I need them to be like
: 2008/11/15
: 2007/11/15

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。