Solved

format sql date

Posted on 2013-01-14
5
288 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
[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
  • 3
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38776514
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 66

Expert Comment

by:Jim Horn
ID: 38776518
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 27

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 38776595
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
ID: 38778170
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 66

Expert Comment

by:Jim Horn
ID: 38778816
Just curious ... how did my first comment not solve the problem?
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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

628 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