Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How To Alter a datetime column to Numeric


How To Alter a datetime column to Numeric

Author
Message
panwar.jt
panwar.jt
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 285
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
Latheesh NK
Latheesh NK
Right there with Babe
Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)

Group: General Forum Members
Points: 739 Visits: 2156
This looks like very odd. Why do you need to convert datetime to numeric?
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16667 Visits: 17030
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)
panwar.jt
panwar.jt
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 285
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..................
codemihir1
codemihir1
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 100
can you please share your code?
panwar.jt
panwar.jt
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 285
If column is Numeric then frist convert to varchar(8)
Alter table <Table name> Alter column <COlumn Name> varchar(8)
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5715 Visits: 7660
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search