Solved

format sql date

Posted on 2013-01-14
5
280 Views
Last Modified: 2013-01-15
declare @StartDate as date
declare @EndDate as date
set @StartDate = '1/14/2013'
set @EndDate = '1/14/2013'
declare @today as date
set @today = FORMAT( @today, 'dd/MM/yyyy') --(CONVERT(VARCHAR(10), GETDATE(), 101))
print @today --2013-01-14
if(@today != @StartDate)
print 'yes'
else
print 'no'


why isn't the above valid? how do i do this? i just need to today and startdate to match up
0
Comment
Question by:fwstealer
  • 3
5 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
You can't format a date and set it to a date variable.

Perhaps you want to do..
IF CONVERT(VARCHAR(10), GETDATE(), 101) = CONVERT(VARCHAR(10),@StartDate, 101)  THEN
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
FORMAT returns a nvarchar value, not a date value.
So you won't be able to compare a nvarchar to a date without risking an implicit conversion error.
http://msdn.microsoft.com/en-us/library/hh213505.aspx
0
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
Comment Utility
1st, as jimhorn is pointing out, you do not store anything in a DATE type variable in any format, it is always stored in the internal numberic date datatype, you can just display it in different formats using the CONVERT or FORMAT functions when retrieving it.

What are you wanting to do, just get the current date into the variable @today?
If so then use either GETDATE() or the ANSI standard CURRENT_TIMESTAMP to assign todays date to a variable like this:
set @today = CURRENT_TIMESTAMP
 modifing your code to this:
declare @StartDate as date
declare @EndDate as date
set @StartDate = '1/14/2013'
set @EndDate = '1/14/2013'
declare @today as date
set @today = CURRENT_TIMESTAMP--FORMAT( @today, 'dd/MM/yyyy') --(CONVERT(VARCHAR(10), GETDATE(), 101))
print @today --2013-01-14
if(@today != @StartDate)
print 'yes'
else
print 'no'

Open in new window

results in this output:
2013-01-14
no

Open in new window

which by your logic means that @today does equal @StartDate
0
 

Author Comment

by:fwstealer
Comment Utility
declare @StartDate as date
declare @EndDate as date
set @StartDate = '1/15/2013'
set @EndDate = '1/15/2013'
declare @today as date
set @today = CURRENT_TIMESTAMP--FORMAT( @today, 'dd/MM/yyyy') --(CONVERT(VARCHAR(10), GETDATE(), 101))
print @today --2013-01-14
if(@today = @StartDate)
print 'yes'
else
print 'no'

so I change it to do the above
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Just curious ... how did my first comment not solve the problem?
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 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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

11 Experts available now in Live!

Get 1:1 Help Now