2

I have a float column, call it money.

I am trying to insert the 0 value as 0 and the empty string value should be NULL

INSERT INTO t1 (money) values ('0.00')
INSERT INTO t1 (money) values ('')

SELECT * FROM t1

This is the result I am trying to achieve

 money
    0
    NULL

I tried Using an INSTEAD OF INSERT trigger (I used '123' just to visualize which condition is met)

iif(CAST(money AS nvarchar) = '', NULL, iif(money < '0.0001', '123', money))

SELECT * FROM t1

money
123
123

So it seems that I cannot compare my float column against an empty string.

But when trying following it looks like in fact it is possible:

CASE WHEN (money = '' THEN '456' ELSE iif(money < '0.0001', '123', money))

SELECT * FROM t1

money 
456
456

Which makes me very unsure of how SQL Server converts those datatypes internally.

5
  • Are you inserting other columns as well? Is the money field in your table set to NOT NULL? Because if you have other columns and this one is not set to NOT NULL, then just don't include it on the insert... Commented Sep 7, 2016 at 12:56
  • 1
    Re: your last statement, SQL Server's data type conversions are documented on MSDN: msdn.microsoft.com/en-us/library/ms191530.aspx Commented Sep 7, 2016 at 12:56
  • @johnhc I am inserting alot more columns and alot of them are of type float, all of them are allowed to be NULL. Commented Sep 7, 2016 at 13:01
  • 1
    Money as float datatype = bad idea. as for null... INSERT INTO t1 (money) values (NULL) or `INSERT INTO t1 (money) values (case when '' = '' then null else '') Commented Sep 7, 2016 at 13:01
  • @xQbert the column is in fact AccruedInterest i simplified it for reading purposes as money Commented Sep 7, 2016 at 13:03

2 Answers 2

2

Uhm,

you try wierd things if you insert string values into a float-column.

INSERT INTO t1 (money) values (nullif('0.00', ''))

BUT you should really invest some time in data type conversion from ground up. I would expect the calling application to have an error if someone tried to insert a string into my float-column...

Sign up to request clarification or add additional context in comments.

1 Comment

I like the, "you try wierd things if you insert string values into a float-column." :P
1

You can try this way

declare @valuetobeinserted varchar(20)
set @valuetobeinserted = '0.00'
insert into t1 select case when  @valuetobeinserted = '' then null else @valuetobeinserted end as m

set @valuetobeinserted = ''
insert into t1 select case when  @valuetobeinserted = '' then null else @valuetobeinserted end as m

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.