tsql - SQL Server function to pass type as param -


Is it possible to type paragraph on the SQL function?

Thanks in advance.

The parameter type is defined in the function header, as a result it is always the same within the function. When you pass in a parameter that is different from the defined type parameter type, then the SQL Server defines the function best according to the parameter type.

I have some functions that I just want to work like what you are, the way I work around this issue, define the function parameter as varchar (x) And then the parameter calls to any required CVONVERT () or function to call the function on the incoming parameter.

as return COALESCE start ( '' '' + @ InputStr + '' '', 'null') end GO announced @DateValue Date set @ DATEVALUE = GETDATE () print '@DateValue =' + dbo.QuoteValue (@DateValue) print '@DateValue =' + dbo.QuoteValue (CONVERT (varchar , @ DATEVALUE, 121))

If you want to work with dates or numbers, then what you can do, function parameter varchar (x) and function within use: < / P>

  • ISDATE (expression) - if Nput expression is a valid date, returns ISDATE 1; Otherwise, it gives 0. The following table shows return value for selection of examples.
  • ISGLEERIC (TRANSCT-SQL) - ISLECR 1 returns when input exposure evaluates valid integer, floating point number, money or decimal type; Otherwise it gives 0. The return value of 1 indicates that the expression can be converted to at least one numerical type.

Here is an example of handling dates ...

- - manipulate some time end date return date @ - If no valid date You can use it as follows:
  DECLARE @ x ​​varchar (50) DECLARE @Y varchar (50) DECLARE @BadDate Debt time DECLARE @GoodDate Datetime SET @ X = 'Bad Date !!' Set @ Y = '1/1/2009' SET @ BadDate = dbo.DateXYZ (@X) SET @ GoodDate = dbo.DateXYZ (@Y) SELECT @ BadDate, @ GoodDate  
< You can also pass in a parameter to inform what you are passing through:

  function dbo.AnyType (@InputStr varchar (8000) - General purpose value, @ReturnValue varchar (8000) - announced as varchar (8000) @ Type four (1) - "I" NT, 'F' loat, 'D' atetime, "S" tring, etc ...) If @ type starts = 'I' --int start declare @IntValue integer set @ intValue = @InputStr --do some integer manipulation here Set @ ReturnValue = @ end and intValue if start @ type = 'f' --float declared SET boat @floatValue @ floatValue = @inputstr --do some boat manipulation here @ ReturnValue = @ FloatValue end and set @ type = 'D' --Datetime Announced @DatetimeValue Date Setting @ DatetimeValue = @ InputStr --do Start some datetime manipulation here. ReturnValue = CONVERT (varchar (23), @ DatetimeValue, 121) end --etc ... - Etc ... Return @ReturnValue End GO  

The task can return only a certain type, make it varchar (x) and the caller is in a variable of the appropriate data type Copy the CAST in, or set as needed results.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -