Jump to content

[SOLVED] IS NULL (), Something simple.


maliary

Recommended Posts

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

 

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')

 

 

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.

 

 

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.

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.