我认为在SQL中结合文本?

时间:2011-09-11 00:17:53

标签: sql sql-server tsql

给出以下SQL语句如下所示。我没有在这行代码中为“image_link”返回任何内容

,  'http://www.thesite.com/v/vspfiles/photos/' + IsNull(p.Vendor_PartNo,p.ProductCode) + '-2.jpg' AS image_link

如果我将此行更改为以下内容(删除-2.jpg中的“。”),则返回完整的链接(前缀和后缀),但显然没有“。”

,  'http://www.thesite.com/v/vspfiles/photos/' + IsNull(p.Vendor_PartNo,p.ProductCode) + '-2jpg' AS image_link

有什么问题,如何加入“。”在后缀?

SELECT p.ProductCode AS id
    ,  p.ProductName AS title
    ,  'Home & Garden > Household Appliance Accessories > Laundry Appliance Accessories' AS product_type
    ,  IsNull(pe.SalePrice,pe.ProductPrice) AS price
    ,  IsNull(pe.ProductManufacturer,'n/a') AS brand
    ,  IsNull(pe.ProductCondition,'new') AS condition
    ,  CONVERT(VARCHAR(10), (GETDATE() + 30),120) AS expiration_date
    ,  pd.ProductDescriptionShort AS [stripHTML-description]
    ,  'http://www.thesite.com/v/vspfiles/photos/' + IsNull(p.Vendor_PartNo,p.ProductCode) + '-2.jpg' AS image_link
    ,  'http://www.thesite.asp?ProductCode=' + p.ProductCode + '&Click=1327'  AS link
    , CAST(pe.ProductWeight AS VARCHAR(20)) + ' lb' AS weight
 FROM Products p
 JOIN Products_Descriptions pd ON p.ProductID = pd.ProductID
 JOIN Products_Extended pe ON pd.ProductID = pe.ProductID
WHERE (p.IsChildOfProductCode is NULL OR p.IsChildOfProductCode = ' 
       AND (p.HideProduct is NULL OR p.HideProduct <> 'Y')
  AND pe.ProductPrice > 0
ORDER BY p.ProductCode

3 个答案:

答案 0 :(得分:1)

在SQL-Server(Microsoft)中,只需使用+号和char(46)来获取句点

'http://www.thesiste/v/vspfiles/photos/'+
    isNull(p.vendor_partno,p.productCode)+'-2'+char(46)+'jpg'

查看http:是否导致问题,也许服务器正在以不同的方式处理链接

'ht'+'tp://www.thesiste/v/vspfiles/photos/'+
    isNull(p.vendor_partno,p.productCode)+'-2'+char(46)+'jpg'

还可以尝试

replace('http:||www.thesiste|v|vspfiles|photos|','|','/')+
    isNull(p.vendor_partNo,p.productCode)+'-2'+char(46)+'jpg'

如果斜杠字符导致问题

看起来斜线字符被特殊处理。尝试使用起始值创建变量并使用...

DECLARE @URL varchar(200)
SET @Url = 'http://www.thesiste/v/vspfiles/photos/'

    @url + isNull(p.vendor_partno,p.productCode)+'-2'+char(46)+'jpg'

甚至,更糟糕的尝试,

    'http:'+char(47)+char(47)+'www.thesiste'+char(47)+'v'+char(47)+'vspfiles'+
char(47)+'photos'+char(47)'+
        isNull(p.vendor_partno,p.productCode)+'-2'+char(46)+'jpg'

答案 1 :(得分:0)

你试过这个吗?

CONCAT('http://www.thesite.com/v/vspfiles/photos/', IsNull(p.Vendor_PartNo,p.ProductCode),'-2.jpg') AS image_link

答案 2 :(得分:0)

我没有对此进行过测试,但是我认为您遇到了数据类型问题吗?

'http://www.thesite.com/v/vspfiles/photos/' + Cast(IsNull(p.Vendor_PartNo,p.ProductCode) AS varchar) + '-2.jpg' AS image_link