I have a problem while inserting time value in the datetime Field.
I want to Insert only time value in this format (08:15:39) into the SQL
Date time Field.
I tried to many ways, I can extract the value in timeonly format by using
this command
Format(now,"HH:mm:ss")
But when I insert it into the Sql Server database, it embadded date value
with it.
the output looks like that "01/01/1900 08:59:00" in that case time is
correct but date is picked up by default, but I don't even want to have date
along with time. and If I want then I already passed the Current Date, so
the current date should be displayed, but why it is displaying the default
date "01/01/1900".
More over I checked to insert the simple current date into the database
with the Insert command, but it still inserts the Default date.
Here is my complete code.
Dim sqlconn As New SqlConnection("Data Source =MSC RHD SERVER;user id
=sa;pwd=testserver;initial catalog = testData")
Dim Sqlcom As New SqlCommand
Try
sqlconn.Open()
Dim m_sql As String
m_sql = "Insert into Logcomms (recdate)"
m_sql = m_sql & " values (" & DateTime.Today & ")"
'MsgBox(m_sql)
Sqlcom.Connection = sqlconn
Sqlcom.CommandText = m_sql
Dim M_result As Integer = Sqlcom.ExecuteNonQuery
Catch ex As Exception
MsgBox(ex.ToString)
Finally
sqlconn.Dispose()
Sqlcom.Dispose()
End Try
*Please Help me it is Urgent. I am new bee in VB.NET
Thanks in Advance
Khurram 15 18849
This is "just how it is". The DateTime datatype in SQL Server is just
that, date and time. 1/1/1900 is the beginning of time with SQL Server,
so you can just strip it out or ignore it.
That's probably not what you wanted, so here is a SQL function to return
only the time. Use like: select dbo.udf_TimeOnly(getdate())
ALTER FUNCTION dbo.udf_TimeOnly
(@inDate DATETIME)
RETURNS VARCHAR(11)
AS
BEGIN
DECLARE @outTime AS VARCHAR(11)
SET @outtime=CONVERT(VARCHAR(8),@indate,8)
IF SUBSTRING(@outTime,1,2)>=12
BEGIN
IF SUBSTRING(@outTime,1,2)>12
SET @outTime=CAST((SUBSTRING(@outTime,1,2)-12) AS VARCHAR) +
SUBSTRING(@outTime,3,6)
SET @outTime = @outTime + ' PM'
END
ELSE
SET @outTime = @outTime + ' AM'
RETURN @outTime
END
"Khurram" <s.*******@gmail.com> wrote in
news:OR**************@TK2MSFTNGP11.phx.gbl: I have a problem while inserting time value in the datetime Field.
I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract the value in timeonly format by using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date value with it. the output looks like that "01/01/1900 08:59:00" in that case time is correct but date is picked up by default, but I don't even want to have date along with time. and If I want then I already passed the Current Date, so the current date should be displayed, but why it is displaying the default date "01/01/1900".
More over I checked to insert the simple current date into the database with the Insert command, but it still inserts the Default date. Here is my complete code. Dim sqlconn As New SqlConnection("Data Source =MSC RHD SERVER;user id =sa;pwd=testserver;initial catalog = testData")
Dim Sqlcom As New SqlCommand
Try
sqlconn.Open()
Dim m_sql As String
m_sql = "Insert into Logcomms (recdate)"
m_sql = m_sql & " values (" & DateTime.Today & ")"
'MsgBox(m_sql)
Sqlcom.Connection = sqlconn
Sqlcom.CommandText = m_sql
Dim M_result As Integer = Sqlcom.ExecuteNonQuery
Catch ex As Exception
MsgBox(ex.ToString)
Finally
sqlconn.Dispose()
Sqlcom.Dispose()
End Try *Please Help me it is Urgent. I am new bee in VB.NET Thanks in Advance
Khurram
Thanks a lot.
But My Requirement is to insert the Time or date with the Current System
Date and time using VB.NET coding.
Thanks a log for telling me PL/SQL coding.
But I hope if you go through my code, all is in VB.net.
Waiting for any more favourable abswers
"cbDevelopment" <de*********@remove.700cb.net> wrote in message
news:Xn***************************@207.46.248.16.. . This is "just how it is". The DateTime datatype in SQL Server is just that, date and time. 1/1/1900 is the beginning of time with SQL Server, so you can just strip it out or ignore it.
That's probably not what you wanted, so here is a SQL function to return only the time. Use like: select dbo.udf_TimeOnly(getdate())
ALTER FUNCTION dbo.udf_TimeOnly (@inDate DATETIME) RETURNS VARCHAR(11) AS BEGIN DECLARE @outTime AS VARCHAR(11)
SET @outtime=CONVERT(VARCHAR(8),@indate,8)
IF SUBSTRING(@outTime,1,2)>=12 BEGIN IF SUBSTRING(@outTime,1,2)>12 SET @outTime=CAST((SUBSTRING(@outTime,1,2)-12) AS VARCHAR) + SUBSTRING(@outTime,3,6)
SET @outTime = @outTime + ' PM' END
ELSE SET @outTime = @outTime + ' AM'
RETURN @outTime END
"Khurram" <s.*******@gmail.com> wrote in news:OR**************@TK2MSFTNGP11.phx.gbl:
I have a problem while inserting time value in the datetime Field.
I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract the value in timeonly format by using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date value with it. the output looks like that "01/01/1900 08:59:00" in that case time is correct but date is picked up by default, but I don't even want to have date along with time. and If I want then I already passed the Current Date, so the current date should be displayed, but why it is displaying the default date "01/01/1900".
More over I checked to insert the simple current date into the database with the Insert command, but it still inserts the Default date. Here is my complete code. Dim sqlconn As New SqlConnection("Data Source =MSC RHD SERVER;user id =sa;pwd=testserver;initial catalog = testData")
Dim Sqlcom As New SqlCommand
Try
sqlconn.Open()
Dim m_sql As String
m_sql = "Insert into Logcomms (recdate)"
m_sql = m_sql & " values (" & DateTime.Today & ")"
'MsgBox(m_sql)
Sqlcom.Connection = sqlconn
Sqlcom.CommandText = m_sql
Dim M_result As Integer = Sqlcom.ExecuteNonQuery
Catch ex As Exception
MsgBox(ex.ToString)
Finally
sqlconn.Dispose()
Sqlcom.Dispose()
End Try *Please Help me it is Urgent. I am new bee in VB.NET Thanks in Advance
Khurram
Khuram,
In addition to cbDevelompment, you can not insert only a time in an SQL
server in datetime format, because there is not a Time format.
You can translate it to a long or whatever other value, however I would not
do it. A DateTime with 01/01/1900 means crazy enough a time withouth a date.
Just to give you from someone else the same answer as cbDevelopment.
Cor
"Khurram" <s.*******@gmail.com>
... Thanks a lot. But My Requirement is to insert the Time or date with the Current System Date and time using VB.NET coding. Thanks a log for telling me PL/SQL coding. But I hope if you go through my code, all is in VB.net. Waiting for any more favourable abswers
"cbDevelopment" <de*********@remove.700cb.net> wrote in message news:Xn***************************@207.46.248.16.. . > This is "just how it is". The DateTime datatype in SQL Server is just > that, date and time. 1/1/1900 is the beginning of time with SQL Server, > so you can just strip it out or ignore it. > > That's probably not what you wanted, so here is a SQL function to return > only the time. Use like: select dbo.udf_TimeOnly(getdate()) > > ALTER FUNCTION dbo.udf_TimeOnly > (@inDate DATETIME) > RETURNS VARCHAR(11) > AS > BEGIN > DECLARE @outTime AS VARCHAR(11) > > SET @outtime=CONVERT(VARCHAR(8),@indate,8) > > IF SUBSTRING(@outTime,1,2)>=12 > BEGIN > IF SUBSTRING(@outTime,1,2)>12 > SET @outTime=CAST((SUBSTRING(@outTime,1,2)-12) AS VARCHAR) + > SUBSTRING(@outTime,3,6) > > SET @outTime = @outTime + ' PM' > END > > ELSE > SET @outTime = @outTime + ' AM' > > RETURN @outTime > END > > > "Khurram" <s.*******@gmail.com> wrote in > news:OR**************@TK2MSFTNGP11.phx.gbl: > >> I have a problem while inserting time value in the datetime Field. >> >> I want to Insert only time value in this format (08:15:39) into the >> SQL >> Date time Field. >> I tried to many ways, I can extract the value in timeonly format by >> using >> this command >> Format(now,"HH:mm:ss") >> But when I insert it into the Sql Server database, it embadded date >> value >> with it. >> the output looks like that "01/01/1900 08:59:00" in that case time >> is >> correct but date is picked up by default, but I don't even want to >> have date along with time. and If I want then I already passed the >> Current Date, so the current date should be displayed, but why it is >> displaying the default date "01/01/1900". >> >> More over I checked to insert the simple current date into the >> database >> with the Insert command, but it still inserts the Default date. >> Here is my complete code. >> Dim sqlconn As New SqlConnection("Data Source =MSC RHD SERVER;user >> id >> =sa;pwd=testserver;initial catalog = testData") >> >> Dim Sqlcom As New SqlCommand >> >> Try >> >> sqlconn.Open() >> >> Dim m_sql As String >> >> m_sql = "Insert into Logcomms (recdate)" >> >> m_sql = m_sql & " values (" & DateTime.Today & ")" >> >> 'MsgBox(m_sql) >> >> Sqlcom.Connection = sqlconn >> >> Sqlcom.CommandText = m_sql >> >> Dim M_result As Integer = Sqlcom.ExecuteNonQuery >> >> Catch ex As Exception >> >> MsgBox(ex.ToString) >> >> Finally >> >> sqlconn.Dispose() >> >> Sqlcom.Dispose() >> >> End Try >> >> >> >> *Please Help me it is Urgent. I am new bee in VB.NET >> Thanks in Advance >> >> Khurram >> >> >> >
If you just need the Time why are you using a DateTime Field...?
instead use a Char field, take the part of the Time you want save an put it
in a Char field.
Have a nice day.
--
David Fúnez
Tegucigalpa, Honduras
"La Piratería Mata Las Ideas"
Desarrollador Cinco Estrellas Visual FoxPro
"Khurram" <s.*******@gmail.com> wrote in message
news:OR**************@TK2MSFTNGP11.phx.gbl... I have a problem while inserting time value in the datetime Field.
I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract the value in timeonly format by
using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date
value with it. the output looks like that "01/01/1900 08:59:00" in that case time is correct but date is picked up by default, but I don't even want to have
date along with time. and If I want then I already passed the Current Date, so the current date should be displayed, but why it is displaying the default date "01/01/1900".
More over I checked to insert the simple current date into the database with the Insert command, but it still inserts the Default date. Here is my complete code. Dim sqlconn As New SqlConnection("Data Source =MSC RHD SERVER;user id =sa;pwd=testserver;initial catalog = testData")
Dim Sqlcom As New SqlCommand
Try
sqlconn.Open()
Dim m_sql As String
m_sql = "Insert into Logcomms (recdate)"
m_sql = m_sql & " values (" & DateTime.Today & ")"
'MsgBox(m_sql)
Sqlcom.Connection = sqlconn
Sqlcom.CommandText = m_sql
Dim M_result As Integer = Sqlcom.ExecuteNonQuery
Catch ex As Exception
MsgBox(ex.ToString)
Finally
sqlconn.Dispose()
Sqlcom.Dispose()
End Try *Please Help me it is Urgent. I am new bee in VB.NET Thanks in Advance
Khurram
OK, let's try something different. You need to store a time value, but
you need the date to always be current? Is that on insert or retrieval?
Regardless, it sounds like you want to do this all in VB.
Here is some code that will always use the current date and will append
whatever time you specify or retrieve to it. Be sure to wrap any
date.parse's in try blocks because you may be working with invalid data
if it's user-submitted.
Hope that helps.
' Insert code
Dim sTime As String
Dim dInDate As Date
Dim dOutDate As Date
Dim dFromDB As Date
sTime = "4:30:00 pm"
dInDate = Date.Parse(Now.Date.ToShortDateString & " " & sTime)
' Extract code
dFromDB = #10/31/2004 6:30:00 PM#
dOutDate = Date.Parse(Now.Date.ToShortDateString & " " & Format(dFromDB,
"HH:mm:ss"))
"Khurram" <s.*******@gmail.com> wrote in
news:Oq**************@TK2MSFTNGP10.phx.gbl: Thanks a lot. But My Requirement is to insert the Time or date with the Current System Date and time using VB.NET coding. Thanks a log for telling me PL/SQL coding. But I hope if you go through my code, all is in VB.net. Waiting for any more favourable abswers
"cbDevelopment" <de*********@remove.700cb.net> wrote in message news:Xn***************************@207.46.248.16.. . > This is "just how it is". The DateTime datatype in SQL Server is > just that, date and time. 1/1/1900 is the beginning of time with > SQL Server, so you can just strip it out or ignore it. > > That's probably not what you wanted, so here is a SQL function to > return only the time. Use like: select > dbo.udf_TimeOnly(getdate()) > > ALTER FUNCTION dbo.udf_TimeOnly > (@inDate DATETIME) > RETURNS VARCHAR(11) > AS > BEGIN > DECLARE @outTime AS VARCHAR(11) > > SET @outtime=CONVERT(VARCHAR(8),@indate,8) > > IF SUBSTRING(@outTime,1,2)>=12 > BEGIN > IF SUBSTRING(@outTime,1,2)>12 > SET @outTime=CAST((SUBSTRING(@outTime,1,2)-12) AS VARCHAR) + > SUBSTRING(@outTime,3,6) > > SET @outTime = @outTime + ' PM' > END > > ELSE > SET @outTime = @outTime + ' AM' > > RETURN @outTime > END > > > "Khurram" <s.*******@gmail.com> wrote in > news:OR**************@TK2MSFTNGP11.phx.gbl: > >> I have a problem while inserting time value in the datetime >> Field. >> >> I want to Insert only time value in this format (08:15:39) into >> the SQL >> Date time Field. >> I tried to many ways, I can extract the value in timeonly format >> by using >> this command >> Format(now,"HH:mm:ss") >> But when I insert it into the Sql Server database, it embadded >> date value >> with it. >> the output looks like that "01/01/1900 08:59:00" in that case >> time is >> correct but date is picked up by default, but I don't even want to >> have date along with time. and If I want then I already passed the >> Current Date, so the current date should be displayed, but why it >> is displaying the default date "01/01/1900". >> >> More over I checked to insert the simple current date into the >> database >> with the Insert command, but it still inserts the Default date. >> Here is my complete code. >> Dim sqlconn As New SqlConnection("Data Source =MSC RHD >> SERVER;user id >> =sa;pwd=testserver;initial catalog = testData") >> >> Dim Sqlcom As New SqlCommand >> >> Try >> >> sqlconn.Open() >> >> Dim m_sql As String >> >> m_sql = "Insert into Logcomms (recdate)" >> >> m_sql = m_sql & " values (" & DateTime.Today & ")" >> >> 'MsgBox(m_sql) >> >> Sqlcom.Connection = sqlconn >> >> Sqlcom.CommandText = m_sql >> >> Dim M_result As Integer = Sqlcom.ExecuteNonQuery >> >> Catch ex As Exception >> >> MsgBox(ex.ToString) >> >> Finally >> >> sqlconn.Dispose() >> >> Sqlcom.Dispose() >> >> End Try >> >> >> >> *Please Help me it is Urgent. I am new bee in VB.NET >> Thanks in Advance >> >> Khurram >> >> >> >
cb Develompment,
Your solution works only in a a part of the world.
Some places the US and Anglo Canadia and surely not the major part of the
world.
Cor
Agreed. Any time you are introducing globalization, there are many design
factors to consider. I did not interpret the original question to require
such consideration.
"Cor Ligthert" <no************@planet.nl> wrote in
news:u6*************@TK2MSFTNGP15.phx.gbl: cb Develompment,
Your solution works only in a a part of the world. Some places the US and Anglo Canadia and surely not the major part of the world.
Cor
Hi,
I tried this code snipset, but it gives me that error, that date is out of
range.
In all cases it gives me the same error, of out of range, I applied three
differant ways to insert the date and time intot it, but no Luck
Please give any more suggestions??
Thanks in advance
Dim sTime As String
Dim dInDate As Date
Dim dOutDate As Date
Dim dFromDB As Date
sTime = "4:30:00 pm"
dInDate = Date.Parse(Now.Date.ToShortDateString & " " & sTime)
Dim m_sql As String
m_sql = "Insert into Logcomms (recdate)"
m_sql = m_sql & " values ('" & CDate(dInDate) & "')" 'First Method
''''m_sql = m_sql & " values (" & dInDate & ")" 'Second Method
''''m_sql = m_sql & " values ('" & dInDate & "')" 'Third Method
Sqlcom.Connection = sqlconn
Sqlcom.CommandText = m_sql
Dim M_result As Integer = Sqlcom.ExecuteNonQuery
"cbDevelopment" <de*********@remove.700cb.net> wrote in message
news:Xn***************************@207.46.248.16.. . OK, let's try something different. You need to store a time value, but you need the date to always be current? Is that on insert or retrieval? Regardless, it sounds like you want to do this all in VB.
Here is some code that will always use the current date and will append whatever time you specify or retrieve to it. Be sure to wrap any date.parse's in try blocks because you may be working with invalid data if it's user-submitted.
Hope that helps.
' Insert code Dim sTime As String Dim dInDate As Date Dim dOutDate As Date Dim dFromDB As Date
sTime = "4:30:00 pm" dInDate = Date.Parse(Now.Date.ToShortDateString & " " & sTime)
' Extract code dFromDB = #10/31/2004 6:30:00 PM# dOutDate = Date.Parse(Now.Date.ToShortDateString & " " & Format(dFromDB, "HH:mm:ss"))
"Khurram" <s.*******@gmail.com> wrote in news:Oq**************@TK2MSFTNGP10.phx.gbl:
Thanks a lot. But My Requirement is to insert the Time or date with the Current System Date and time using VB.NET coding. Thanks a log for telling me PL/SQL coding. But I hope if you go through my code, all is in VB.net. Waiting for any more favourable abswers
"cbDevelopment" <de*********@remove.700cb.net> wrote in message news:Xn***************************@207.46.248.16.. . > This is "just how it is". The DateTime datatype in SQL Server is > just that, date and time. 1/1/1900 is the beginning of time with > SQL Server, so you can just strip it out or ignore it. > > That's probably not what you wanted, so here is a SQL function to > return only the time. Use like: select > dbo.udf_TimeOnly(getdate()) > > ALTER FUNCTION dbo.udf_TimeOnly > (@inDate DATETIME) > RETURNS VARCHAR(11) > AS > BEGIN > DECLARE @outTime AS VARCHAR(11) > > SET @outtime=CONVERT(VARCHAR(8),@indate,8) > > IF SUBSTRING(@outTime,1,2)>=12 > BEGIN > IF SUBSTRING(@outTime,1,2)>12 > SET @outTime=CAST((SUBSTRING(@outTime,1,2)-12) AS VARCHAR) + > SUBSTRING(@outTime,3,6) > > SET @outTime = @outTime + ' PM' > END > > ELSE > SET @outTime = @outTime + ' AM' > > RETURN @outTime > END > > > "Khurram" <s.*******@gmail.com> wrote in > news:OR**************@TK2MSFTNGP11.phx.gbl: > >> I have a problem while inserting time value in the datetime >> Field. >> >> I want to Insert only time value in this format (08:15:39) into >> the SQL >> Date time Field. >> I tried to many ways, I can extract the value in timeonly format >> by using >> this command >> Format(now,"HH:mm:ss") >> But when I insert it into the Sql Server database, it embadded >> date value >> with it. >> the output looks like that "01/01/1900 08:59:00" in that case >> time is >> correct but date is picked up by default, but I don't even want to >> have date along with time. and If I want then I already passed the >> Current Date, so the current date should be displayed, but why it >> is displaying the default date "01/01/1900". >> >> More over I checked to insert the simple current date into the >> database >> with the Insert command, but it still inserts the Default date. >> Here is my complete code. >> Dim sqlconn As New SqlConnection("Data Source =MSC RHD >> SERVER;user id >> =sa;pwd=testserver;initial catalog = testData") >> >> Dim Sqlcom As New SqlCommand >> >> Try >> >> sqlconn.Open() >> >> Dim m_sql As String >> >> m_sql = "Insert into Logcomms (recdate)" >> >> m_sql = m_sql & " values (" & DateTime.Today & ")" >> >> 'MsgBox(m_sql) >> >> Sqlcom.Connection = sqlconn >> >> Sqlcom.CommandText = m_sql >> >> Dim M_result As Integer = Sqlcom.ExecuteNonQuery >> >> Catch ex As Exception >> >> MsgBox(ex.ToString) >> >> Finally >> >> sqlconn.Dispose() >> >> Sqlcom.Dispose() >> >> End Try >> >> >> >> *Please Help me it is Urgent. I am new bee in VB.NET >> Thanks in Advance >> >> Khurram >> >> >> >
What is your locale if not US-en? A perfectly valid response to my post
was that my suggestion was not culture-agnostic.
The code worked fine for me. Try:
m_sql = m_sql & " values ('" & dInDate.ToString & "')"
Is the error a SQL error or a .NET error?
"Khurram" <s.*******@gmail.com> wrote in
news:O5**************@tk2msftngp13.phx.gbl: Hi, I tried this code snipset, but it gives me that error, that date is out of range. In all cases it gives me the same error, of out of range, I applied three differant ways to insert the date and time intot it, but no Luck
Please give any more suggestions??
Thanks in advance Dim sTime As String
Dim dInDate As Date
Dim dOutDate As Date
Dim dFromDB As Date
sTime = "4:30:00 pm"
dInDate = Date.Parse(Now.Date.ToShortDateString & " " & sTime)
Dim m_sql As String
m_sql = "Insert into Logcomms (recdate)"
m_sql = m_sql & " values ('" & CDate(dInDate) & "')" 'First Method
''''m_sql = m_sql & " values (" & dInDate & ")" 'Second Method
''''m_sql = m_sql & " values ('" & dInDate & "')" 'Third Method
Sqlcom.Connection = sqlconn
Sqlcom.CommandText = m_sql
Dim M_result As Integer = Sqlcom.ExecuteNonQuery "cbDevelopment" <de*********@remove.700cb.net> wrote in message news:Xn***************************@207.46.248.16.. . > OK, let's try something different. You need to store a time value, > but you need the date to always be current? Is that on insert or > retrieval? Regardless, it sounds like you want to do this all in > VB. > > Here is some code that will always use the current date and will > append whatever time you specify or retrieve to it. Be sure to > wrap any date.parse's in try blocks because you may be working with > invalid data if it's user-submitted. > > Hope that helps. > > ' Insert code > Dim sTime As String > Dim dInDate As Date > Dim dOutDate As Date > Dim dFromDB As Date > > sTime = "4:30:00 pm" > dInDate = Date.Parse(Now.Date.ToShortDateString & " " & sTime) > > ' Extract code > dFromDB = #10/31/2004 6:30:00 PM# > dOutDate = Date.Parse(Now.Date.ToShortDateString & " " & > Format(dFromDB, "HH:mm:ss")) > > > "Khurram" <s.*******@gmail.com> wrote in > news:Oq**************@TK2MSFTNGP10.phx.gbl: > >> Thanks a lot. >> But My Requirement is to insert the Time or date with the >> Current System >> Date and time using VB.NET coding. >> Thanks a log for telling me PL/SQL coding. >> But I hope if you go through my code, all is in VB.net. >> Waiting for any more favourable abswers >> >> "cbDevelopment" <de*********@remove.700cb.net> wrote in message >> news:Xn***************************@207.46.248.16.. . >> > This is "just how it is". The DateTime datatype in SQL Server >> > is just that, date and time. 1/1/1900 is the beginning of time >> > with SQL Server, so you can just strip it out or ignore it. >> > >> > That's probably not what you wanted, so here is a SQL function >> > to return only the time. Use like: select >> > dbo.udf_TimeOnly(getdate()) >> > >> > ALTER FUNCTION dbo.udf_TimeOnly >> > (@inDate DATETIME) >> > RETURNS VARCHAR(11) >> > AS >> > BEGIN >> > DECLARE @outTime AS VARCHAR(11) >> > >> > SET @outtime=CONVERT(VARCHAR(8),@indate,8) >> > >> > IF SUBSTRING(@outTime,1,2)>=12 >> > BEGIN >> > IF SUBSTRING(@outTime,1,2)>12 >> > SET @outTime=CAST((SUBSTRING(@outTime,1,2)-12) AS VARCHAR) + >> > SUBSTRING(@outTime,3,6) >> > >> > SET @outTime = @outTime + ' PM' >> > END >> > >> > ELSE >> > SET @outTime = @outTime + ' AM' >> > >> > RETURN @outTime >> > END >> > >> > >> > "Khurram" <s.*******@gmail.com> wrote in >> > news:OR**************@TK2MSFTNGP11.phx.gbl: >> > >> >> I have a problem while inserting time value in the datetime >> >> Field. >> >> >> >> I want to Insert only time value in this format (08:15:39) >> >> into the SQL >> >> Date time Field. >> >> I tried to many ways, I can extract the value in timeonly >> >> format by using >> >> this command >> >> Format(now,"HH:mm:ss") >> >> But when I insert it into the Sql Server database, it >> >> embadded date value >> >> with it. >> >> the output looks like that "01/01/1900 08:59:00" in that >> >> case time is >> >> correct but date is picked up by default, but I don't even >> >> want to have date along with time. and If I want then I >> >> already passed the Current Date, so the current date should be >> >> displayed, but why it is displaying the default date >> >> "01/01/1900". >> >> >> >> More over I checked to insert the simple current date into >> >> the database >> >> with the Insert command, but it still inserts the Default >> >> date. >> >> Here is my complete code. >> >> Dim sqlconn As New SqlConnection("Data Source =MSC RHD >> >> SERVER;user id >> >> =sa;pwd=testserver;initial catalog = testData") >> >> >> >> Dim Sqlcom As New SqlCommand >> >> >> >> Try >> >> >> >> sqlconn.Open() >> >> >> >> Dim m_sql As String >> >> >> >> m_sql = "Insert into Logcomms (recdate)" >> >> >> >> m_sql = m_sql & " values (" & DateTime.Today & ")" >> >> >> >> 'MsgBox(m_sql) >> >> >> >> Sqlcom.Connection = sqlconn >> >> >> >> Sqlcom.CommandText = m_sql >> >> >> >> Dim M_result As Integer = Sqlcom.ExecuteNonQuery >> >> >> >> Catch ex As Exception >> >> >> >> MsgBox(ex.ToString) >> >> >> >> Finally >> >> >> >> sqlconn.Dispose() >> >> >> >> Sqlcom.Dispose() >> >> >> >> End Try >> >> >> >> >> >> >> >> *Please Help me it is Urgent. I am new bee in VB.NET >> >> Thanks in Advance >> >> >> >> Khurram >> >> >> >> >> >> >> > >> >> >
Thanks for your reply.
My Local is New Zealand. GMT +12
I tried that code too, it gives me that sqlclient Exception.
A conversion from character to Datetime is out of range value.
Any more thought please.
Thanks is advance
"cbDevelopment" <de*********@remove.700cb.net> wrote in message
news:Xn***************************@207.46.248.16.. . What is your locale if not US-en? A perfectly valid response to my post was that my suggestion was not culture-agnostic.
The code worked fine for me. Try:
m_sql = m_sql & " values ('" & dInDate.ToString & "')"
Is the error a SQL error or a .NET error?
"Khurram" <s.*******@gmail.com> wrote in news:O5**************@tk2msftngp13.phx.gbl:
Hi, I tried this code snipset, but it gives me that error, that date is out of range. In all cases it gives me the same error, of out of range, I applied three differant ways to insert the date and time intot it, but no Luck
Please give any more suggestions??
Thanks in advance Dim sTime As String
Dim dInDate As Date
Dim dOutDate As Date
Dim dFromDB As Date
sTime = "4:30:00 pm"
dInDate = Date.Parse(Now.Date.ToShortDateString & " " & sTime)
Dim m_sql As String
m_sql = "Insert into Logcomms (recdate)"
m_sql = m_sql & " values ('" & CDate(dInDate) & "')" 'First Method
''''m_sql = m_sql & " values (" & dInDate & ")" 'Second Method
''''m_sql = m_sql & " values ('" & dInDate & "')" 'Third Method
Sqlcom.Connection = sqlconn
Sqlcom.CommandText = m_sql
Dim M_result As Integer = Sqlcom.ExecuteNonQuery "cbDevelopment" <de*********@remove.700cb.net> wrote in message news:Xn***************************@207.46.248.16.. . > OK, let's try something different. You need to store a time value, > but you need the date to always be current? Is that on insert or > retrieval? Regardless, it sounds like you want to do this all in > VB. > > Here is some code that will always use the current date and will > append whatever time you specify or retrieve to it. Be sure to > wrap any date.parse's in try blocks because you may be working with > invalid data if it's user-submitted. > > Hope that helps. > > ' Insert code > Dim sTime As String > Dim dInDate As Date > Dim dOutDate As Date > Dim dFromDB As Date > > sTime = "4:30:00 pm" > dInDate = Date.Parse(Now.Date.ToShortDateString & " " & sTime) > > ' Extract code > dFromDB = #10/31/2004 6:30:00 PM# > dOutDate = Date.Parse(Now.Date.ToShortDateString & " " & > Format(dFromDB, "HH:mm:ss")) > > > "Khurram" <s.*******@gmail.com> wrote in > news:Oq**************@TK2MSFTNGP10.phx.gbl: > >> Thanks a lot. >> But My Requirement is to insert the Time or date with the >> Current System >> Date and time using VB.NET coding. >> Thanks a log for telling me PL/SQL coding. >> But I hope if you go through my code, all is in VB.net. >> Waiting for any more favourable abswers >> >> "cbDevelopment" <de*********@remove.700cb.net> wrote in message >> news:Xn***************************@207.46.248.16.. . >> > This is "just how it is". The DateTime datatype in SQL Server >> > is just that, date and time. 1/1/1900 is the beginning of time >> > with SQL Server, so you can just strip it out or ignore it. >> > >> > That's probably not what you wanted, so here is a SQL function >> > to return only the time. Use like: select >> > dbo.udf_TimeOnly(getdate()) >> > >> > ALTER FUNCTION dbo.udf_TimeOnly >> > (@inDate DATETIME) >> > RETURNS VARCHAR(11) >> > AS >> > BEGIN >> > DECLARE @outTime AS VARCHAR(11) >> > >> > SET @outtime=CONVERT(VARCHAR(8),@indate,8) >> > >> > IF SUBSTRING(@outTime,1,2)>=12 >> > BEGIN >> > IF SUBSTRING(@outTime,1,2)>12 >> > SET @outTime=CAST((SUBSTRING(@outTime,1,2)-12) AS VARCHAR) + >> > SUBSTRING(@outTime,3,6) >> > >> > SET @outTime = @outTime + ' PM' >> > END >> > >> > ELSE >> > SET @outTime = @outTime + ' AM' >> > >> > RETURN @outTime >> > END >> > >> > >> > "Khurram" <s.*******@gmail.com> wrote in >> > news:OR**************@TK2MSFTNGP11.phx.gbl: >> > >> >> I have a problem while inserting time value in the datetime >> >> Field. >> >> >> >> I want to Insert only time value in this format (08:15:39) >> >> into the SQL >> >> Date time Field. >> >> I tried to many ways, I can extract the value in timeonly >> >> format by using >> >> this command >> >> Format(now,"HH:mm:ss") >> >> But when I insert it into the Sql Server database, it >> >> embadded date value >> >> with it. >> >> the output looks like that "01/01/1900 08:59:00" in that >> >> case time is >> >> correct but date is picked up by default, but I don't even >> >> want to have date along with time. and If I want then I >> >> already passed the Current Date, so the current date should be >> >> displayed, but why it is displaying the default date >> >> "01/01/1900". >> >> >> >> More over I checked to insert the simple current date into >> >> the database >> >> with the Insert command, but it still inserts the Default >> >> date. >> >> Here is my complete code. >> >> Dim sqlconn As New SqlConnection("Data Source =MSC RHD >> >> SERVER;user id >> >> =sa;pwd=testserver;initial catalog = testData") >> >> >> >> Dim Sqlcom As New SqlCommand >> >> >> >> Try >> >> >> >> sqlconn.Open() >> >> >> >> Dim m_sql As String >> >> >> >> m_sql = "Insert into Logcomms (recdate)" >> >> >> >> m_sql = m_sql & " values (" & DateTime.Today & ")" >> >> >> >> 'MsgBox(m_sql) >> >> >> >> Sqlcom.Connection = sqlconn >> >> >> >> Sqlcom.CommandText = m_sql >> >> >> >> Dim M_result As Integer = Sqlcom.ExecuteNonQuery >> >> >> >> Catch ex As Exception >> >> >> >> MsgBox(ex.ToString) >> >> >> >> Finally >> >> >> >> sqlconn.Dispose() >> >> >> >> Sqlcom.Dispose() >> >> >> >> End Try >> >> >> >> >> >> >> >> *Please Help me it is Urgent. I am new bee in VB.NET >> >> Thanks in Advance >> >> >> >> Khurram >> >> >> >> >> >> >> > >> >> >
OK, so you are in a locale that formats dates as DD/MM/YYYY. It sounds
like your SQL Server and your application server might have different
regional settings. the .ToString method formats the date to the current
locale and we did that properly, but the server isn't accepting it.
If this is the problem, this might help:
imports system.globalization
imports system.threading
Dim sTime As String
Dim dInDate As Date
Dim dOutDate As Date
Dim dFromDB As Date
Dim m_sql As String
dim ciOriginal as cultureinfo
dim ciDatabase as cultureinfo
' Define the current date settings and the database settings
ciOriginal = thread.currentthread.currentculture
ciDatabase = CultureInfo.CreateSpecificCulture("en-US")
' Set the current thread to use the db settings
Thread.CurrentThread.CurrentCulture = ciDatabase
sTime = "4:30:00 pm"
dInDate = Date.Parse(Now.Date.ToShortDateString & " " & sTime)
m_sql = "Insert into Logcomms (recdate)"
m_sql = m_sql & " values ('" & CDate(dInDate) & "')" 'First
Sqlcom.Connection = sqlconn
Sqlcom.CommandText = m_sql
Dim M_result As Integer = Sqlcom.ExecuteNonQuery
' return to original settings
Thread.CurrentThread.CurrentCulture = ciOriginal
And Cor, if you're watching, you're probably laughing right now...
"Khurram" <s.*******@gmail.com> wrote in
news:Oc**************@TK2MSFTNGP10.phx.gbl: Thanks for your reply. My Local is New Zealand. GMT +12
I tried that code too, it gives me that sqlclient Exception.
A conversion from character to Datetime is out of range value.
Any more thought please.
Thanks is advance
"cbDevelopment" <de*********@remove.700cb.net> wrote in message news:Xn***************************@207.46.248.16.. . > What is your locale if not US-en? A perfectly valid response to my > post was that my suggestion was not culture-agnostic. > > The code worked fine for me. Try: > > m_sql = m_sql & " values ('" & dInDate.ToString & "')" > > Is the error a SQL error or a .NET error? > > > "Khurram" <s.*******@gmail.com> wrote in > news:O5**************@tk2msftngp13.phx.gbl: > >>
Thanks a Lot cbDevelopment.
It works like Magic ;) Thanks a lot again, with heaps of prayers for you
all who helped me
Now more over to that, I want to insert the Time Value in my database.
I can't use the Char Format, I have to use the Same datetime field, but
the data I require in that that is Time Only.
I tried only to insert the Time, but It Insert Date with it.
can you give me any idea, that how can I insert only Time value without
date.
i tested it with Old VB6 code, and it works and inserted only timevalue.
But in VB.net I can't.
I hope you must have solution for that too.
Thanks in advance.
Khurram
"cbDevelopment" <de*********@remove.700cb.net> wrote in message
news:Xn***************************@207.46.248.16.. . OK, so you are in a locale that formats dates as DD/MM/YYYY. It sounds like your SQL Server and your application server might have different regional settings. the .ToString method formats the date to the current locale and we did that properly, but the server isn't accepting it.
If this is the problem, this might help:
imports system.globalization imports system.threading
Dim sTime As String Dim dInDate As Date Dim dOutDate As Date Dim dFromDB As Date Dim m_sql As String dim ciOriginal as cultureinfo dim ciDatabase as cultureinfo
' Define the current date settings and the database settings ciOriginal = thread.currentthread.currentculture ciDatabase = CultureInfo.CreateSpecificCulture("en-US")
' Set the current thread to use the db settings Thread.CurrentThread.CurrentCulture = ciDatabase
sTime = "4:30:00 pm" dInDate = Date.Parse(Now.Date.ToShortDateString & " " & sTime) m_sql = "Insert into Logcomms (recdate)" m_sql = m_sql & " values ('" & CDate(dInDate) & "')" 'First
Sqlcom.Connection = sqlconn Sqlcom.CommandText = m_sql
Dim M_result As Integer = Sqlcom.ExecuteNonQuery
' return to original settings Thread.CurrentThread.CurrentCulture = ciOriginal
And Cor, if you're watching, you're probably laughing right now...
"Khurram" <s.*******@gmail.com> wrote in news:Oc**************@TK2MSFTNGP10.phx.gbl:
Thanks for your reply. My Local is New Zealand. GMT +12
I tried that code too, it gives me that sqlclient Exception.
A conversion from character to Datetime is out of range value.
Any more thought please.
Thanks is advance
"cbDevelopment" <de*********@remove.700cb.net> wrote in message news:Xn***************************@207.46.248.16.. . > What is your locale if not US-en? A perfectly valid response to my > post was that my suggestion was not culture-agnostic. > > The code worked fine for me. Try: > > m_sql = m_sql & " values ('" & dInDate.ToString & "')" > > Is the error a SQL error or a .NET error? > > > "Khurram" <s.*******@gmail.com> wrote in > news:O5**************@tk2msftngp13.phx.gbl: > >>
Thanks David,
As I already told you that My Application was already Built In In VB6
code, and there are millions of records already entered into the database.
And also My Clietn application is Running in More than Ten Departments for
data entering.
So in case of change the DateTime field to Char will ended up to change
the Time value as a Charachter value.
AnyWay Thanks for you Idea.
"David Fúnez" <df****@hotmail.com> wrote in message
news:e3**************@TK2MSFTNGP11.phx.gbl... If you just need the Time why are you using a DateTime Field...? instead use a Char field, take the part of the Time you want save an
put it in a Char field.
Have a nice day.
-- David Fúnez Tegucigalpa, Honduras
"La Piratería Mata Las Ideas" Desarrollador Cinco Estrellas Visual FoxPro
"Khurram" <s.*******@gmail.com> wrote in message news:OR**************@TK2MSFTNGP11.phx.gbl... I have a problem while inserting time value in the datetime Field.
I want to Insert only time value in this format (08:15:39) into the
SQL Date time Field. I tried to many ways, I can extract the value in timeonly format by using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date value with it. the output looks like that "01/01/1900 08:59:00" in that case time is correct but date is picked up by default, but I don't even want to have date along with time. and If I want then I already passed the Current Date,
so the current date should be displayed, but why it is displaying the
default date "01/01/1900".
More over I checked to insert the simple current date into the
database with the Insert command, but it still inserts the Default date. Here is my complete code. Dim sqlconn As New SqlConnection("Data Source =MSC RHD SERVER;user id =sa;pwd=testserver;initial catalog = testData")
Dim Sqlcom As New SqlCommand
Try
sqlconn.Open()
Dim m_sql As String
m_sql = "Insert into Logcomms (recdate)"
m_sql = m_sql & " values (" & DateTime.Today & ")"
'MsgBox(m_sql)
Sqlcom.Connection = sqlconn
Sqlcom.CommandText = m_sql
Dim M_result As Integer = Sqlcom.ExecuteNonQuery
Catch ex As Exception
MsgBox(ex.ToString)
Finally
sqlconn.Dispose()
Sqlcom.Dispose()
End Try *Please Help me it is Urgent. I am new bee in VB.NET Thanks in Advance
Khurram
In article <u1**************@TK2MSFTNGP15.phx.gbl>, Khurram
<s.*******@gmail.com> writes Thanks a Lot cbDevelopment. It works like Magic ;) Thanks a lot again, with heaps of prayers for you all who helped me
If you use a Stored Procedure that accepts parameters to pass the date,
you shouldn't need to do any adjustment of the date, no matter what time
zone you're in, as you pass it the variable, not a string representation
of a date.
I prefer these because:
a) I think they're faster
b) They're more secure and
c) You can pass things like dates or names like O'Brien to them without
having to do stuff like replace ' with ''.
Sorry, I don't have time to post an example here, but look up
parameterized queries or some such and you should find enough help.
If you really must build SQL "on the fly", you can even write it to
include parameters.
Now more over to that, I want to insert the Time Value in my database. I can't use the Char Format, I have to use the Same datetime field, but the data I require in that that is Time Only. I tried only to insert the Time, but It Insert Date with it. can you give me any idea, that how can I insert only Time value without date. i tested it with Old VB6 code, and it works and inserted only timevalue.
Repeating what has already been said, in a different way:
The DateTime field in SQL Server holds a date and a time. Period.
If you pass it just a time, SQL Server will store this time, but will
add a default date to it e.g. 1/1/1900
This is the same for VB6 as for VB.NET.
If you're only interested in the date, just pass a date to SQL Server.
If you read the data back, ignore the date part and use only the time
part.
HTH
--
No Sheds
Thanks a Lot. I got it now.
Thanks you very Very Much
"No Sheds" <me@privacy.net> wrote in message
news:E9**************@hst.me.uk... In article <u1**************@TK2MSFTNGP15.phx.gbl>, Khurram <s.*******@gmail.com> writes Thanks a Lot cbDevelopment. It works like Magic ;) Thanks a lot again, with heaps of prayers for
youall who helped me If you use a Stored Procedure that accepts parameters to pass the date, you shouldn't need to do any adjustment of the date, no matter what time zone you're in, as you pass it the variable, not a string representation of a date.
I prefer these because: a) I think they're faster b) They're more secure and c) You can pass things like dates or names like O'Brien to them without having to do stuff like replace ' with ''.
Sorry, I don't have time to post an example here, but look up parameterized queries or some such and you should find enough help.
If you really must build SQL "on the fly", you can even write it to include parameters.
Now more over to that, I want to insert the Time Value in my database. I can't use the Char Format, I have to use the Same datetime field,
butthe data I require in that that is Time Only. I tried only to insert the Time, but It Insert Date with it. can you give me any idea, that how can I insert only Time value
withoutdate. i tested it with Old VB6 code, and it works and inserted only
timevalue. Repeating what has already been said, in a different way: The DateTime field in SQL Server holds a date and a time. Period.
If you pass it just a time, SQL Server will store this time, but will add a default date to it e.g. 1/1/1900
This is the same for VB6 as for VB.NET.
If you're only interested in the date, just pass a date to SQL Server. If you read the data back, ignore the date part and use only the time part.
HTH -- No Sheds This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ryan.mclean |
last post by:
Hi all, I am new to using sql server and parameterized sql. I am
hoping to be returned the value of a column that has been inserted.
Here is my statement
strSqlInsetrtTrack = _
"INSERT INTO...
|
by: Jerome |
last post by:
Hallo,
I know a lot has already been told about date/time fields in a database but
still confuses me, specif when dealing with SQLserver(Express).
It seems that sqlserver only accepts the date in...
|
by: Jim in Arizona |
last post by:
I have a gridview that's being populated from an access db query. The
problem I'm having is that the date/time fields in access that are
populating the gridview are showing both date and time, when...
|
by: Paul |
last post by:
I'm sure this isn't a difficult question, but I've been struggling trying to
insert the current date into a sql server field. I've tried numerous
suggestions that I've seen in newsgroups and...
|
by: purnimakhamri |
last post by:
hi can any body tell the code for date time picker using calendar control .Am using ASP.NET 2.0 ..calandar control must be in hidden field ,if i click on that it will pop up and display the date and...
|
by: Rehana |
last post by:
I am having problem,while inserting date using insert query...In front end am using c#.net and my database is in ms-access..
if am pass date in dd/mm/yy format in insert query..it store as mm/dd/yy...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |