April 17, 2008 at 1:53 am
Hi Guys. I have a select query as follows (sample code):
SELECT SalesOrderId, convert(varchar, Date, 103)AS ExpiryDate, Type
FROM dbo.Values INNER JOIN
SalesOrders ON SalesOrders.SalesOrder = dbo.Values.RecordNumber INNER JOIN
dbo.DataNameFields ON dbo.DataNameFields.DataField = dbo.Values.DataField
WHERE dbo.Values.DataField = '43' AND dbo.SalesOrders.Type = 'Q'
The field that contains the 'Date' has a number of Null Values. Rather than the query returning 'Null' I thought it might be an idea to return 'No Date Specified' as I will be using the query in a report (or even exclude NULL from query results?).
Is that possible or even viable?
Thanks,
Phil.
-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."
Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '
Tommy Cooper
April 17, 2008 at 1:55 am
Lookup the ISNULL function. Should do what you want.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 17, 2008 at 2:17 am
OR you can use:
----------------
SELECT
CASE
WHEN [DateField] IS NULL then 'No Date Specified'
ELSE convert(varchar, Date, 103)AS ExpiryDate
END AS DD
FROM
[AppsGo].[dbo].[Sales]
April 17, 2008 at 2:44 am
Thanks for the quick response. I looked up the ISNULL prior to posting but could not get it to work!
Tried:
SELECT SalesOrderId, convert (varchar, (ISNULL(CustomFieldValueDate,'No Date',103))AS ExpiryDate, Type
Have tried code below, returns error 'Incorrect syntax near the keyword 'CASE'
SELECT SalesOrderId, Type
CASE
WHEN [Date] IS NULL then 'No Date Specified'
ELSE convert(varchar, Date, 103)AS ExpiryDate
END AS DD
FROM dbo.Values INNER JOIN
SalesOrders ON SalesOrders.SalesOrder = dbo.Values.RecordNumber INNER JOIN
dbo.DataNameFields ON dbo.DataNameFields.DataField = dbo.Values.DataField
WHERE dbo.Values.DataField = '43' AND dbo.SalesOrders.Type = 'Q'
Thanks,
Phil.
-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."
Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '
Tommy Cooper
April 17, 2008 at 2:52 am
Ad a comma after Type and remove the AS clause in the CASE part.
SELECTSalesOrderId,
Type,
CASE
WHEN [Date] IS NULL then 'No Date Specified'
ELSE CONVERT(VARCHAR, Date, 103)
END AS ExpiryDate
FROMdbo.Values
INNER JOINSalesOrders ON SalesOrders.SalesOrder = dbo.Values.RecordNumber
INNER JOINdbo.DataNameFields ON dbo.DataNameFields.DataField = dbo.Values.DataField
WHEREdbo.Values.DataField = '43'
AND dbo.SalesOrders.Type = 'Q'
N 56°04'39.16"
E 12°55'05.25"
April 17, 2008 at 2:53 am
SELECTSalesOrderId,
Type,
ISNULL(CONVERT(VARCHAR, Date, 103), 'No Date Specified') AS ExpiryDate
FROMdbo.Values
INNER JOINSalesOrders ON SalesOrders.SalesOrder = dbo.Values.RecordNumber
INNER JOINdbo.DataNameFields ON dbo.DataNameFields.DataField = dbo.Values.DataField
WHEREdbo.Values.DataField = '43'
AND dbo.SalesOrders.Type = 'Q'
N 56°04'39.16"
E 12°55'05.25"
April 17, 2008 at 3:48 am
Peso (4/17/2008)
SELECTSalesOrderId,
Type,
ISNULL(CONVERT(VARCHAR, Date, 103), 'No Date Specified') AS ExpiryDate
FROMdbo.Values
INNER JOINSalesOrders ON SalesOrders.SalesOrder = dbo.Values.RecordNumber
INNER JOINdbo.DataNameFields ON dbo.DataNameFields.DataField = dbo.Values.DataField
WHEREdbo.Values.DataField = '43'
AND dbo.SalesOrders.Type = 'Q'
Thks peso for the idea peso, faster to write and maybe better for performance ! ! !
April 17, 2008 at 3:51 am
Peso awesome, many thanks to you and to all those that gave their time in assisting me with this problem.
Many Thanks,
Phil.
-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."
Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '
Tommy Cooper
April 18, 2008 at 11:17 am
It's been suggested that "COALESCE" is preferable to ISNULL.
It allows for a list of arguments, the first of which evaluates to not null being returned.
Coalesce is ANSI standard; ISNULL is Microsoft only, so for portability of code (and sanity of a developer working multiple platforms), COALESCE would be the choice.
see artilcle on nulls:
http://www.sqlservercentral.com/articles/Advanced+Querying/fourrulesfornulls/1915/
April 20, 2008 at 6:13 am
Thanks for your input John.
Phil.
-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."
Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '
Tommy Cooper
April 20, 2008 at 4:45 pm
john.arnott (4/18/2008)
It's been suggested that "COALESCE" is preferable to ISNULL.It allows for a list of arguments, the first of which evaluates to not null being returned.
Coalesce is ANSI standard; ISNULL is Microsoft only, so for portability of code (and sanity of a developer working multiple platforms), COALESCE would be the choice.
see artilcle on nulls:
http://www.sqlservercentral.com/articles/Advanced+Querying/fourrulesfornulls/1915/
Please tell me that you don't believe in the myth of code portability...
--Jeff Moden
Change is inevitable... Change for the better is not.
April 21, 2008 at 4:06 am
ISNULL(CONVERT(VARCHAR, Date, 103), 'No Date Specified') AS ExpiryDate
Just beware of a little potential gotcha here - using convert with varchar and no specified length default to a varchar(20) return - so above converts the date to varchar(20) with alternative text which luckily is <20 chars. If you used a longer string than 'No Date Specified' it would be truncated to 20 chars long in above code - in general I like to specify length on converts (even if the length I want is 20) as this protects against unexpected defaults ...
James Horsley
Workflow Consulting Limited
April 21, 2008 at 9:53 am
Please tell me that you don't believe in the myth of code portability...
--Jeff Moden
It's not so much code portability as developer portability. I prefer being able to use as similar approach as possible in our T-SQL and DB2 code and remember as few differences as necessiary. Since "COALESCE" does the same job as "ISNULL" as well as add flexibility, I'd just as soon learn to use this weirdly named function on both platforms, even if it sounds like gelatin setting.
April 21, 2008 at 10:29 am
john.arnott (4/21/2008)
Please tell me that you don't believe in the myth of code portability...
--Jeff Moden
It's not so much code portability as developer portability. I prefer being able to use as similar approach as possible in our T-SQL and DB2 code and remember as few differences as necessiary. Since "COALESCE" does the same job as "ISNULL" as well as add flexibility, I'd just as soon learn to use this weirdly named function on both platforms, even if it sounds like gelatin setting.
I remember developer portability. About twelve years ago, someone walked into my cube at the time, handed me a SQL Server book and told me to stop building Access databases...Poof - I'd been "ported"....:D
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
April 21, 2008 at 12:01 pm
Matt,
At least they handed you a SQL Server book to start with. All they gave me was the instructions: 'Here is a SQL Server database. Make it work.' Before that, All I was doing was MS Access 2.0 and 97. Those were such fun days.
Dave Novak
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply