Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 If Value = 1, Display as '3 Day Lead time'

Author  Topic 

SergioM
Posting Yak Master

170 Posts

Posted - 2011-09-07 : 17:04:05
I would like to create a SQL query that shows a value different from the one that it reads in the database. Normally, I would just SELECT & UPDATE the table. But the DBMS inputs a numeric field and it expects to find it. I would like to create a query that says something like

quote:
If Inventory.Location is equal to '1'
- Display as '1 Day Lead time'
If Inventory.Location is equal to '2'
- Display as '3 Day Lead time'
If Inventory.Location is equal to '3'
- Display as '5 Day Lead time'


The following SQL Query works very well for the part where I select what I need to find. I just don't know how to make it show something based on the data it finds. How can I do this in SQL?

SELECT  Items.Manufacturer, Inventory.PartNum, Inventory.QtyOnHand, Items.Title
FROM Inventory
INNER JOIN Items
ON Inventory.InventoryID = Items.InventoryID
WHERE Inventory.Location = '1'

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-07 : 17:10:59
How is the lead time calculated?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SergioM
Posting Yak Master

170 Posts

Posted - 2011-09-07 : 17:22:06
It's a fixed value. There isn't any other data in the DB that would tell you. I just know it. There are also too few warehouses (5) to want to develop anything slick. I'm ok with retyping the code 5 times.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-07 : 17:24:51
SELECT CASE WHEN Inventory.Location = 1 THEN '1 Day Lead Time' WHEN Inventory.Location = 2 THEN '3 Day Lead Time' ... END AS LeadTime, Items.Manufacturer, Inventory.PartNum, Inventory.QtyOnHand, Items.Title
FROM Inventory
INNER JOIN Items
ON Inventory.InventoryID = Items.InventoryID
WHERE Inventory.Location = 1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SergioM
Posting Yak Master

170 Posts

Posted - 2011-09-07 : 19:06:04
Thanks! I just tried it out & it does exactly what I need. I don't want to take advantage, but I have a very similar question and it doesn't justify a second post. What if I want to keep it within the same column as the other data?

IE If I have a price list and some values are missing. So rather than having them appear as 'NULL', I can say, if it is NULL, display '0'. Except, I wouldn't want a second column. I would want to display it all in the same one, just altered.

Again, thanks for the help. I'm halfway through an SQL, book but (so far) it encompasses basic understanding without delving into these subtleties.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-07 : 19:12:06
Show us a data example to make it more clear. It's hard to read people's descriptions and understand what is wanted. Before and after data example makes it very clear.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SergioM
Posting Yak Master

170 Posts

Posted - 2011-09-07 : 22:24:06
I plan to use this SQL technique on a couple of things. The following table is easier to express. These three vendors sell the similar items. When they all carry the items, the comparison is easy. Sometimes there is little or no overlap. So that vendor does not carry the item. Since the field exists, it is just stated as 'NULL'. However, that clutters any exports I have and takes up a lot of space with needless 'NULL' strings. I'd prefer for it to be blank or say '0'.

	Vendor_A	Vendor_B	Vendor_C
Item_1 $12.00 $11.00 $10.00
Item_2 $15.00 NULL $5.00
Item_3 $11.00 $11.00 NULL
Item_4 $10.00 $15.00 $15.00
Item_5 NULL NULL $5.00


In your previous example, all of the output END AS 'LeadTime', a new column created in real time. In this example, I want it all to appear in the column it came from. So using the example above, I would want to alter Row 3, Column 3 (Vendor_B:Item_2). But I would want the output to be right there where it came from. Not in a new column.

I'm sure that the SQL command is a variation of what you showed earlier. But I'm getting error after error as I tinker with it on my own.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-07 : 23:35:39
You would just use the ISNULL function for this: http://msdn.microsoft.com/en-us/library/ms184325.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SergioM
Posting Yak Master

170 Posts

Posted - 2011-09-08 : 00:08:08
Again, it's exactly what I needed & works like a charm. Thank you!!! :)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-08 : 00:15:06
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -