Solved

format sql date

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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