Volleynerd Knowledge Base

Tuesday, September 28, 2004

SQL convert from date to string


I always forget this SQL syntax, to convert from date to common string formats, so blogging it here once and for all.

-- convert from date to string, with leading zeros, etc.
-- see books online help for "CAST and CONVERT"


-- get leading 0 on a date part
select Right( '00' + CONVERT(VarChar(2),DatePart(mm,getdate())), 2)

-- convert date to a common string format
-- (100 series numbers are with century, 0-99 are with 2 digit year)
SELECT CONVERT(VARCHAR(10), getDate(), 101) -- mm/dd/yyyy (USA)
SELECT CONVERT(VARCHAR(10), getDate(), 112) -- yyyymmdd (ISO)



Comments: Post a Comment

Home