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
 SQL View

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2011-06-15 : 09:44:12

Can I have a view that has a field populate based on two other fields in the record?

In my record I have a Request Date and Ship Date.

In my view I want a field where if request date < Ship Date then populate with O if not L.


Request_date Ship_dt OnTime
06/01/2011 05/28/2011 O

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-06-15 : 09:46:27
select
... other columns ...
CASE WHEN request < ship THEN 'O' ELSE 'L' END as OnTime,
... other colums ...
from ...

edit: GUESS: dates are of type DATETIME OR DATE

No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-06-15 : 12:24:23
People cannot read your mind, so post your code and clear specs if you really want help. Please post real DDL and not narrative or your own personal programming language. Learn to use ISO-11179 rules for the data element names, avoid needless dialect and use ISO-8601 temporal formats, codes and so forth. Please tell us what SQL product and release you are using. Tell us if you can change the DDL or if you are stuck with it.


In my view I want a field [sic: columns are not fields!] where if request_date < shipment_date then populate with O if not L.

CASE WHEN request_date < shipment_date
THEN 'ontime' ELSE 'late' END AS ontime_flag

but we do not like to write with flags in SQL. What is this assembly language stile flag causing to happen? If this is for display to the user, do it in the front end, not here. ?


--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -