Solved

SQL / xml / real conversion problem

Posted on 2014-01-23
12
333 Views
Last Modified: 2014-02-16
MS SQL Server 2008.

I have a stored procedure that recieves and xml document and uses it to update a table.

I have a line of code:

  [Tumorhojd] = @xmlData.value('(BRUM-registret/OriginalData2012/Tumorhojd)[1]', 'real'),

Open in new window

that updates a REAL column.

It works when the value of Tumorhojd is a number that has a decimal point, but not when it contains a decimal comma.

We are using decimal comma's in our data.

Is there a way to set up the conversion so that decimal comma is accepted?

Thanks
0
Comment
Question by:soozh
  • 5
  • 4
  • 3
12 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39803798
you can do this

[Tumorhojd] = CAST(REPLACE(@xmlData.value('(BRUM-registret/OriginalData2012/Tumorhojd)[1]', 'VARCHAR'),',','') AS REAL)

Open in new window

0
 

Author Comment

by:soozh
ID: 39803841
Yes i could do that but i was wondering how i could change the database/server settings to use decimal comma.
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39803933
I believe that cannot be done (atleast upto my knowledge).
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 39808105
Something like this perhaps:
[Tumorhojd] = CAST(REPLACE(@xmlData.value('(BRUM-registret/OriginalData2012/Tumorhojd)[1]', 'varchar(20)'), ',', '.') AS real)

Open in new window

Incidentally using real data type is very very rarely a good idea.
0
 

Author Comment

by:soozh
ID: 39808309
ok how would you store "real" data.  By that i mean data with a decimal point(or comma).

And how do countries that use decimal comma (and i Think that is most of Europé) handle this problem?  MS SQL Server must be set up for the "Locale" just like windows can be?
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39808367
decimal points are accepted, but the commas are not accepted as part of real or decimal data types
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:soozh
ID: 39808379
what i mean is that if i convert a real to a varchar i would expect it to contain a decimal comma in the countries that use decimal comma, and decimal Point in the countries that use decimal Point....

and it should be configurable... that sounds resonable... you cant expect the french to use decimal Point..
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39808796
ok how would you store "real" data.  By that i mean data with a decimal point(or comma).
Both Real and Float are approximate numeric data types and should only be used for scientific or astronomic data.  That is unless you are OK with $10.23 represented as 10.230000001 or 10.2299999991.  You are better off using numeric (or decimal) or money or smallmoney data types to represent decimal values as they are exact.

And how do countries that use decimal comma (and i Think that is most of Europé) handle this problem?  
This is handled at the application level.  SQL Server does not store values with decimal points or decimal commas.
0
 

Author Comment

by:soozh
ID: 39808899
This is handled at the application level.  SQL Server does not store values with decimal points or decimal commas.

I understand..but if i have a stored procedure with this code:

[Tumorhojd] = @xmlData.value('(BRUM-registret/OriginalData2012/Tumorhojd)[1]', 'real' )

i guess the stored procedure is the application - so how do it get it to use decimal comma?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39809117
Did you see my solution earlier on?
0
 

Author Comment

by:soozh
ID: 39809187
yes but say i am programing in delphi to can convert a string '1,23' to a real number using the standard strToFloat function.

However if my code is run on a server with English settings this would give an error because it expects '1.23'

the StrToFloat function looks at the locale to determine what type of decimal character to use.

Since i am using standard tsql code i would expect the tsql functions to look at the locale and convert decimals and dates accordingly.

So my assumption is that maybe i have the locale set wrong?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39809333
It is possible.  I don't really know.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query 2 43
Implementing SQL Server Data Files in Azure 1 35
user defined date datatype in SQL Server- can it be overdone.. 6 22
Max Consumption Rate (MCR) 3 32
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now