maliary Posted April 17, 2009 Share Posted April 17, 2009 Hello, I have the query below, for some columns such as Rec. Qty, no values are returned. Where there is no value returned I would like to put the word 'Nothing', instead of having a blank. SELECT T0.[itemCode], T0.[Dscription], T0.[Quantity] as 'Iss. Qty', T0.[DocDate] as 'Iss. Date', T1.[Quantity] as 'Rec. Qty', T0.[u_Newfield], T1.[DocDate] as 'Rev. Date' FROM IGE1 T0 LEFT OUTER JOIN IGN1 T1 ON T1.[u_GI_Issue] = T0.[DocEntry] WHERE T0.[u_Newfield] = [%0] and T0.[DocDate] between [%1] and [%2] Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/ Share on other sites More sharing options...
Mchl Posted April 17, 2009 Share Posted April 17, 2009 I think there's IFNULL() function for that. Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/#findComment-812113 Share on other sites More sharing options...
maliary Posted April 17, 2009 Author Share Posted April 17, 2009 Thanks for pointing me in the right direction IFNULL() does not work with MSSQL, but I used COALESCED instead. But I get the following conversion error ERROR CONVERTING VARCHAR TO NUMERIC when I use it in on the T1.[Quantity] as 'Rec. Qty' column. Like this COALESCE (T1.[Quantity], 'NOT') Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/#findComment-812117 Share on other sites More sharing options...
maliary Posted April 17, 2009 Author Share Posted April 17, 2009 I am using MSSQL 2005 Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/#findComment-812151 Share on other sites More sharing options...
mtoynbee Posted April 17, 2009 Share Posted April 17, 2009 ISNULL is the correct function - for example: ISNULL(T0.[Quantity], 'Nothing') as 'Iss. Qty' Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/#findComment-812321 Share on other sites More sharing options...
maliary Posted April 17, 2009 Author Share Posted April 17, 2009 Still getting the same error if I use ISNULL on an interger or datetime field. Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/#findComment-812401 Share on other sites More sharing options...
mtoynbee Posted April 17, 2009 Share Posted April 17, 2009 Try this ISNULL(CAST(T0.[Quantity] AS varchar(50)), 'Nothing') as 'Iss. Qty' Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/#findComment-812441 Share on other sites More sharing options...
maliary Posted April 18, 2009 Author Share Posted April 18, 2009 Thanks, this works well only that the quantity field is changed to varchar, no problem with that though. However I have tried the same with the DocDate field but the date format is not the best when not null. I would like to change the date format from Arp 17 2009 - varchar to the datetime format. This I believe would require the use of case or if else statements. but they keep giving me the same error I have been having. Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/#findComment-813041 Share on other sites More sharing options...
maliary Posted April 18, 2009 Author Share Posted April 18, 2009 I have this SELECT T0.[itemCode], T0.[Dscription], T0.[Quantity] as 'Iss. Qty', T0.[DocDate] as 'Iss. Date', ISNULL (T1.[Quantity],0) , CASE WHEN ISNULL (T1.[Quantity],0) = 0 THEN ISNULL(CAST(T1.[Quantity] AS varchar(50)), 'Nothing') WHEN ISNULL (T1.[Quantity],0) != 0 THEN ISNULL(CAST(T1.[Quantity] AS varchar(50)), 'Nothing') END, CASE WHEN ISNULL (T1.[DocDate],0) = 0 THEN ISNULL(CAST(T1.[DocDate] AS Char(20)), 'Nothing') WHEN ISNULL (T1.[DocDate],0) != 0 THEN ISNULL(CAST(T1.[DocDate] AS datetime), 'Nothing') END , ISNULL(CAST(T1.[Quantity] AS varchar(50)), 'Nothing') as 'Recieved Qty' , T0.[u_Newfield], ISNULL(CAST(T1.[DocDate] AS varchar(50)), 'Nothing') ,COALESCE (CAST (T1.[DocDate] as Char(20)),'NOT') as 'Rev. Date' FROM IGE1 T0 LEFT OUTER JOIN IGN1 T1 ON T1.[u_GI_Issue] = T0.[DocEntry] WHERE T0.[u_Newfield] = [%0] and T0.[DocDate] between [%1] and [%2] When ISNULL (T1.[DocDate],0) = 0 it throws an error Conversion failed when converting datetime to character string. Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/#findComment-813055 Share on other sites More sharing options...
mtoynbee Posted April 20, 2009 Share Posted April 20, 2009 As with previous example you need to output the fields in the same format hence you cannot output 'Nothing' (varchar value) for an integer without CAST(ing) the integer. This is the same for the datetime field as 0 is not a valid datetime value. Your best bet for this is just to output as NULL. Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/#findComment-814303 Share on other sites More sharing options...
maliary Posted April 20, 2009 Author Share Posted April 20, 2009 I solved it this way ISNULL(CONVERT(VARCHAR(20), T1.[DocDate], 101) , 'Not Returned') Link to comment https://forums.phpfreaks.com/topic/154451-solved-is-null-something-simple/#findComment-814347 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.