Concatenating a string in a like statement

时间:2015-06-15 14:38:36

标签: sql sql-server-2008

I have a field that I'd like to get at, but when I try to concatenate a '%' to the field I sill get no matches.

Here's the full query:

SELECT
    dbo.SALESQUOTATIONTABLE.CUSTACCOUNT AS 'Account_Number__c',
    dbo.SALESQUOTATIONTABLE.BUSRELACCOUNT AS 'Business_Relation_Number__c',
    SALESQUOTATIONTABLE.DIMENSION2_ AS 'Division__c',
    QUOTATIONEXPIRYDATE AS 'Expiration__c',
    PMF_NOOFITEMS AS 'Number_of_Items__c',
    CREATEDDATE AS 'Quote_Date__c',
    SALESQUOTATIONTABLE.QUOTATIONID AS 'Quote_Number_Ext_ID__c',
    SALESQUOTATIONTABLE.QUOTATIONID AS 'Name',
    SALESQUOTATIONTABLE.PAYMENT AS 'Terms__c',
    SALESGROUP AS 'Territory__c',
    dbo.SALESQUOTATIONTABLE.BUSRELACCOUNT AS 'Account_CBusiness_Relation_Number__c',
    CASE dbo.SALESQUOTATIONTABLE.QUOTATIONSTATUS
        WHEN 0 THEN 'Created'
        WHEN 1 THEN 'Sent'
        WHEN 2 THEN 'Confirmed'
        WHEN 3 THEN 'Lost'
        WHEN 4 THEN 'Cancelled'
        WHEN 5 THEN 'Won'
        ELSE 'Unknown' END AS 'Quote_Status__c',
    '"' + REPLACE(NAME, '"', '') + '"' AS 'Contact_Name__c',
    PHONE AS 'Contact_Phone__c',
    Replace(dbo.CONTACTPERSON.EMAIL,',',' ') AS 'Contact_Email__c',
    PMF_RFQRECEIVEDDATE AS 'Quote_Recieved_Date__c',
    PMF_QUOTESENTDATE AS 'Quote_Sent_Date__c',
    Replace(PGMNAME,',',' ') AS 'Program_Name__c',
    Replace(OEM,',',' ') AS 'OEM_c',
    (SELECT TOP 1(NAME) from USERINFO where SALESQUOTATIONTABLE.SalesTaker = USERINFO.NETWORKALIAS) as 'Pricing_Specialist_c',
    SALESQUOTATIONTABLE.CustomerRef as 'Customer_Reference__c',
SalesQuotationTable.pmf_SalesQuoteComment as 'Sales_Quote_Comment__c',
SalesQuotationTable.QuotationCategory as 'Quotation_Type__c',
(select Max(CUSTQUOTATIONJOUR.QuotationDate) from CUSTQUOTATIONJOUR where SALESQUOTATIONTABLE.QUOTATIONID like CUSTQUOTATIONJOUR.QUOTATIONDOCNUM +'%')  as 'Quotation_Date__c'       
FROM
    dbo.SALESQUOTATIONTABLE 
LEFT OUTER JOIN 
    dbo.CONTACTPERSON ON dbo.SALESQUOTATIONTABLE.CONTACTPERSONID = dbo.CONTACTPERSON.CONTACTPERSONID
--LEFT OUTER JOIN CUSTQUOTATIONJOUR on SALESQUOTATIONTABLE.QUOTATIONID like  (CUSTQUOTATIONJOUR.QUOTATIONDOCNUM +'%')
WHERE
    CREATEDDATE >= CONVERT(VARCHAR(10), GETDATE() - 30, 101)
    OR
    --Only update quotes that were created in 2013 or after
    (MODIFIEDDATE >= CONVERT(VARCHAR(10), GETDATE() - 30, 101)
    AND
    YEAR(CREATEDDATE) > 2012)

The part that I'm trying to work with is the:

(select Max(CUSTQUOTATIONJOUR.QuotationDate) from CUSTQUOTATIONJOUR where SALESQUOTATIONTABLE.QUOTATIONID like CUSTQUOTATIONJOUR.QUOTATIONDOCNUM +'%')  as 'Quotation_Date__c'

The data is like this in the table (as an example)

SALESQUOTATIONTABLE.QUOTATIONID = QT-00103078
CUSTQUOTATIONJOUR.QUOTATIONDOCNUM = QT-00103078-1

I am getting the result from date (i just need the latest, thus the Max().

however, the date filed in that subquery is only returning nulls, even though I verified that there is indeed a match. The question is, is the

CUSTQUOTATIONJOUR.QUOTATIONDOCNUM +'%'

Correct?

1 个答案:

答案 0 :(得分:1)

I had the like backwards:

(select Max(CUSTQUOTATIONJOUR.QuotationDate) from CUSTQUOTATIONJOUR where CUSTQUOTATIONJOUR.QUOTATIONDOCNUM like SALESQUOTATIONTABLE.QUOTATIONID +'%'  )  as 'Quotation_Date__c'