Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How To Alter a datetime column to Numeric Expand / Collapse
Author
Message
Posted Tuesday, December 21, 2010 5:53 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, December 17, 2012 12:12 PM
Points: 76, Visits: 284
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
Post #1037652
Posted Tuesday, December 21, 2010 6:09 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Thursday, September 18, 2014 3:24 AM
Points: 731, Visits: 2,020
This looks like very odd. Why do you need to convert datetime to numeric?
Post #1037659
Posted Tuesday, December 21, 2010 7:32 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:34 PM
Points: 12,923, Visits: 12,342
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1037718
Posted Tuesday, December 21, 2010 9:36 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, December 17, 2012 12:12 PM
Points: 76, Visits: 284
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..................
Post #1038057
Posted Tuesday, January 4, 2011 10:10 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 19, 2013 9:31 AM
Points: 4, Visits: 100
can you please share your code?
Post #1042811
Posted Wednesday, January 5, 2011 1:50 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, December 17, 2012 12:12 PM
Points: 76, Visits: 284
If column is Numeric then frist convert to varchar(8)
Alter table <Table name> Alter column <COlumn Name> varchar(8)
Post #1042862
Posted Wednesday, January 5, 2011 2:16 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:15 PM
Points: 5,383, Visits: 7,454
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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1042875
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse