Jump to content

Archived

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

maliary

[SOLVED] IS NULL (), Something simple.

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]

Share this post


Link to post
Share on other sites

I think there's IFNULL() function for that.

Share this post


Link to post
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')

 

Share this post


Link to post
Share on other sites

ISNULL is the correct function - for example:

 

ISNULL(T0.[Quantity], 'Nothing') as 'Iss. Qty'

 

 

Share this post


Link to post
Share on other sites

Still getting the same error if I use ISNULL on an interger or datetime field.

Share this post


Link to post
Share on other sites

Try this

 

ISNULL(CAST(T0.[Quantity] AS varchar(50)), 'Nothing') as 'Iss. Qty'

 

Share this post


Link to post
Share on other sites

 

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.

 

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

 

 

Share this post


Link to post
Share on other sites

 

I solved it this way

 

ISNULL(CONVERT(VARCHAR(20), T1.[DocDate], 101) , 'Not Returned')

Share this post


Link to post
Share on other sites

×
×
  • 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.