473,839 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert Null in Date field to String

Hi,

My SQL returns a NULL in a datefield if there is no date in that field.
If there is a NULL in this column, I want to replace it with spaces in
my SELECT statement when I am selecting these records. Can any of you
experts out there help me with a sample SQL.

Nov 20 '06 #1
10 39193
SELECT COALESCE(VARCHA R(datefield), '') FROM X...

sa**********@gm ail.com wrote:
Hi,

My SQL returns a NULL in a datefield if there is no date in that field.
If there is a NULL in this column, I want to replace it with spaces in
my SELECT statement when I am selecting these records. Can any of you
experts out there help me with a sample SQL.
Nov 20 '06 #2
Thank you. I was trying the coalesce but did not cast it to VARCHAR.
I really appreciate your help. It works now.

Have a good day !
jefftyzzer wrote:
SELECT COALESCE(VARCHA R(datefield), '') FROM X...

sa**********@gm ail.com wrote:
Hi,

My SQL returns a NULL in a datefield if there is no date in that field.
If there is a NULL in this column, I want to replace it with spaces in
my SELECT statement when I am selecting these records. Can any of you
experts out there help me with a sample SQL.
Nov 20 '06 #3
My pleasure. Glad it worked!

--Jeff

sa**********@gm ail.com wrote:
Thank you. I was trying the coalesce but did not cast it to VARCHAR.
I really appreciate your help. It works now.

Have a good day !
jefftyzzer wrote:
SELECT COALESCE(VARCHA R(datefield), '') FROM X...

sa**********@gm ail.com wrote:
Hi,
>
My SQL returns a NULL in a datefield if there is no date in that field.
If there is a NULL in this column, I want to replace it with spaces in
my SELECT statement when I am selecting these records. Can any of you
experts out there help me with a sample SQL.
Nov 20 '06 #4
This is pretty darn cool! Much nicer than using "indicator variables" if
you don't really care that it's a NULL field.

I don't suppose that there's anything similar going the opposite direction.
In other words,

MOVE SPACES TO NULLABLE-CHAR-FIELD

EXEC SQL
INSERT INTO MYTABLE (NULLABLE_CHAR_ FIELD)
VALUES (NULL_IF_BLANK( :NULLABLE-CHAR-FIELD))
END-EXEC

Does anything like my made up function "NULL_IF_BL ANK" exist, where it
returns the input field unless the input field is all spaces in which case
it would return NULL?
If not, I may have to give a shot to making my own. Sounds fairly simple, I
think.

Thoughts?

Frank

---
Frank Swarbrick
Senior Developer/Analyst - Mainframe Applications
FirstBank Data Corporation - Lakewood, CO USA
>>jefftyzzer<je ********@sbcglo bal.net11/20/06 12:50 PM >>>
My pleasure. Glad it worked!

--Jeff

sa**********@gm ail.com wrote:
Thank you. I was trying the coalesce but did not cast it to VARCHAR.
I really appreciate your help. It works now.

Have a good day !
jefftyzzer wrote:
SELECT COALESCE(VARCHA R(datefield), '') FROM X...

sa**********@gm ail.com wrote:
Hi,
>
My SQL returns a NULL in a datefield if there is no date in that
field.
If there is a NULL in this column, I want to replace it with spaces
in
my SELECT statement when I am selecting these records. Can any of
you
experts out there help me with a sample SQL.


Nov 23 '06 #5
You might try:
INSERT INTO MYTABLE (NULLABLE_CHAR_ FIELD)
VALUES (CASE WHEN :NULLABLE-CHAR-FIELD = ''
THEN NULL
ELSE :NULLABLE-CHAR-FIELD
)

Phil Sherman

Frank Swarbrick wrote:
This is pretty darn cool! Much nicer than using "indicator variables" if
you don't really care that it's a NULL field.

I don't suppose that there's anything similar going the opposite direction.
In other words,

MOVE SPACES TO NULLABLE-CHAR-FIELD

EXEC SQL
INSERT INTO MYTABLE (NULLABLE_CHAR_ FIELD)
VALUES (NULL_IF_BLANK( :NULLABLE-CHAR-FIELD))
END-EXEC

Does anything like my made up function "NULL_IF_BL ANK" exist, where it
returns the input field unless the input field is all spaces in which case
it would return NULL?
If not, I may have to give a shot to making my own. Sounds fairly simple, I
think.

Thoughts?

Frank

---
Frank Swarbrick
Senior Developer/Analyst - Mainframe Applications
FirstBank Data Corporation - Lakewood, CO USA
>>>jefftyzzer<j e********@sbcgl obal.net11/20/06 12:50 PM >>>
My pleasure. Glad it worked!

--Jeff

sa**********@gm ail.com wrote:
>Thank you. I was trying the coalesce but did not cast it to VARCHAR.
I really appreciate your help. It works now.

Have a good day !
jefftyzzer wrote:
>>SELECT COALESCE(VARCHA R(datefield), '') FROM X...

sa**********@gm ail.com wrote:
Hi,

My SQL returns a NULL in a datefield if there is no date in that
field.
>>>If there is a NULL in this column, I want to replace it with spaces
in
>>>my SELECT statement when I am selecting these records. Can any of
you
>>>experts out there help me with a sample SQL.


Nov 23 '06 #6

Frank Swarbrick wrote:
>
MOVE SPACES TO NULLABLE-CHAR-FIELD

EXEC SQL
INSERT INTO MYTABLE (NULLABLE_CHAR_ FIELD)
VALUES (NULL_IF_BLANK( :NULLABLE-CHAR-FIELD))
END-EXEC

Does anything like my made up function "NULL_IF_BL ANK" exist, where it
returns the input field unless the input field is all spaces in which case
it would return NULL?
If not, I may have to give a shot to making my own. Sounds fairly simple, I
think.
Can you use NULLIF? Like this.
EXEC SQL
INSERT INTO MYTABLE (NULLABLE_CHAR_ FIELD)
VALUES (NULLIF(:NULLAB LE-CHAR-FIELD,''))
END-EXEC

Nov 23 '06 #7
Frank Swarbrick wrote:
This is pretty darn cool! Much nicer than using "indicator variables" if
you don't really care that it's a NULL field.
The main thing to recognize here is that the result is a string and not a
date value. An empty string '' is not a valid date in the first place.
I don't suppose that there's anything similar going the opposite
direction. In other words,

MOVE SPACES TO NULLABLE-CHAR-FIELD

EXEC SQL
INSERT INTO MYTABLE (NULLABLE_CHAR_ FIELD)
VALUES (NULL_IF_BLANK( :NULLABLE-CHAR-FIELD))
END-EXEC

Does anything like my made up function "NULL_IF_BL ANK" exist, where it
returns the input field unless the input field is all spaces in which case
it would return NULL?
If not, I may have to give a shot to making my own. Sounds fairly simple,
I think.
You can use the built-in NULLIF function for that.

If you have a string consisting of whitespaces only and you want to have
this string interpreted as NULL date, then a CASE expression is needed:

CASE
WHEN TRANSLATE(str, '', ' ') = ''
THEN NULL
ELSE str
END

If it is permissable to remove whitespaces from the string even if the
string is not empty (and should not be stored as NULL), you can nest
TRANSLATE into NULLIF:

NULLIF(TRANSLAT E(str, '', ' '), '')

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 23 '06 #8
Tonkuma<to***** @jp.ibm.com11/22/06 11:17 PM >>>
>
Frank Swarbrick wrote:
>>
MOVE SPACES TO NULLABLE-CHAR-FIELD

EXEC SQL
INSERT INTO MYTABLE (NULLABLE_CHAR_ FIELD)
VALUES (NULL_IF_BLANK( :NULLABLE-CHAR-FIELD))
END-EXEC

Does anything like my made up function "NULL_IF_BL ANK" exist, where it
returns the input field unless the input field is all spaces in which
case
>it would return NULL?
If not, I may have to give a shot to making my own. Sounds fairly
simple, I
>think.
Can you use NULLIF? Like this.
EXEC SQL
INSERT INTO MYTABLE (NULLABLE_CHAR_ FIELD)
VALUES (NULLIF(:NULLAB LE-CHAR-FIELD,''))
END-EXEC
That is perfect! Thank you!

---
Frank Swarbrick
Senior Developer/Analyst - Mainframe Applications
FirstBank Data Corporation - Lakewood, CO USA
Nov 24 '06 #9
Knut Stolze<st****@d e.ibm.com11/23/06 1:00 AM >>>
>Frank Swarbrick wrote:
>This is pretty darn cool! Much nicer than using "indicator variables"
if
>you don't really care that it's a NULL field.

The main thing to recognize here is that the result is a string and not a
date value. An empty string '' is not a valid date in the first place.
Yes, but within a COBOL program that seems to me to be perfectly
acceptable.
>I don't suppose that there's anything similar going the opposite
direction. In other words,

MOVE SPACES TO NULLABLE-CHAR-FIELD

EXEC SQL
INSERT INTO MYTABLE (NULLABLE_CHAR_ FIELD)
VALUES (NULL_IF_BLANK( :NULLABLE-CHAR-FIELD))
END-EXEC

Does anything like my made up function "NULL_IF_BL ANK" exist, where it
returns the input field unless the input field is all spaces in which
case
>it would return NULL?
If not, I may have to give a shot to making my own. Sounds fairly
simple,
>I think.

You can use the built-in NULLIF function for that.

If you have a string consisting of whitespaces only and you want to have
this string interpreted as NULL date, then a CASE expression is needed:

CASE
WHEN TRANSLATE(str, '', ' ') = ''
THEN NULL
ELSE str
END

If it is permissable to remove whitespaces from the string even if the
string is not empty (and should not be stored as NULL), you can nest
TRANSLATE into NULLIF:

NULLIF(TRANSLA TE(str, '', ' '), '')
I'm not sure I understand why either of these is necessary. I just did the
following:
CREATE TABLE TEST2 (TIMESTAMP1 TIMESTAMP , NULL1 VARCHAR(100) );
EXEC SQL
BEGIN DECLARE SECTION
END-EXEC
01 SQL-FIELDS.
05 TIMESTAMP1 PIC X(26).
05 NULL1VC PIC X(100).
EXEC SQL
END DECLARE SECTION
END-EXEC

PROCEDURE DIVISION.
EXEC SQL
CREATE TABLE TEST2 (TIMESTAMP1 TIMESTAMP , NULL1 VARCHAR(100) );
END-EXEC

MOVE '2006-11-24-11.58.53.012345 '
TO TIMESTAMP1
MOVE 'This is a test' TO NULL1VC
PERFORM INSERT-TEST2
MOVE SPACES TO TIMESTAMP1, NULL1VC
PERFORM INSERT-TEST2
Nov 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
22700
by: Mark | last post by:
Hi I have been trying to convert the week number to a range of dates that I can use. It should be fairly simple for you guru's out there but for us mere mortals it is beyond our grasp. I know that there are different start days of the week so I would presume any function would provide that facility. Hope you can help Mark
8
10413
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have is not much help here either. Googling has given me a little help. This is my current understanding -- I would appreciate any comments or corrections... "" -- this means an empty string when applied to String data type, and also to Variant...
2
5210
by: Annie D via AccessMonster.com | last post by:
Hi, I am using the following SQL statement courtesy of access monster user: 'Terry Kreft ' to convert dates from a text format (20020331 00:00:00) to a recognised date/time format (31/03/2002). SELECT END_DTTM, Left(,4) AS END_DTTM_year, Mid(,5,2) AS END_DTTM_month,
6
15036
by: Paulb1us | last post by:
I want to set a DateTime field to Null before passing it to the DB //First I check to see if anything is in this datarow column, because sometimes we have no data. DateTime dt; if ( datarow.ToString().Length > 0) { //We assume the data is a date dt = (DateTime)datarow; }
5
10605
by: darrel | last post by:
I have the following right now to enter a date into SQL getting the data from some pull down menus: ------------------------------------------------- dim dateCCJApprovedDate as DateTime if cbx_ccjDateNone.Checked = True then dateCCJApprovedDate = ctype("", DateTime) else dateCCJApprovedDate = ctype(ddl_CCJDateMonth.SelectedValue.tostring &
6
12999
by: Dean Slindee | last post by:
I am looking for the "right" way to handle inserting and presenting null date values. Public Const c_NullDate As Date = #12:00:00 AM# If I set the value of a date variable in an SQL Server insert statement to c_NullDate, the insert statement works. When I re-read the row and display the inserted date value in a text box, the string "1/1/1900" is displayed. If one wanted the textbox to display an empty string, how should either the insert...
12
3002
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this and not have it affected by the null? because it is right now causes truncated data at wierd places... but as soon as i manually with a hex editor change char(00) to char(20) in the files it reads prerfectly... which leads me to my 2nd...
7
1756
by: Mike Howard | last post by:
I'm trying to convert the following (simplified) VB.Net code to C#, that makes use of some externally developed COM code written in VB6. VB.Net Code Sub Main() Dim oAPP As Object Dim oApp2 As APP2.Class Dim user As String
3
2947
by: Elliot | last post by:
How can to save a string("13/12/2007") in a field which datatype is date in the table? field = xxx?
0
9697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10906
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10647
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10292
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7828
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5866
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4482
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.