SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


about casting string date value to datetme


about casting string date value to datetme

Author
Message
sa.ordekci
sa.ordekci
SSC Eights!
SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)SSC Eights! (870 reputation)

Group: General Forum Members
Points: 870 Visits: 108
Hi. I use Sql Server 2008.
I have a column that is varchar. Some of values are integer valueand some of valus aer datetime value. For example, year column datas like below:
2015
2016
12.4.2016
2.2.2017
2017
....
this field is varchar type. want to show this column only year values like
2015
2016
2016
2017
2017
.....
.How can I control tihs?
thanks in advance.
J Livingston SQL
J Livingston SQL
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45998 Visits: 41777
sa.ordekci - Wednesday, December 20, 2017 3:52 AM
Hi. I use Sql Server 2008.
I have a column that is varchar. Some of values are integer valueand some of valus aer datetime value. For example, year column datas like below:
2015
2016
12.4.2016
2.2.2017
2017
....
this field is varchar type. want to show this column only year values like
2015
2016
2016
2017
2017
.....
.How can I control tihs?
thanks in advance.

if the year is always four digits and always at the end of the string....as per your sample data then use RIGHT(<your column>,4)


________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (135K reputation)SSC Guru (135K reputation)SSC Guru (135K reputation)SSC Guru (135K reputation)SSC Guru (135K reputation)SSC Guru (135K reputation)SSC Guru (135K reputation)SSC Guru (135K reputation)

Group: General Forum Members
Points: 135234 Visits: 19483
Use a string splitter to split each date into its individual elements, and keep those that have four characters and can be converted to a number between, say, 1950 and 2050.

Please tell me you're doing this as a one-off to clean your data and that you're not persisting with storing date values in that way?

John
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search