Solved

format sql date

Posted on 2013-01-14
5
281 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
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 65

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 26

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 65

Expert Comment

by:Jim Horn
ID: 38778816
Just curious ... how did my first comment not solve the problem?
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

23 Experts available now in Live!

Get 1:1 Help Now