Solved

Need help with a query

Posted on 2016-10-27
6
75 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 28

Accepted Solution

by:
Pawan Kumar 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

809 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