473,431 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,431 software developers and data experts.

How to create a function with boolean parameter?

oulan
4
I want to create a function like IIF in Microsoft Access. But SQL Server 2000 do not have boolean data type. Someone told me bit could be alternative.

The question is, if I define function like:
create function IIF(@boolexp bit, @tv sql_variant, @fv sql_variant) ...

I could

SELECT IIF(COLNAME*3, 'T', 'F') FROM TABLENAME

but could not use

SELECT IFF(COLNAME > 3, 'T', 'F') FROM TABLENAME

So, how to solve that?

Thanks a lot.
Jul 8 '10 #1
6 6433
ck9663
2,878 Expert 2GB
Maybe the CASE expression will do.

Happy Coding!!!

~~ CK
Jul 8 '10 #2
oulan
4
I think you misunderstand me.

I need a function it could accept a boolean expression. I know CASE could do the work. It is not the real function of IIF.

What I need is

SELECT user_function(boolean_expression, ...) ...
Jul 8 '10 #3
ck9663
2,878 Expert 2GB
There are no IIF() function in SQL Server. It also does not have Boolean data type, instead it uses a Bit Data Type. Based on the sample you first posting, that could be done in SQL Server but it will take a lot of programming and the technique that you're going to use can degrade the performance of your code.


Good Luck!!!

~~ CK
Jul 8 '10 #4
oulan
4
@ck9663
Thank you CK.
"Based on the sample you first posting, that could be done in SQL Server but it will take a lot of programming and the technique that you're going to use can degrade the performance of your code."
That maybe mean I still could use a boolean expression in my user defined function?
For example, I want to call a user defined function like
SELECT UDF(COLNAME > 3, 'T', 'F') FROM TABLENAME
COLNAME > 3 is a boolean expression, but it would not be recognized in SQL Server:(
I could not found answer on how to use boolean parameter in MSDN and other site.
I were writing a software to build system with "unified SQL", That's the reason of why I need this function. "case when" need more time on SQL parsing.

Thank you for your answer. I still need know the posibility, or I'll give up.
Jul 8 '10 #5
ck9663
2,878 Expert 2GB
Yes you can.
1. Create a function that accepts 3 parameters and returns 1 value.
2. Build a dynamic t-sql that will test your boolean expression.
3. Executed the dynamic t-sql using EXEC and return 'T' for True or 'F' for False.
4. Store that returned value into a variable.
5. Return the value of the variable.

Again, this will drag your process.

Good Luck!!!

~~ CK
Jul 8 '10 #6
oulan
4
CK, Thank you.

CREATE TABLE TABLENAME(
...
COLNAME ...
...
)
go

SELECT UDF('COLNAME > 1', 'T', 'F') FROM TABLENAME

How could exec could return the value of 'COLNAME > 1'?

Maybe only way is write sql parser translate SELECT IIF(...) to CASE WHEN.

Hard work.

Thank you.
Jul 9 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
5
by: Matt | last post by:
I have a simple JS function that I want to return a true or false value based on the parameter passed in. At this point of time I receive the error "'True' is undefined". Here is my code below. ...
1
by: Barbara Lindsey | last post by:
I am a postgres newbie. I am trying to create a trigger that will put a copy of a record into a backup table before update or delete. As I understand it, in order to do this I must have a...
10
by: Zoe Hart | last post by:
I have a wsdl file that I received from a third party and I'm using wsdl.exe (.NET 2.0) to import it and generate a proxy class. I've actually got a proxy class that works, but I'm trying to...
0
by: bog39 | last post by:
We have z/os and DB/2 V. 8 running. I try to create a new UDF using the command CREATE FUNCTION: CREATE FUNCTION CNGETADR (INTEGER) RETURNS CHAR(50) EXTERNAL NAME CNADR001 ...
6
by: many_years_after | last post by:
Hi, cppers: I am studying cpp recently. As is said, member funciton can be as one parameter of stl algorithm. BUT when I pass member function whose parameter is instance of the class, it doesn't...
2
by: Franck | last post by:
I am looking for a way to pass a function as parameter, NOT A DELEGATE. what i am trying to do is a worker process as for example of what i want to do : public static void...
1
by: siva125 | last post by:
function populatedropdown(dayfield, monthfield, yearfield) { var today=new Date() var dayfield=document.getElementById(dayfield) var monthfield=document.getElementById(monthfield) var...
3
by: Rohullah | last post by:
Hello i want to make optional a parameter in oracel function for example this is my function create function sum1(No1 in number,No2 in number,Result out number) is begin Result:=No1+No2;...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.