user1837575
user1837575

Reputation: 337

Convert date and find date

I'm having some trouble with syntax.

First, I'd like to convert a date from this format:

2014-12-18 21:49:54.047

To this format:

20141218

I can do this just fine using this select statement:

SELECT F253 = CONVERT (VARCHAR (20), F253, 112) 
FROM SAL_HDR

My problem is with the syntax. How do I put that select statement inside of a select statement with a bunch of other lines? I can't seem to make it work right. I have commented out the line in question. How do I write that line into the larger select statement?

    SELECT 
    SAL_HDR.F253 AS [Transaction Date], 
    /*F253 = CONVERT (VARCHAR (20), F253, 112)*/
    SAL_HDR.F1036 AS [Transaction Time], 
    SAL_HDR.F1032 AS [Transaction #],
    FROM SAL_HDR

Upvotes: 0

Views: 38

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270421

Just add it in as an expression:

SELECT CONVERT(VARCHAR(20), SAL_HDR.F253, 112)  AS [Transaction Date], 
       SAL_HDR.F1036 AS [Transaction Time], 
       SAL_HDR.F1032 AS [Transaction #]
FROM SAL_HDR;

Alternatively, you can use the syntax:

SELECT [Transaction Date] = CONVERT(VARCHAR(20), SAL_HDR.F253, 112), 
       [Transaction Time] = SAL_HDR.F1036, 
       [Transaction #] = SAL_HDR.F1032
FROM SAL_HDR;

I personally prefer the first version, because I find that the second is too close to variable assignment.

Upvotes: 1

Related Questions