Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. convert nchar to datetime in sql server

convert nchar to datetime in sql server

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelp
3 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndieDu
    wrote on last edited by
    #1

    Hi, I have a nchar type in my sql with this kind of value '02 12', it stands for February 2012, is there a way that i can convert this value to DateTime data type in sql server. i was tring these, but none of them are working: *. select convert(DateTime, '02 12') *. select cast('02 12' as DateTime) any help will be greatly appreciated.

    M W 2 Replies Last reply
    0
    • A AndieDu

      Hi, I have a nchar type in my sql with this kind of value '02 12', it stands for February 2012, is there a way that i can convert this value to DateTime data type in sql server. i was tring these, but none of them are working: *. select convert(DateTime, '02 12') *. select cast('02 12' as DateTime) any help will be greatly appreciated.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Not a chance, datetime requires a minimum of d/m/y. I would pad it out with something to end up with valid date information. Pick and arbitrary day, 1 or 15 and concat the string then use a date mast to convert it. Look ad convert/cast it will give you the mask types.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • A AndieDu

        Hi, I have a nchar type in my sql with this kind of value '02 12', it stands for February 2012, is there a way that i can convert this value to DateTime data type in sql server. i was tring these, but none of them are working: *. select convert(DateTime, '02 12') *. select cast('02 12' as DateTime) any help will be greatly appreciated.

        W Offline
        W Offline
        WoutL
        wrote on last edited by
        #3

        You will have to convert your string in a valid datestring first. This should work:

        declare @d as nchar(5)

        set @d = '02 12'

        select Cast('20'+ Right(@d, 2) + Left(@d,2) + '01' as datetime)

        Wout Louwers

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups