x
login about faq Site discussion (meta-askssc)

MySQL to MSSQL: IF(value=5,1,NULL) how to do it in MSSQL?

How can I convert this MySQL statement to MSSQL?

IF(value=5,1,NULL)

It is a simple IF statement that returns the first value if TRUE (1), and the second if FALSE (NULL)

I am using this with PHP so it is not to be placed in a function/procedure.

Thanks!

more ▼

asked Jul 20 '10 at 09:40 PM in Default

johnshaddad gravatar image

johnshaddad
43 8 8 10

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

SELECT CASE WHEN VALUE = 5 THEN 1 ELSE NULL END

more ▼

answered Jul 20 '10 at 09:44 PM

Cyborg gravatar image

Cyborg
10.1k 29 39 44

The statement on its own is working, and i have tried it before on MSSQL Query window. But my question was about PHP inline mssql query... to put it in my PHP code.. not in a stored procedure or function.

I tried this but didn't work!

$query = "INSERT INTO notes (requestid,pmid,clientid,note) VALUES ('".$_GET['itemid']."',CASE WHEN LEN('$session->pmid') = 0 THEN NULL ELSE '$session->pmid',CASE WHEN LEN('$session->clientid') = 0 THEN NULL ELSE '$session->clientid','@note')";

Also tried it by adding SELECT before each CASE:

$query = "INSERT INTO notes (requestid,pmid,clientid,note) VALUES ('".$_GET['itemid']."', SELECT CASE WHEN LEN('$session->pmid') = 0 THEN NULL ELSE '$session->pmid',SELECT CASE WHEN LEN('$session->clientid') = 0 THEN NULL ELSE '$session->clientid','@note')";

Also tried adding "END" next to each CASE statement:

$query = "INSERT INTO notes (requestid,pmid,clientid,note) VALUES ('".$_GET['itemid']."', SELECT CASE WHEN LEN('$session->pmid') = 0 THEN NULL ELSE '$session->pmid' END,SELECT CASE WHEN LEN('$session->clientid') = 0 THEN NULL ELSE '$session->clientid','@note') END";

None works! What is wrong? I am good at MySQL but new to MSSQL, sorry!

Jul 20 '10 at 10:08 PM johnshaddad

@Jhonshaddad

INSERT INTO notes (requestid,pmid,clientid,note)

SELECT @ItemID, CASE WHEN LEN('$session->pmid') = 0 THEN NULL ELSE '$session->pmid' END,CASE WHEN LEN('$session->clientid') = 0 THEN NULL ELSE '$session->clientid','@note') END

Jul 20 '10 at 10:13 PM Cyborg

There is no point devolving this down to the SQL Server - you should clean your parameter up in the PHP first, then use a simpler construct for the actual insert.

Jul 21 '10 at 01:04 AM Matt Whitfield ♦♦

I agree with @Matt - send only what you want to SQL Server. Use the front-end code to preselect the value. Fewer bytes crossing the network and less work for the back-end means better performance all round.

Jul 21 '10 at 05:35 AM Blackhawk-17

lol sorry for the wrong name target @Matt Whitifield go grill that flying pig :P

Jul 21 '10 at 07:27 PM johnshaddad
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x912
x52
x25

asked: Jul 20 '10 at 09:40 PM

Seen: 5682 times

Last Updated: Jul 21 '10 at 12:56 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.