Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 converting char to int

Author  Topic 

Bjcascone
Starting Member

37 Posts

Posted - 2008-12-19 : 08:56:34
how would i convert data that is stored as char in to int values?

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-12-19 : 08:57:59
using CAST or CONVERT functions
Go to Top of Page

Bjcascone
Starting Member

37 Posts

Posted - 2008-12-19 : 09:00:19
what is the difference between the 2?
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-12-19 : 09:15:47
quote:
Originally posted by Bjcascone

what is the difference between the 2?



I can think of just once difference,CONVERT has the third parameter "style" which can be used when converting datetime to string.
Go to Top of Page

JamesRyan
Starting Member

18 Posts

Posted - 2008-12-19 : 09:45:37
from: http://searchwinit.techtarget.com/tip/0,289483,sid1_gci1014050,00.html#

SQL Server programmers can choose between two functions in SQL Server 7 and 2000 for converting expressions from one type to another. In many cases there will be a need within a stored procedure or other routine to convert data from, say, a datetime type to a varchar type; CONVERT and CAST are used for such things.

Because SQL Server provides both functions, there may be some confusion about which is best to use and under what circumstances. CONVERT is specific to SQL Server, and allows for a greater breadth of flexibility when converting between date and time values, fractional numbers, and monetary signifiers.

CAST is the more ANSI-standard of the two functions, meaning that while it's more portable (i.e., a function that uses CAST can be used in other database applications more or less as-is), it's also less powerful. CAST is also required when converting between decimal and numeric values to preserve the number of decimal places in the original expression. For those reasons, it's best to use CAST first, unless there is some specific thing that only CONVERT can provide in the work you're doing.

CAST and CONVERT can also be used in conjunction with each other to achieve certain effects. For instance, a typical way to produce a char variable with the current date would be to use:

SELECT CONVERT(CHAR(10), CURRENT_TIMESTAMP, 102)

(The 102 indicates that the ANSI date format, yy.mm.dd, is to be used.)

However, if you wanted to cast this variable explicitly as a datetime or smalldatetime value for compatibility in a specific database column, you could use:

SELECT CAST(CONVERT(CHAR(10),CURRENT_TIMESTAMP,102) AS DATETIME

This would return the value yy.mm.dd 00:00:00 (i.e., 12:00AM as the timestamp; the time information from CURRENT_TIMESTAMP would be discarded).



James Ryan
www.sqlhowto.co.uk
Go to Top of Page
   

- Advertisement -