Solved

Need help with a query

Posted on 2016-10-27
6
80 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
[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
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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