Solved

Need help with a query

Posted on 2016-10-27
6
54 Views
Last Modified: 2016-10-28
Hi Experts,

In the query if the ExpPurchaseDt="" then Formatted_ExpPurchaseDt is coming as "01/01/1900". Is there anyway I can prevent it in the query. I want if  ExpPurchaseDt="" then Formatted_ExpPurchaseDt should be "", otherwise it should format the date.

This is my query.
SELECT ExpPurchaseDt, CONVERT(VARCHAR, CONVERT(DATE, ExpPurchaseDt), 101) as Formatted_ExpPurchaseDt
FROM JOBDETAILS where JobOrOpportunity='O'

Thanks in advance.
0
Comment
Question by:RadhaKrishnaKiJaya
6 Comments
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41862870
You can use a case statement.  See this example:

DECLARE @date varchar(50)
SET @date = '2/5/2016'
SELECT CASE WHEN @date = '' THEN '' ELSE CONVERT(VARCHAR, CONVERT(DATE, @date), 101) END
SET @date = ''
SELECT CASE WHEN @date = '' THEN '' ELSE CONVERT(VARCHAR, CONVERT(DATE, @date), 101) END

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41862874
>In the query if the ExpPurchaseDt=""
fyi this is one of the reasons a varchar column is a poor choice of data type to store date values, as it will allow values that cannot be converted to a date such as empty string '', 42, or 'banana', which will require queries to evaluate, convert it to a date format, and handle non-date values gracefully every time it needs to behave as a date.

Aside from that Dustin's comment above is the correct answer, just use a CASE block to pick it off.   For some extra reading on CASE check out the article SQL Server CASE
1
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41862898
Thanks Mr. Jim for your suggestion.  When we do the next db design I will keep this thing in mind.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 48

Expert Comment

by:PortletPaul
ID: 41862926
And in the previous question I  provided an article reference you should read regarding using text as dates.  It's not a good thing to do.
0
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
ID: 41863244
Informational , WITH 2012+ IIF is also there which handles these kind of scenarios.

Also note that always provide proper LENGTH while conversions and declaring variables like below
CONVERT(VARCHAR(10), CONVERT(DATE, @date), 101))

DECLARE @date VARCHAR(10)

SET @date = '2/5/2016'
SELECT IIF ( @date = '' , '' ,CONVERT(VARCHAR(10), CONVERT(DATE, @date), 101)) Formatted_ExpPurchaseDt

SET @date = ''
SELECT IIF ( @date = '' , '' ,CONVERT(VARCHAR(10), CONVERT(DATE, @date), 101)) Formatted_ExpPurchaseDt


--Code for your query

SELECT ExpPurchaseDt, IIF ( ExpPurchaseDt = '' , '' ,CONVERT(VARCHAR(10), CONVERT(DATE, ExpPurchaseDt), 101)) Formatted_ExpPurchaseDt
FROM JOBDETAILS where JobOrOpportunity='O'

Open in new window


Also if you can change the data type of the date column to DateTime/Date that would be good!!

Thnx!
0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 41864038
Thank you very much. This is what exactly I wanted. In next project I will definitely make any date's datatype datetime.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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