Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3601548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:38:07+00:00 2026-05-18T20:38:07+00:00

is there any way to use defined column name in SELECT statement with WHERE

  • 0

is there any way to use defined column name in SELECT statement with WHERE clause?
Here is my t-sql codes,

SELECT CONVERT(DATETIME,(
    CASE WHEN Operator = 'T' THEN 
        (SUBSTRING(SendDate,7,4)+'-'+SUBSTRING(SendDate,4,2)+'-'+ 
        SUBSTRING(SendDate,1,2) + SUBSTRING(SendDate,11,9)) 
    ELSE 
        RecivedSMS.SendDate 
    END)) AS DefinedDate 
WHERE DATEPART(wk, DefinedDate) =  52

Yours affectionately and thank you very much for your helps, already now.

Edited part of posted

CREATE TABLE #TEMP
(
DateField DATETIME,
NumberField VARCHAR(10),
Sender VARCHAR(255)
)

INSERT #TEMP
SELECT DISTINCT(
CONVERT(DATETIME, ( 
                    CASE WHEN Operator = 'T' 
                    THEN (  SUBSTRING( SendDate, 7, 4 ) + '-'
                            + SUBSTRING( SendDate, 4, 2 ) + '-'
                            + SUBSTRING( SendDate, 1, 2 )
                            + SUBSTRING( SendDate, 11, 9 )
                         ) 
                    ELSE SendDate 
                    END))) AS Table1.DateField,

SUBSTRING(Table1.Message,7,7) AS NumberField

FROM Table1
INNER JOIN 
Table2 ON Table1.Sender = Table2.PhoneNumber,
(
          SELECT  CONVERT(DATETIME, ( 
                    CASE WHEN Operator = 'T' 
                    THEN (  SUBSTRING( SendDate, 7, 4 ) + '-'
                            + SUBSTRING( SendDate, 4, 2 ) + '-'
                            + SUBSTRING( SendDate, 1, 2 )
                            + SUBSTRING( SendDate, 11, 9 )
                         ) 
                    ELSE SendDate 
                    END)) AS DefinedDate 
          FROM    Table1 WHERE Table1.Operator IS NOT NULL
        ) q  
WHERE Operator IS NOT NULL AND SUBSTRING(Table1.Message,1,6) = 'TREE ST'  AND DATEPART( wk, q.DefinedDate ) =  52 AND DATEPART(year, q.DefinedDate ) = 2010


SELECT *  FROM #TEMP ORDER BY NumberField

DROP TABLE #TEMP
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:38:08+00:00Added an answer on May 18, 2026 at 8:38 pm

    Not directly but you could wrap it in a subselect.

    SQL Statement

        SELECT  *
        FROM    (
                  SELECT  CONVERT(DATETIME, ( 
                            CASE WHEN Operator = 'T' 
                            THEN (  SUBSTRING( SendDate, 7, 4 ) + '-'
                                    + SUBSTRING( SendDate, 4, 2 ) + '-'
                                    + SUBSTRING( SendDate, 1, 2 )
                                    + SUBSTRING( SendDate, 11, 9 )
                                 ) 
                            ELSE RecivedSMS.SendDate 
                            END)) AS DefinedDate 
                  FROM    YourTable
                ) q          
        WHERE   DATEPART( wk, DefinedDate ) =  52
    

    update

    I believe following would be equivalent to what you wrote:

    SELECT  q.DefinedDate
            , q.NumberField
    FROM    (
              SELECT  Sender
                      , NumberField = SUBSTRING(Table1.Message, 7, 7) 
                      , Operator
                      , CONVERT(
                          DATETIME
                          , ( 
                              CASE WHEN Operator = 'T' 
                              THEN SUBSTRING( SendDate, 7, 4 ) + '-'
                                   + SUBSTRING( SendDate, 4, 2 ) + '-'
                                   + SUBSTRING( SendDate, 1, 2 )
                                   + SUBSTRING( SendDate, 11, 9 )
                              ELSE SendDate 
                              END )) AS DefinedDate 
              FROM    Table1 WHERE Table1.Operator IS NOT NULL
            ) q  
            INNER JOIN Table2 ON Table2.PhoneNumber = Tabl1.Sender
    WHERE   q.NumberField LIKE 'TREE ST%'  
            AND DATEPART( wk, q.DefinedDate ) =  52 
            AND DATEPART( year, q.DefinedDate ) = 2010
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to use inheritance in database (Specifically in SQL Server 2005)?
Is there any way to make all the applications on a server use SQL
Is there any way to use a constant as a hash key? For example:
Is there any way to use C# to build a container application where each
Is there any way to use AppleScript (or something else) to query currently running
Is there any way to use Relative path when configuring subversion externals. For example
Is there any tricky way to use Java reserved words as variable, method, class,
Is there any way to get Python to use my ActiveTcl installation instead of
Is there any way to force Text-Mate to use a two-space tab instead of
Is there any way to access a dynamically accessing member of a User- defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.