Solved

ms sql server 2005 date time issues

Posted on 2011-02-16
8
771 Views
Last Modified: 2012-05-11
I need to pull a date that is this setup:  '2011-02-16 00:00:00'

the below is not working....


 DECLARE @APP_DATE smalldatetime
 SET  @APP_DATE = ISNULL(@APP_DATE, getDATE())

if @APP_DATE = '2011-02-16 00:00:00'
begin
DECLARE @CONFIRM NVARCHAR(10)
SET @CONFIRM = 'yes'
PRINT @CONFIRM
eND

print @APP_DATE
0
Comment
Question by:GlobaLevel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 34913140
Won't @app_date always be null in your code?

The date format for yyyy-mm-dd hh:mm:ss is ....

CONVERT(VARCHAR(19), GETDATE(), 120)
0
 
LVL 12

Expert Comment

by:jmcmunn
ID: 34913163

change your second line to this:
SET  @APP_DATE = ISNULL(@APP_DATE, convert(varchar,getdate(), 101));

and you're all set.
0
 
LVL 29

Expert Comment

by:QPR
ID: 34913170
101 is MM/DD/YYYY
120 is yyyy-mm-dd hh:mm:ss
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 12

Expert Comment

by:jmcmunn
ID: 34913181

He doesn't want the time, he wants all zeroes for the hh:mm:ss.  Thus, my method fills in the time as all zeroes by only selecting the MM/dd/yyyy.  The rest of the short date time is filled with 00:00:00.

Trust me, I ran it, debugged it and it worked.  ;-)
0
 
LVL 12

Expert Comment

by:jmcmunn
ID: 34913183
presumably there is more code somewhere that MIGHT set Appdate...in which case it would not have to do the select and get the 00:00:00...at least that's the assumption I made since the above code wouldn't be of much use on it's own.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 34913342
Convert GETDATE() to TIME that will take care of time part.

DECLARE @APP_DATE smalldatetime
 SET  @APP_DATE = ISNULL(CONVERT(TIME,@APP_DATE), CONVERT(TIME,GETDATE()))

if @APP_DATE = '2011-02-16 00:00:00'
begin
DECLARE @CONFIRM NVARCHAR(10)
SET @CONFIRM = 'yes'
PRINT @CONFIRM
eND

print @APP_DATE

Open in new window

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34913565
as getdate() has date and time, you would need to convert to DATE, not to TIME ... however, that would only be possible as from sql 2008.

before, you need:

if @APP_DATE >= '2011-02-16 00:00:00' AND @APP_DATE > '2011-02-17 00:00:00'

0
 
LVL 12

Expert Comment

by:jmcmunn
ID: 34916752
That's ok if this really does solve your issue, but the accepted solution is correct for Sql 2008 and the question is labeled as Sql 2005 for anyone visiting this thread in the future.  There are other valid solutions for 2005 listed here.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Database Restore 2008 R2 1 41
denied execute as 13 57
T-SQL: Please describe what a page split is 5 58
I am new to using JSON in SQL Server 2 57
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question