Search code examples
sql-serverstringtype-conversionmoney-format

Issue with datatype Money in SQL SERVER vs string


I have a spreadsheet that gets all values loaded into SQL Server. One of the fields in the spreadsheet happens to be money. Now in order for everything to be displayed correcctly - i added a field in my tbl with Money as DataType.

When i read the value from spreadsheet I pretty much store it as a String, such as this... "94259.4". When it get's inserted in sql server it looks like this "94259.4000". Is there a way for me to basically get rid of the 0's in the sql server value when I grab it from DB - because the issue I'm running across is that - even though these two values are the same - because they are both compared as Strings - it thinks that there not the same values.

I'm foreseeing another issue when the value might look like this...94,259.40 I think what might work is limiting the numbers to 2 after the period. So as long as I select the value from Server with this format 94,259.40 - I thin I should be okay.

EDIT:

For Column = 1 To 34
    Select Case Column
        Case 1  'Field 1
           If Not ([String].IsNullOrEmpty(CStr(excel.Cells(Row, Column).Value)) Or CStr(excel.Cells(Row, Column).Value) = "") Then
  strField1 = CStr(excel.Cells(Row, Column).Value)
           End If
        Case 2 'Field 2
        ' and so on

I go through each field and store the value as a string. Then I compare it against the DB and see if there is a record that has the same values. The only field in my way is the Money field.


Solution

  • You can use the Format() to compare strings, or even Float For example:

    Declare @YourTable table (value money)
    Insert Into @YourTable values
    (94259.4000),
    (94259.4500),
    (94259.0000)
    
    Select Original  = value
          ,AsFloat   = cast(value as float)
          ,Formatted = format(value,'0.####')
     From  @YourTable
    

    Returns

    Original    AsFloat     Formatted
    94259.40    94259.4     94259.4
    94259.45    94259.45    94259.45
    94259.00    94259       94259
    

    I should note that Format() has some great functionality, but it is NOT known for its performance