about casting string date value to datetme

  • 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.

  • 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

  • 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

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply