SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 integer to string
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sfjtraps
Yak Posting Veteran

63 Posts

Posted - 06/19/2009 :  16:33:38  Show Profile  Reply with Quote
can I select an integer from SQL and convert it to a string in the SELECT statement? If anyone knows a syntax for this it would be greatly appreciated. Thanks

vijayisonly
Flowing Fount of Yak Knowledge

USA
1836 Posts

Posted - 06/19/2009 :  16:37:39  Show Profile  Reply with Quote
select convert(varchar,<urcolumn>) from <table>
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 06/20/2009 :  01:49:46  Show Profile  Reply with Quote
also make sure you specify an explicit length for varchar

select convert(varchar(length),<urcolumn>) from <table>
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 06/20/2009 :  01:53:25  Show Profile  Reply with Quote
also see
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx
Go to Top of Page

Jeff Moden
Aged Yak Warrior

USA
649 Posts

Posted - 06/21/2009 :  18:57:01  Show Profile  Reply with Quote
Now that you have the conversion, please tell us, what are you going to do with the result?

--Jeff Moden
"Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! "
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row"

For better, quicker answers, click on the following... http://www.sqlservercentral.com/articles/Best Practices/61537/
Go to Top of Page

KMontz
Starting Member

USA
1 Posts

Posted - 04/22/2013 :  15:22:08  Show Profile  Reply with Quote
In case anyone is looking for the way to get numbers from a mixed string:
select dbo.UDF_ParseNumbers('ABCD1234') will yield 1234. Here's a UDF that works:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[UDF_ParseNumbers]
(
@string VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @IncorrectCharLoc SMALLINT
SET @IncorrectCharLoc = PATINDEX('%[^0-9]%', @string)
WHILE @IncorrectCharLoc > 0
BEGIN
SET @string = STUFF(@string, @IncorrectCharLoc, 1, '')
SET @IncorrectCharLoc = PATINDEX('%[^0-9]%', @string)
END
SET @string = @string
RETURN @string
END

Keith

Edited by - KMontz on 04/22/2013 15:23:34
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000