SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Location
    UK
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert nvarchar to smalldatetime

    HI folks

    I have tried to search for this but no luck so far.

    I have a table that stores a date in the format dd/yy/yyyy but it is a nvarchar(50) instead of, for example, smalldatetime. The column is also named date.

    I am unable to change the column to a smalldatetime because there are a large number of Classic ASP pages linking to this and it would mean a lot of work.

    OK my problem is there is some SQL like:

    SELECT t.id, t.course, t.title, MAX(d.[date]) AS date FROM tbl_training t LEFT OUTER JOIN tbl_training_dates d ON t.id = d.event_id WHERE t.cat_id = 3 AND t.visible = 1 AND d.date > getdate() GROUP BY t.id, t.course, t.title ORDER BY t.course

    Obviously this doesnt work because the date column isnt actually a date. I've been trying to change the MAX(d.[date]) to something like:

    MAX(CAST(d.date AS smalldatetime))

    ...but still get an error on my ASP page that says:

    [Microsoft][ODBC SQL Server Driver][SQL Server]Arithmetic overflow error converting expression to data type datetime.

    Any ideas how to successfully convert this properly? Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what version of SQL server you use. if you use 2008 r2 try this function CONVERT
    CAST and CONVERT (Transact-SQL)

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    use MAX(CONVERT(DATETIME,d.date,103))
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •