简体   繁体   中英

SQL Server aliasing with column calculation

I'm making this calculation in my SQL statement:

CD_WELLBORE_FORMATION.prognosed_md + {fn IFNULL(CD_DATUM.datum_elevation, 0)}

How do I alias this calculation to say, "MD"? I've tried different placements of the AS keyword, but I keep getting syntax errors.

You should be able to do this:

(
 CD_WELLBORE_FORMATION.prognosed_md 
 + {fn IFNULL(CD_DATUM.datum_elevation, 0)}
) as MD

I assume that { fn IFNULL ...} is just something you put here as an example. That's obviously invalid SQL

SELECT 
        prognosed_md + {fn IFNULL(datum_elevation, 0)} AS MD
FROM (SELECT 1 AS prognosed_md, 2 AS datum_elevation) T

works fine for me. Why not just use COALESCE though instead of the ODBC sequence? COALESCE is standard SQL...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM