473,651 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert/insert NULL date value into MS SQL?

I have the following right now to enter a date into SQL getting the data
from some pull down menus:

-------------------------------------------------
dim dateCCJApproved Date as DateTime

if cbx_ccjDateNone .Checked = True then
dateCCJApproved Date = ctype("", DateTime)
else
dateCCJApproved Date = ctype(ddl_CCJDa teMonth.Selecte dValue.tostring &
"/01/" & ddl_CCJDateYear .SelectedValue. tostring,
System.Data.Sql Types.SqlDateTi me)
End If
-------------------------------------------------

That works if there is a date to enter. But fails if there isn't, as "" is a
string and can't be converted to a date/time.

So, I did a bit of googling, and came up with this:

-------------------------------------------------
dim dateCCJApproved Date as System.Data.Sql Types.SqlDateTi me

if cbx_ccjDateNone .Checked = True then
dateCCJApproved Date = System.Data.Sql Types.SqlDateTi me.null
else
dateCCJApproved Date = ctype(ddl_CCJDa teMonth.Selecte dValue.tostring &
"/01/" & ddl_CCJDateYear .SelectedValue. tostring,
System.Data.Sql Types.SqlDateTi me)
End If
-------------------------------------------------

But I have the opposite problem...I can use the null value, but I can't
convert the second set of data to SQLDateTime.

So, I seem to be trying to use/cast two different types of data to the same
field format in SQL and hence my problem. I'm guessing the second method is
a better approach, but it appears I need to do some sort of intermediate
cast/conversion. Am I on the right track with that line of thinking?

-Darrel

Nov 19 '05 #1
5 10594

command.Paramet ers.Add("@date" , SqlDbType.DateT ime)
if cbx_ccjDateNone .Checked = True then
command.Paramet ers("@date").Va lue = DbNull.Value
else
command.Paramet ers("@date").Va lue =
cdate(ddl_CCJDa teMonth.Selecte dValue.tostring & "/01/" &
ddl_CCJDateYear .SelectedValue. tostring)
end if
hopefully nullable types in 2.0 will make this cleaner..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"darrel" <no*****@hotmai l.com> wrote in message
news:e3******** *****@TK2MSFTNG P09.phx.gbl...
I have the following right now to enter a date into SQL getting the data
from some pull down menus:

-------------------------------------------------
dim dateCCJApproved Date as DateTime

if cbx_ccjDateNone .Checked = True then
dateCCJApproved Date = ctype("", DateTime)
else
dateCCJApproved Date = ctype(ddl_CCJDa teMonth.Selecte dValue.tostring &
"/01/" & ddl_CCJDateYear .SelectedValue. tostring,
System.Data.Sql Types.SqlDateTi me)
End If
-------------------------------------------------

That works if there is a date to enter. But fails if there isn't, as "" is
a
string and can't be converted to a date/time.

So, I did a bit of googling, and came up with this:

-------------------------------------------------
dim dateCCJApproved Date as System.Data.Sql Types.SqlDateTi me

if cbx_ccjDateNone .Checked = True then
dateCCJApproved Date = System.Data.Sql Types.SqlDateTi me.null
else
dateCCJApproved Date = ctype(ddl_CCJDa teMonth.Selecte dValue.tostring &
"/01/" & ddl_CCJDateYear .SelectedValue. tostring,
System.Data.Sql Types.SqlDateTi me)
End If
-------------------------------------------------

But I have the opposite problem...I can use the null value, but I can't
convert the second set of data to SQLDateTime.

So, I seem to be trying to use/cast two different types of data to the
same
field format in SQL and hence my problem. I'm guessing the second method
is
a better approach, but it appears I need to do some sort of intermediate
cast/conversion. Am I on the right track with that line of thinking?

-Darrel

Nov 19 '05 #2
> command.Paramet ers.Add("@date" , SqlDbType.DateT ime)
if cbx_ccjDateNone .Checked = True then
command.Paramet ers("@date").Va lue = DbNull.Value
else
command.Paramet ers("@date").Va lue =
cdate(ddl_CCJDa teMonth.Selecte dValue.tostring & "/01/" &
ddl_CCJDateYear .SelectedValue. tostring)
end if


So the solution is to use stored procedures?

-Darrel
Nov 19 '05 #3
Parameters can be used with inline sql as well...there's really no excuse
not to use them (and plenty of reasons to do it)

dim c as new SqlCommand("SEL ECT * FROM Blah WHERE x = @Date")
c.Parameters.Ad d("@Date", SqlDbType.DateT ime).Value = SomeValue

works perfectly.

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"darrel" <no*****@hotmai l.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
command.Paramet ers.Add("@date" , SqlDbType.DateT ime)
if cbx_ccjDateNone .Checked = True then
command.Paramet ers("@date").Va lue = DbNull.Value
else
command.Paramet ers("@date").Va lue =
cdate(ddl_CCJDa teMonth.Selecte dValue.tostring & "/01/" &
ddl_CCJDateYear .SelectedValue. tostring)
end if


So the solution is to use stored procedures?

-Darrel

Nov 19 '05 #4
You could also use a regular expression validator and not even allow
the call to the sp to happen if the date is not a valid date.

Nov 19 '05 #5
> dim c as new SqlCommand("SEL ECT * FROM Blah WHERE x = @Date")
c.Parameters.Ad d("@Date", SqlDbType.DateT ime).Value = SomeValue


Thanks, Carl. I definitely need to start playing with paramaters.

-Darrel
Nov 19 '05 #6

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

Similar topics

3
12040
by: jason | last post by:
I need to deposit and empty dummy value into a date/time field within Access using INSERT. However, I pick up a data type error if I attempt to insert a NULL value for this particular date time field.... What are my options here.....I need the date to come up empty.....what can I do? Many thanks Jason
19
7271
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below. Specifically, I have a problem with this portion in the WHERE clause: DATEADD(Day,tblMyEventTableName.ReminderDays, @DateNow) Between CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) + '/' + str(DATEPART(Day, tblMyEventTableName.TaskDateTime)) + '/'...
4
41582
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I do my insert into 3 different table all using the same uniqueID. I can't use the @@identity function because my application uses a connection pool and it's not garanteed that a connection won't be used
9
4041
by: cavassinif | last post by:
I need to dynamic select a column in which insert a vale based on a parameter value, I have this code, but it throws an incorrect syntax error. How do I dinamically select a column to insert based on a parameter? Create PROCEDURE dbo.UpdateDetalleOT ( @eotId int, )
5
3780
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public List<RoleData> GetRoles() { return GetRoles(null, false); }
10
39001
by: satishrajana | last post by:
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.
7
19210
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
5
3450
by: =?Utf-8?B?bXBhaW5l?= | last post by:
Hello, I am completely lost as to why I can't update a DropDownList inside a DetailsView after I perform an insert into an object datasource. I tried to simply it down to the core demostration: default.aspx:
2
5033
by: wizardry | last post by:
hello - i'm trying to insert a blob into my table, it will insert but the string that i insert when i query the inserted data returns null with 0 bytes in the column. I have other tables set up this way and i'm able to insert text data into it with no problems. I've checked the database design its basiclly a duplicate. I did have to remove my not null on the long blob element. but other then that, i don't understand why this is...
0
8803
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...
0
8581
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
7298
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...
0
5612
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
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.