Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

format sql date

Posted on 2013-01-14
5
Medium Priority
?
289 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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