Solved

ms sql server 2005 date time issues

Posted on 2011-02-16
8
772 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

690 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