简体   繁体   中英

Convert conditional variable to date in SQL Server 2008

I have a CTE that splits rows into new ones based on the lengh of the period (start to end dates), this code is currently printing the splitted new dates into the full datetime in US format.

This:

SELECT --VARIABLES
newBEG = CASE n WHEN 0  THEN f ELSE bp END
FROM --TABLE

Prints this:

2017-12-26 00:00:00.000

The original dates I can easily convert them in the format I want ( DD/MM/YYYY ) using the CONVERT() function because it's just the varible without changes:

This:

CONVERT(VARCHAR(10),f, 103)

Prints this:

26/12/2017

But considering newBEG it's a calculated variable I'm unable to put the entire code inside the CONVERT() function, is any way -inside the same select i'm using- convert the variable to the 103 format?

You can try below -

SELECT CONVERT(VARCHAR(10),CASE n WHEN 0 THEN f ELSE bp END, 103) as newBEG 
FROM tablename

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