Solved

how to call a standardNumber with 2digits

Posted on 2009-04-10
11
237 Views
Last Modified: 2013-11-05
I do have following code

strSQL = "SELECT [Waehrung]&' '& [Kurs]  " & _
             "FROM dbo_GFHilfstabelleV1_Sicht255 " & _
             "WHERE dbo_GFHilfstabelleV1_Sicht255.Symbol = '" & (.Item("GF_V1_TA_Symbol_MainStock")) & "' "

            With .Item("GF_V1_TA_Kurs_MainStock")
                .Value = CurrentProject.Connection.Execute(strSQL)(0)
            End With
             
where I would like to have [Kurs] as a StandardNumber with 2digits. Haven't found any help on internet.
thx
Kongta
0
Comment
Question by:Kongta
  • 4
  • 4
  • 3
11 Comments
 
LVL 12

Expert Comment

by:koutny
Comment Utility
Use the Format function:
Format(numeric_expression, "00")


strSQL = "SELECT [Waehrung]&' '& Format([Kurs],"00")  " & _

             "FROM dbo_GFHilfstabelleV1_Sicht255 " & _

             "WHERE dbo_GFHilfstabelleV1_Sicht255.Symbol = '" (.Item("GF_V1_TA_Symbol_MainStock")) & "' "

Open in new window

0
 
LVL 12

Expert Comment

by:koutny
Comment Utility
Looking at your code in more detail now I can see that you are concatanating the fields within the SQL string. So the the Format function will only work if the dabase is MS Access.
0
 

Author Comment

by:Kongta
Comment Utility
I do have MS SQL-Server backend and cant find any way to format the number in the code as I fusion text & numbers in my script
0
 

Author Comment

by:Kongta
Comment Utility
Sorry, I might have expressed not detailed enough, I meant with 2digits behind the comma if needed, but if I have a round number, it doesn't show. Same as I would choose StandardNumber with 2comma in Access
0
 
LVL 12

Expert Comment

by:koutny
Comment Utility
Well, if it is SQL Server then you might be able to use the Replicate function. Here is an example which format the ID field of a Contacts table:

SELECT Replicate('0', 2 - Len(Cast(ContactID as varchar(10)))) + Cast(ContactID as varchar(10))
FROM Contacts

However, if you only use the value returened by your query in your code then I would say it might be easier to format the result after you have obtained the values from the database. What I mean is output the two fields seperately in the select statement and only then join them together. See the code below.



dim rst as ADODB.Recordset
 

strSQL = "SELECT [Waehrung], [Kurs]  " & _

             "FROM dbo_GFHilfstabelleV1_Sicht255 " & _

             "WHERE dbo_GFHilfstabelleV1_Sicht255.Symbol = '" & (.Item("GF_V1_TA_Symbol_MainStock")) & "' "
 

set rst = CurrentProject.Connection.Execute(strSQL)
 

            With .Item("GF_V1_TA_Kurs_MainStock")

                .Value = rst.Fields(0) & " " &  Format(rst.Fields(1)),"00") 

            End With

rst.Close

set rst = nothing

             

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 12

Expert Comment

by:Dimitris
Comment Utility
Use the cast as decimal
You provide the precision and scale. By seting the scale to 2 all the numbers will have 2 decimals

strSQL = "SELECT [Waehrung], cast([Kurs] as Decimal(18,2)) as [Kurs] " & _
             "FROM dbo_GFHilfstabelleV1_Sicht255 " & _
             "WHERE dbo_GFHilfstabelleV1_Sicht255.Symbol = '" & (.Item("GF_V1_TA_Symbol_MainStock")) & "' "
0
 

Author Comment

by:Kongta
Comment Utility
I see the way but it does't work as it should. Maybe there is no proper way to combine
0
 
LVL 12

Accepted Solution

by:
koutny earned 250 total points
Comment Utility
The code snippet below should work too.

By the way, I am a bit confused here, shouldn't the SQL string for SQL Server back end use SQL Server SQL syntax? What I meant is the use of the & operator. Shouldn't it be +, like in my code below?
strSQL = "SELECT [Waehrung] + ' ' + Right('00' + Cast(Kurs as varchar(2)),2)  " & _

             "FROM dbo_GFHilfstabelleV1_Sicht255 " & _

             "WHERE dbo_GFHilfstabelleV1_Sicht255.Symbol = '" (.Item("GF_V1_TA_Symbol_MainStock")) & "' " 

Open in new window

0
 
LVL 12

Expert Comment

by:Dimitris
Comment Utility
what u mean that it doesn't work as it sould?
It keeps the leading digits and rounds up any decimals to 2 digits.
0
 
LVL 12

Expert Comment

by:Dimitris
Comment Utility
sorry Rounds, not rounds up 1,004 will be 1,00 and 1,004 will be 1,01
0
 

Author Closing Comment

by:Kongta
Comment Utility
ok, got it done now, thx a lot.
I have no idea on the  &/+  issue but it works, I copy/pasted this out of an older Access.FrontEnd and it works.
Anyway, many thx and have a good time
Bye
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

9 Experts available now in Live!

Get 1:1 Help Now