Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date calculations 2

Status
Not open for further replies.

Gaylord

Technical User
Sep 26, 2002
49
0
0
DE
Hi,

I am having a few problems with the formatting of dates using a SQL trigger.... I get todays date, no problem, then I add a day to it, try and format it and it comes across in a totally different format, for example...

I want the format to be 12/12/2004 and it gives me Dec 12 2004 12:00am

The code I have currently used is...

dateadd(d, 1, convert(datetime, @outputdate, 103))

Which works ok, but I just can't get it to go into my database (column is varchar)

Hope someone can help me as I feel like I am knocking my head against a wall!

Thanks

Jamie
 
Firstly, why aren't you using a datetime column to hold this date data?

If you really want to store it as character data in dd/mm/yyyy format then use:

Code:
CONVERT(varchar, GETDATE() + 1, 103)

(Use style 101 for mm/dd/yyyy format).

--James
 
First of all, the style portion of CONVERT only works if you are converting to a string (char,varchar,nchar,nvarchar), but not if you are converting to DATETIME. DATETIME is not stored in any specific format. It's an 8-byte value (see the BOL for more information). When you display a DATETIME value, it displays it using the COLLATION you set SQL Server to use. UNLESS you convert it to a string and provide the style information.

So, you say you are trying to store the DATETIME as a VARCHAR. Why? You should always store DATETIME as DATETIME. Why? 1. you can't do date calculations on strings. 2. You can't sort dates correctly when they are strings.

But if you really have to store it as VARCHAR...

[CODE}
CONVERT(VARCHAR(7),(dateadd(d, 1, convert(datetime, @outputdate))), 103)
[/CODE}

That will store JUST the date in the format dd/mm/yyyy. If you want the time also, you will need to increase the value for VARCHAR.

-SQLBill

BOL=Books OnLine=Microsoft SQL Server's HELP
Installed as part of the Client Tools
Found at Start>Programs>Microsoft SQL Server>Books OnLine

Posting advice: FAQ481-4875
 
r937,

Okay, but how can it when the DATETIME is not stored in a date/time format? You can tell SQL Server to convert it to style 'whatever' but it ends up being stored as an 8-byte value with no format. So, if you convert it to 101 or 103 it still ends up being stored the exact same way.

The BOL even says in the style paragraph for CONVERT, that style is for conversions from DATETIME or SMALLDATETIME to character data. It doesn't say anything about the reverse.

-SQLBill
 
That's what I used to think as well, but I believe the point was made and demonstrated on this forum a few months back that the convert function will use the style value when deciding whether to interpret "01/02/03" as "Jan 2, 2003", "Feb 1, 2003", or "Feb 3, 2001".

select convert(datetime, '01/02/03', 1)
select convert(datetime, '01/02/03', 3)
select convert(datetime, '01/02/03', 11)

Results:
2003-01-02 00:00:00.000
2003-02-01 00:00:00.000
2001-02-03 00:00:00.000

but i don't have the previous url bookmarked

rudy | r937.com | Ask the Expert | Premium SQL Articles
SQL for Database-Driven Web Sites (course starts January 9 2005)
 
Good tip rudy.
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Thank you Rudy. I learned something new....just like the BOL to not have it right.

-SQLBill
 
Yeah, that was good. Thanks, Rudy!

As SQLBill said, BOL is deficient in covering this.

BOL said:
In the table, the two columns on the left represent the style values for datetime or smalldatetime conversion to character data. Add 100 to a style value to get a four-place year that includes the century (yyyy).

Nothing about conversion from character to datetime. Annoying!

-------------------------------------
It is better to have honor than a good reputation.
(Reputation is what other people think about you. Honor is what you know about yourself.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top