How To Alter a datetime column to Numeric

  • I need to alter few columns which is datetime column, So wa alter these columns to numeric but query given this error "Implicit conversion from data type datetime to numeric is not allowed. Use the CONVERT function to run this query. Query is like below

    Alter Table FPM_IDDWBCPaymentInputHeader Alter Column FileDate numeric(6)

    Please help to sort out this problem

    Thanks

    Kd

  • This looks like very odd. Why do you need to convert datetime to numeric?

  • and numeric 6??? Are you trying to store mmddyy instead of the actual datetime?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I had fix the problemmm.

    As per my understanding we can't alter a datetime column directly into numeric type. So I convert frist into varchar(8) then to Numeric.

    Thanks for your reply..................

  • can you please share your code?

  • If column is Numeric then frist convert to varchar(8)

    Alter table <Table name> Alter column <COlumn Name> varchar(8)

  • panwar.jt (12/21/2010)


    I had fix the problemmm.

    As per my understanding we can't alter a datetime column directly into numeric type. So I convert frist into varchar(8) then to Numeric.

    Thanks for your reply..................

    Panwar, would you mind sharing the business need you had for this?

    What you've posted doesn't add up to me as a requirement. For example:

    DECLARE @dateVar DATETIME

    SET @dateVar = GETDATE()

    DECLARE @varcharVar VARCHAR(8)

    SET @varcharVar = CONVERT( VARCHAR(8), @dateVar)

    DECLARE @numVar NUMERIC(6)

    SET @numVar = @varcharVar

    PRINT @dateVar

    print @varcharVar

    PRINT @numVar

    Comes up with this:

    Msg 8114, Level 16, State 5, Line 8

    Error converting data type varchar to numeric.

    Some manipulation to get this to behave in any method...

    DECLARE @dateVar DATETIME

    SET @dateVar = GETDATE()

    DECLARE @varcharVar VARCHAR(8)

    SET @varcharVar = CONVERT( VARCHAR(8), @dateVar, 121)

    DECLARE @numVar NUMERIC(6)

    SET @numVar = REPLACE( @varcharVar, '-', '')

    PRINT @dateVar

    print @varcharVar

    PRINT @numVar

    Produces:

    Jan 5 2011 2:15AM

    2011-01-

    201101

    Giving you, at best, a year/month code. If that's your requirement, there's more precise ways to arrive at the value.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

Viewing 7 posts - 1 through 6 (of 6 total)

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