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
 How to select the ascii code from a column

Author  Topic 

Studevs
Starting Member

15 Posts

Posted - 2010-02-19 : 05:50:54
Dear All,

I have a table with two columns:

name and number...the type of both columns is varchar.
now how can ik select the both columns buth the value of column number most be returned in ascii.
Please some help.

Thanks in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-19 : 05:57:34
use ASCII() function. you might have to loop over entire length of number to convert all characters to ASCII.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Studevs
Starting Member

15 Posts

Posted - 2010-02-19 : 06:01:58
Hi visakh16,

How can i loop over the length of the number column?
quote:
Originally posted by visakh16

use ASCII() function. you might have to loop over entire length of number to convert all characters to ASCII.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-19 : 06:03:13
are you using sql 2005?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-19 : 06:05:15
quote:
Originally posted by Studevs

Dear All,

I have a table with two columns:

name and number...the type of both columns is varchar.
now how can ik select the both columns buth the value of column number most be returned in ascii.
Please some help.

Thanks in advance


Post some sample data with expected result

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Studevs
Starting Member

15 Posts

Posted - 2010-02-19 : 06:42:23
Yes it's sql server 2005
quote:
Originally posted by visakh16

are you using sql 2005?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-19 : 07:21:14
Have you seen my previous reply?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Studevs
Starting Member

15 Posts

Posted - 2010-02-19 : 07:31:37
Hi madhivanan,

This is the code:


CREATE TABLE [dbo].[customer](
[name] [varchar](50) NULL,
[number] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO




INSERT INTO [qvtelemetrie].[dbo].[customer]
([name]
,[number])
VALUES
('name','3214')
GO


quote:
Originally posted by madhivanan

Have you seen my previous reply?

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-19 : 07:32:42
quote:
Originally posted by Studevs

Hi madhivanan,

This is the code:


CREATE TABLE [dbo].[customer](
[name] [varchar](50) NULL,
[number] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO




INSERT INTO [qvtelemetrie].[dbo].[customer]
([name]
,[number])
VALUES
('name','3214')
GO


quote:
Originally posted by madhivanan

Have you seen my previous reply?

Madhivanan

Failing to plan is Planning to fail




What is your expected result?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Studevs
Starting Member

15 Posts

Posted - 2010-02-19 : 07:42:45
i want a select statement wich returns the 'name' column data just like it is and the ascii code of the 'number' column data
quote:
Originally posted by madhivanan

quote:
Originally posted by Studevs

Hi madhivanan,

This is the code:


CREATE TABLE [dbo].[customer](
[name] [varchar](50) NULL,
[number] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO




INSERT INTO [qvtelemetrie].[dbo].[customer]
([name]
,[number])
VALUES
('name','3214')
GO


quote:
Originally posted by madhivanan

Have you seen my previous reply?

Madhivanan

Failing to plan is Planning to fail




What is your expected result?

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-02-19 : 08:08:35
ASCII code for the number 1234 will give you the ascii code of 1 only.
what you need



Vabhav T
Go to Top of Page

Studevs
Starting Member

15 Posts

Posted - 2010-02-19 : 08:32:25
I need the ascii of the entire string, not only from first character
quote:
Originally posted by vaibhavktiwari83

ASCII code for the number 1234 will give you the ascii code of 1 only.
what you need



Vabhav T

Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-02-19 : 08:50:49
ascii is for only one character.
what you want you want to concatenate all the ascii code for each character or what.

Vabhav T
Go to Top of Page

Studevs
Starting Member

15 Posts

Posted - 2010-02-19 : 09:11:24
Hi Vabhav,

Thanks,
yes that's exatly what i want :)
quote:
Originally posted by vaibhavktiwari83

ascii is for only one character.
what you want you want to concatenate all the ascii code for each character or what.

Vabhav T

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-19 : 09:17:12
quote:
Originally posted by Studevs

Hi Vabhav,

Thanks,
yes that's exatly what i want :)
quote:
Originally posted by vaibhavktiwari83

ascii is for only one character.
what you want you want to concatenate all the ascii code for each character or what.

Vabhav T




Why do you want to do this?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Studevs
Starting Member

15 Posts

Posted - 2010-02-19 : 09:20:35
it's a requirement
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-19 : 09:44:41
quote:
Originally posted by Studevs

it's a requirement


create a function like this


CREATE FUNCTION dbo.GetAsciiValue
(
@str varchar(100)
)
RETURNS varchar(1000)
AS
BEGIN
DECLARE @res varchar(1000)

SELECT @res=COALESCE(@res,'')+CAST(ASCII(SUBSTRING(@str,number,1)) AS varchar(5))
FROM master..spt_values
WHERE type='p'
AND number BETWEEN 1 AND LEN(@str)

RETURN @res
END

then use it like

select name,number,dbo.GetAsciiValue(number) AS AsciiEqv
from yourtable


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Studevs
Starting Member

15 Posts

Posted - 2010-02-19 : 10:02:28
Dear visakh16,

This works great , thanks a lot for your great help.
quote:
Originally posted by visakh16

quote:
Originally posted by Studevs

it's a requirement


create a function like this


CREATE FUNCTION dbo.GetAsciiValue
(
@str varchar(100)
)
RETURNS varchar(1000)
AS
BEGIN
DECLARE @res varchar(1000)

SELECT @res=COALESCE(@res,'')+CAST(ASCII(SUBSTRING(@str,number,1)) AS varchar(5))
FROM master..spt_values
WHERE type='p'
AND number BETWEEN 1 AND LEN(@str)

RETURN @res
END

then use it like

select name,number,dbo.GetAsciiValue(number) AS AsciiEqv
from yourtable


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-19 : 10:04:00
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -