Solved

ms sql server 2005 date time issues

Posted on 2011-02-16
8
766 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 40

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 142

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

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.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now