473,883 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SIMPLE command to convert string to number? Not CAST or CONVERT.

Dear Experts,

Ok, I hate to ask such a seemingly dumb question, but I've
already spent far too much time on this. More that I
would care to admit.

In Sql server, how do I simply change a character into a number??????

In Oracle, it is:

select to_number(20.55 )
from dual

TO_NUMBER(20.55 )
----------------
20.55

And we are on with our lives.
In sql server, using the Northwinds database:

SELECT
r.regionid,
STR(r.regionid, 7,2) as a_string,
CONVERT(numeric , STR(r.regionid, 7,2)) as a_number,
cast ( STR(r.regionid) as int ) as cast_to_number
FROM REGION R

1 1.00 1 1
2 2.00 2 2
3 3.00 3 3
4 4.00 4 4


SELECT
r.regionid,
STR(r.regionid, 7,2) as a_string,
CONVERT(numeric , STR(r.regionid, 7,2) ) as a_number,
cast (STR(r.regionid ,7,2) as numeric ) as cast_to_number
FROM REGION R

1 1.00 1 1
2 2.00 2 2
3 3.00 3 3
4 4.00 4 4

Str converts from number to string in one motion.

Isn't there a simple function in Sql Server to convert
from string to number?

What is the secret?

Thanks

Aug 15 '06 #1
4 118821
db*****@yahoo.c om wrote:
Dear Experts,

Ok, I hate to ask such a seemingly dumb question, but I've
already spent far too much time on this. More that I
would care to admit.

In Sql server, how do I simply change a character into a number??????

In Oracle, it is:

select to_number(20.55 )
from dual

TO_NUMBER(20.55 )
----------------
20.55

And we are on with our lives.
In sql server, using the Northwinds database:

SELECT
r.regionid,
STR(r.regionid, 7,2) as a_string,
CONVERT(numeric , STR(r.regionid, 7,2)) as a_number,
cast ( STR(r.regionid) as int ) as cast_to_number
FROM REGION R

1 1.00 1 1
2 2.00 2 2
3 3.00 3 3
4 4.00 4 4


SELECT
r.regionid,
STR(r.regionid, 7,2) as a_string,
CONVERT(numeric , STR(r.regionid, 7,2) ) as a_number,
cast (STR(r.regionid ,7,2) as numeric ) as cast_to_number
FROM REGION R

1 1.00 1 1
2 2.00 2 2
3 3.00 3 3
4 4.00 4 4

Str converts from number to string in one motion.

Isn't there a simple function in Sql Server to convert
from string to number?

What is the secret?

Thanks
I don't really understand your problem. CAST does what you want and has
the advantage of being Standard SQL (unlike TO_NUMBER). STR does
something different and isn't needed in this case.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Aug 15 '06 #2
Hmm. The question remains the same. (Not, why do I want to do that,
or, what's wrong with what you already have? Or, you shouldn't do
that.)

I want a SIMPLE sql server function like to_number(), or str()
To convert a string to a number.

Does a simple function exist in sql server to do the conversion?
What is the secret?

Thanks

David Portas wrote:
db*****@yahoo.c om wrote:
Dear Experts,

Ok, I hate to ask such a seemingly dumb question, but I've
already spent far too much time on this. More that I
would care to admit.

In Sql server, how do I simply change a character into a number??????

In Oracle, it is:

select to_number(20.55 )
from dual

TO_NUMBER(20.55 )
----------------
20.55

And we are on with our lives.
In sql server, using the Northwinds database:

SELECT
r.regionid,
STR(r.regionid, 7,2) as a_string,
CONVERT(numeric , STR(r.regionid, 7,2)) as a_number,
cast ( STR(r.regionid) as int ) as cast_to_number
FROM REGION R

1 1.00 1 1
2 2.00 2 2
3 3.00 3 3
4 4.00 4 4


SELECT
r.regionid,
STR(r.regionid, 7,2) as a_string,
CONVERT(numeric , STR(r.regionid, 7,2) ) as a_number,
cast (STR(r.regionid ,7,2) as numeric ) as cast_to_number
FROM REGION R

1 1.00 1 1
2 2.00 2 2
3 3.00 3 3
4 4.00 4 4

Str converts from number to string in one motion.

Isn't there a simple function in Sql Server to convert
from string to number?

What is the secret?

Thanks

I don't really understand your problem. CAST does what you want and has
the advantage of being Standard SQL (unlike TO_NUMBER). STR does
something different and isn't needed in this case.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--
Aug 15 '06 #3
Yes, there is a simple function. It is called CAST(). It is has the
advantage of being completely ANSI SQL compliant. And it is easy to use.

Of course, you could make your own FUNCTION if CAST() is too complex or
difficult to use.

I don't get the repeated question. It was asked and answered.

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<db*****@yahoo. comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Hmm. The question remains the same. (Not, why do I want to do that,
or, what's wrong with what you already have? Or, you shouldn't do
that.)

I want a SIMPLE sql server function like to_number(), or str()
To convert a string to a number.

Does a simple function exist in sql server to do the conversion?
What is the secret?

Thanks

David Portas wrote:
>db*****@yahoo.c om wrote:
Dear Experts,

Ok, I hate to ask such a seemingly dumb question, but I've
already spent far too much time on this. More that I
would care to admit.

In Sql server, how do I simply change a character into a number??????

In Oracle, it is:

select to_number(20.55 )
from dual

TO_NUMBER(20.55 )
----------------
20.55

And we are on with our lives.
In sql server, using the Northwinds database:

SELECT
r.regionid,
STR(r.regionid, 7,2) as a_string,
CONVERT(numeric , STR(r.regionid, 7,2)) as a_number,
cast ( STR(r.regionid) as int ) as cast_to_number
FROM REGION R

1 1.00 1 1
2 2.00 2 2
3 3.00 3 3
4 4.00 4 4


SELECT
r.regionid,
STR(r.regionid, 7,2) as a_string,
CONVERT(numeric , STR(r.regionid, 7,2) ) as a_number,
cast (STR(r.regionid ,7,2) as numeric ) as cast_to_number
FROM REGION R

1 1.00 1 1
2 2.00 2 2
3 3.00 3 3
4 4.00 4 4

Str converts from number to string in one motion.

Isn't there a simple function in Sql Server to convert
from string to number?

What is the secret?

Thanks

I don't really understand your problem. CAST does what you want and has
the advantage of being Standard SQL (unlike TO_NUMBER). STR does
something different and isn't needed in this case.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Aug 16 '06 #4
The ANSI/ISO international standard for the SQL language
does not provide the function you want. Oracle chose to add
a proprietary nonstandard function for this (which seems not
to allow you to specify the specific number type, and there are
many), and SQL Server uses the ANSI/ISO CAST function and
requires a type.

SELECT CAST('20.55' AS DECIMAL(4,2))

You can (if you know the consequences) use implicit conversion:

SELECT 100.00*'20.55'
SELECT 10000.0000*'20. 55'
SELECT 1E0*'20.55'

If you aren't happy with what SQL offers as a language, there are
plenty of other languages in the world of computing from which
you can choose another.

Steve Kass
Drew University

<db*****@yahoo. comwrote in message news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Hmm. The question remains the same. (Not, why do I want to do that,
or, what's wrong with what you already have? Or, you shouldn't do
that.)

I want a SIMPLE sql server function like to_number(), or str()
To convert a string to a number.

Does a simple function exist in sql server to do the conversion?
What is the secret?

Thanks

David Portas wrote:
>db*****@yahoo.c om wrote:
Dear Experts,

Ok, I hate to ask such a seemingly dumb question, but I've
already spent far too much time on this. More that I
would care to admit.

In Sql server, how do I simply change a character into a number??????

In Oracle, it is:

select to_number(20.55 )
from dual

TO_NUMBER(20.55 )
----------------
20.55

And we are on with our lives.
In sql server, using the Northwinds database:

SELECT
r.regionid,
STR(r.regionid, 7,2) as a_string,
CONVERT(numeric , STR(r.regionid, 7,2)) as a_number,
cast ( STR(r.regionid) as int ) as cast_to_number
FROM REGION R

1 1.00 1 1
2 2.00 2 2
3 3.00 3 3
4 4.00 4 4


SELECT
r.regionid,
STR(r.regionid, 7,2) as a_string,
CONVERT(numeric , STR(r.regionid, 7,2) ) as a_number,
cast (STR(r.regionid ,7,2) as numeric ) as cast_to_number
FROM REGION R

1 1.00 1 1
2 2.00 2 2
3 3.00 3 3
4 4.00 4 4

Str converts from number to string in one motion.

Isn't there a simple function in Sql Server to convert
from string to number?

What is the secret?

Thanks

I don't really understand your problem. CAST does what you want and has
the advantage of being Standard SQL (unlike TO_NUMBER). STR does
something different and isn't needed in this case.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Aug 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
47079
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
11
3824
by: jguilford | last post by:
I have created a SQL Stored Procedure that uses a Case statement to determine the Order By. For one of the Case statements I am trying to turn a Char field into Datetime in for the Order By, however I can not get it to work. Can someone please take a look and my code below and tell me what I am doing wrong. Thank you. ORDER BY CASE WHEN @SortBy = 'dttm_stamp' THEN dttm_stamp End, CASE WHEN @SortBy = 'Event_Date1' THEN...
7
7130
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done without strtol or s/printf function. Thanks, whatluo.
6
2030
by: Picho | last post by:
Hi group. This must have come up a few times before but I found no specific resources regarding this. the problem is simple: convert the number 55 to the string "Fifty five" (obviously with any given number...) The algorithm is quite simple and I would not have bothered you with such a simple problem.
19
3624
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and places: let's move from here forward please. The usability of any program depends on exact behavior...
11
12354
by: davidj411 | last post by:
i am parsing a cell phone bill to get a list of all numbers and the total talktime spend on each number. i already have a unique list of the phone numbers. now i must go through the list of numbers and add up the totals for each number. on the bill, each line has a few fields,one field containing the phone number, another field containing the number of minutes on that call. the bill is comma delimited.
2
3102
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See the section on "simple decimal...
4
7243
subedimite
by: subedimite | last post by:
Here I am with another question being a very new VBA user. I have this scenario to work with: I would think this is fairly common routine of work for VBA. I have a string of hex contents as characters such as String1 = “3B F0 96 00 FF C0 0A 31 FE 4D D1” I need to do some calculation with these and then update a table with the text value. For example, A sum of all these.
0
9932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9778
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5784
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3228
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.