|
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!
(comments are locked)
|
|
SELECT CASE WHEN VALUE = 5 THEN 1 ELSE NULL END 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)
|

