Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number to string conversion

Status
Not open for further replies.

teddysnow12

Programmer
Aug 12, 2011
30
US
Hi,

I have a field , end_yr_mnth which has a value like 201103 (datatype is number)


I have other field rptg_yyyymm which has a value like 2011-03
(datatype is string and format is yyyy-mm)


I need a syntax for converting either number into string or string into number to match these two fields.
 
Check CAST() or CONVERT() functions in BOL.
If you want to convert string to number you must remove the hyphen, so check REPLACE() function also :)

Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
It's probably safer to convert the number to a string because ALL numbers can be converted. Not all strings can be converted to a number.

To convert to a string and insert the hypen...

Code:
Declare @Test Int

Set @Test = 201103

Select Stuff(Convert(VarChar(6), @Test), 5, 0, '-')



-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top