Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server ASP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server ASP Discussions about ASP programming with Microsoft's SQL Server. For more ASP forums, see the ASP forum category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server ASP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 25th, 2006, 05:37 AM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default Changing Data Type Behaviour

Good day

I have a populated dateTime field. The value stored is conventional Aussie date format:

24/08/2006 10:25:13 PM

My objective was to change the data type to a varchar. The reason for this is to store the update date in the form of a commer delimited string (there is a good reason for this). Is there a way to preserve the date value above while converting the data type to a varchar? When I do this the values change to:

8 August 2006...

Lucky I had a back up to restore the original date formats. To achieve my objective I had to:
>get the unique ID and the date value into temporary session variables (ASP)
>Using EM, change the data type to varchar
>Then loop through my session variables updating the field with the original values

Objective achieved however there must be a quicker way?

Wind is your friend
Matt
__________________
Wind is your friend
Matt
Reply With Quote
  #2 (permalink)  
Old August 25th, 2006, 08:03 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Ya there is a much easier way, if you are using the US format it would be:

convert (varchar (10), [datetimefield], 101) as [field]

For you, you are going to want to use the British/French standard to maintain dd/mm/yyyy:

convert (varchar (10), [datetimefield], 103) as [field]

Just remember that this will only pull your date information, the time information will not be returned from this.

hth

"The one language all programmers understand is profanity."
Reply With Quote
  #3 (permalink)  
Old August 26th, 2006, 12:19 AM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Thank you for your reply. So no matter what format I am using, there is no way to preserve the my original syntax which of course includes the time?

Wind is your friend
Matt
Reply With Quote
  #4 (permalink)  
Old August 26th, 2006, 02:50 AM
Imar's Avatar
Wrox Author
Points: 71,953, Level: 100
Points: 71,953, Level: 100 Points: 71,953, Level: 100 Points: 71,953, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,061
Thanks: 80
Thanked 1,585 Times in 1,561 Posts
Default

Hi Matt,

You can also use a messy combination of CONVERT and DatePart to build up a string of all the individual parts:

SELECT Convert(char(4), Year(GetDate())) + '/' + Convert(varchar(2),
Month(GetDate())) + '/' + Convert(varchar(2), Day(GetDate())) + ' '
+ Convert(varchar(2), DatePart(hh, GetDate())) + ':' + Convert
(varchar(2), DatePart(mi, GetDate()))

Run this in the query analyzer and you get something like this:

2006/8/26 9:48

Instead of Year, Month and Day you can also use DatePart if you like....

There might be cleaner solutions, but at least this one gives you full control over the output. If you put this in a user defined function, it's easier and cleaner to reuse.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #5 (permalink)  
Old August 26th, 2006, 10:39 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You can do this if you want the date and time:

SELECT
    convert (varchar (10), [datetimefield], 103) + ' ' + convert (varchar (10), [datetimefield], 108) as [field]
FROM Table

This will return dd/mm/yyyy hh:mm:ss

hth

"The one language all programmers understand is profanity."
Reply With Quote
  #6 (permalink)  
Old August 26th, 2006, 11:17 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Two very useful replies. Thank you very much, I learnt several things from both responses. I am now thinking I under utalize the capabilities of SQL.

This query :
update testDateConversion set newField=(SELECT convert (varchar (10), updated, 103) + ' ' + convert (varchar (10), updated, 108));

Takes the '20/08/2006 1:29:06 AM' value out of the dateTime field (updated) and inserts it into the new varchar (newField) field nicely however its missing the AM/PM. It inserts '20/08/2006 01:29:06' I am trying to alter it to capture the AM PM should this be possible?

TYIA

Wind is your friend
Matt
Reply With Quote
  #7 (permalink)  
Old August 27th, 2006, 04:45 AM
Imar's Avatar
Wrox Author
Points: 71,953, Level: 100
Points: 71,953, Level: 100 Points: 71,953, Level: 100 Points: 71,953, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,061
Thanks: 80
Thanked 1,585 Times in 1,561 Posts
Default

You can use CASE and then look at the hour:

SELECT CASE WHEN
DatePart(hh, GetDate()) < 12 THEN ' AM' ELSE ' PM' END

Just as with my previous messy query there may be more efficient ways to do this though...

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #8 (permalink)  
Old August 28th, 2006, 09:18 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

SQL maintains a 24 hour clock, but, I am pretty sure when you write the time out to the page the AM/PM will be appended to the time when its displayed. (In my experience, when binding to a datagrid, this was the behaviour I experienced) If you want the AM/PM appended in the table your inserting to, I think you are going to have to adapt Imar's statement because there is not a specific convert option that will strictly convert your time AND append AM/PM.

There are options that will do this but it applies a format to the entire field, e.g. mon dd yyyy hh:mm:ss:mmm am/pm (convert option 109).

hth.

"The one language all programmers understand is profanity."
Reply With Quote
  #9 (permalink)  
Old August 28th, 2006, 07:17 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Much thanks...

Wind is your friend
Matt
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Data type samnachilomo SQL Server 2000 1 November 25th, 2006 03:25 PM
Changing data type error kuku SQL Server 2000 1 August 18th, 2005 10:20 AM
behaviour of NULL value in date type columns kasanar SQL Server 2000 2 July 7th, 2005 04:40 PM
changing data type when importing Excel worksheet awwhite Access VBA 0 September 8th, 2004 09:37 AM
Changing data type JLN Pro VB 6 1 July 13th, 2004 12:49 PM



All times are GMT -4. The time now is 12:46 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.