sql server - Strange CAST behaviour with INT and a single hyphen ( - ) -


i answered question: concate primary keys in sql

there encountered strange behaviour:

select 5 + '-' +  8  

returns 13

select cast('-' int)  

returns 0, explains above...

but: why single hyphen casted 0 implictly?

btw: it's same single + or (multi-)blank string...

this related cast hyphen (-) decimal points fact, cast decimal not bring these results...

i'm writing answer because it's long/complex comment - it's based off comments. note - not have official source, no confirmation logic "what's implemented". (but makes sense think :))

but suppose you're writing conversion function, needs perform.

so have string validate - example cast('-50' int);
take each character on own:

`-` valid part of conversion, move next character.   `5` valid part of conversion, move next character.   `0` valid part of conversion, move next character.   done. 

so supposed string cast('-' int);:

`-` valid part of conversion, move next character.   done. 

now - additional check invalidate because - not allowed standalone, require additional code.

similar + , spaces. same currency , money or period , money:

select cast('$' money) select cast('.' money) 

both valid characters - in actuality in connection actual numbers well. parse through - indicating it's intentional, , speed conversion seems sensible.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

unity3d - Fatal error- Monodevelop-Unity failed to start -