Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 14th, 2007, 03:41 AM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to cmualo
Default DateTime Problem!!! Pls help

To all,

1. I have a table with 2 fields
      prdYear (Integer) - eg: value store (2007)
      prdMonth (Integer) - eg: value store (2)

2. The below statement working correctly

select Top 1 convert(datetime, cast(prdYear as varchar) + cast(prdMonth as varchar) +'01') from AgentProductionReport

3. But When i remove the 'Top 1', I getting this Error : Syntax error converting datetime from character string.

select convert(datetime, cast(prdYear as varchar) + cast(prdMonth as varchar) +'01') from AgentProductionReport


Can anyone tell me, why the above statement having a error!

Pls help, thanks in advanced!!!
 
Old August 14th, 2007, 05:11 AM
Friend of Wrox
 
Join Date: Feb 2006
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

Yes, I have checked your query, it's not working for some data. Change your query to :

select convert(datetime, cast(prdYear as varchar) + '-' + cast(prdMonth as varchar) + '-' + '01' )
from AgentProductionReport

Gaurav
 
Old August 16th, 2007, 03:50 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

1) CAST(... AS VARCHAR) returns only first character. CONVERT(VARCHAR, ...) returns all characters.
2) There is no ORDER BY, so the TOP 1 cannot be guaranteed.
3) This is more efficient

select Top 1 dateadd(month, 12 * prdYear - 22801 + prdMonth, 0) from AgentProductionReport
order by prdYear DESC, prdMonth DESC

select dateadd(month, 12 * prdYear - 22801 + prdMonth, 0) from AgentProductionReport
order by prdYear DESC, prdMonth DESC
 
Old August 16th, 2007, 06:50 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi Peso,

I always see you posting for the datetime questions. You are good at it. If you wouldn't mind explaining your response, what exactly does the 22801 do? I'm just having the hardest time wrapping my head around the DATEADD function.

Thanks,
Richard

 
Old August 17th, 2007, 03:08 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As the default year is 0 (1900-01-01), 22800 is the number of months (1900 * 12).
This way I still have the date 1900-01-01 as base date for further calculations.

See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=22339 for further explanation.







Similar Threads
Thread Thread Starter Forum Replies Last Post
DateTime Problem kwilliams ASP.NET 2.0 Professional 1 March 12th, 2008 01:36 PM
problem with dateTime picker roy_mm Reporting Services 2 March 14th, 2007 11:34 AM
Datetime problem hastikeyvan Dreamweaver (all versions) 16 January 25th, 2006 11:58 AM
DateTime problem acko Pro VB Databases 7 October 31st, 2004 05:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.