Solved

ms sql server 2005 date time issues

Posted on 2011-02-16
8
764 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this article I will describe the Detach & Attach 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

14 Experts available now in Live!

Get 1:1 Help Now