Avatar of shilpi2
shilpi2
 asked on

DateTime sql2

Hi Experts,

I want a datetime variable to be stored as 12:00 am in the sql2 table.
I have done the following:
                        DateTime LastCouponDate = System.Data.SqlTypes.SqlDateTime.MinValue.Value;
                       
                        String.Format("{0:t tt}", LastCouponDate);
This returns lastcoupondate as 1/1/1753 12:00:00 AM.
This gives me some error during processing. How do I get rid of 1/1/1753?

Thanks

Shilpi
C#Microsoft SQL Server.NET Programming

Avatar of undefined
Last Comment
AlokJain0412

8/22/2022 - Mon
kaufmed

There is not Time class, per se. If you only want to work with time values, then use a TimeSpan: http://msdn.microsoft.com/en-us/library/system.timespan.aspx
jonnidip

You can define your SQL field as time data type:

declare @temp as table
( test time )

insert into @temp
select '12:00:00'

select * from @temp

Open in new window

Sql output
The corresponding c# type will be:
System.Data.SqlDbType.Time

Open in new window

shilpi2

ASKER
I cannot make changes to SQL2. In SQL2 the field appears as 12:00:00 am whereas when with c sharp the field appears as 1/1/1753. The result is that I get some problem in processing this record with date 1/1/1753 as the legacy code processes with input as 12:0:00 am.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Lowfatspread

can you confirm which database system you mean by SQL2 ?

if you mean sql server ... then it only supported a datetime datatype until the 2008 version when separate date and time data types where also introduced...

if you want to just reference time from a sql server datetime column then use convert with style 106...
Ark

>>In SQL2 the field appears as 12:00:00<<
In SQL2, SQL3,SQL12345,Oracle,Access and wherever else the field is stored as 01100111011011..
In a TableView, QueryView, DataView etc. it appears according field format and your view format settings.
BTW, System.Data.SqlTypes.SqlDateTime.MinValue.Value equals 01/01/1753 00:00:00. To get 12AM you need System.Data.SqlTypes.SqlDateTime.MinValue.Value.AddHours(12);
To format value as time use LastCouponDate.ToLongTimeString or 0:tt in String.Format
ASKER CERTIFIED SOLUTION
MikeToole

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AlokJain0412

HI,
Read following care fully and as use as suitable
If its not use full then again specify your problem in detail



-- SQL month sequence - SQL date sequence generation with table variable

-- SQL Server cast string to datetime - SQL Server cast datetime to string

-- SQL Server insert default values method

DECLARE @Sequence table (Sequence int identity(1,1))

DECLARE @i int; SET @i = 0

DECLARE @StartDate datetime;

SET @StartDate = CAST(CONVERT(varchar, year(getdate()))+

                 RIGHT('0'+convert(varchar,month(getdate())),2) + '01' AS DATETIME)

WHILE ( @i < 120)

BEGIN

      INSERT @Sequence DEFAULT VALUES

      SET @i = @i + 1

END

SELECT MonthSequence = CAST(DATEADD(month, Sequence,@StartDate) AS varchar)

FROM @Sequence

GO

/* Partial results:

MonthSequence

Jan  1 2012 12:00AM

Feb  1 2012 12:00AM

Mar  1 2012 12:00AM

Apr  1 2012 12:00AM

*/

------------


⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.