简体   繁体   中英

SQL Insert into query where part of the Select is a substring

Morning All,

I'm trying to create an Insert Into Select query where part of the criteria is obtained from truncating the values in a column eg

INSERT INTO tbRoom ([Ref],[Type])

SELECT ([Ref],substring([Description], 1, charIndex(':',[Description] )-1)) FROM tbRoomUsage

WHERE tbRoomUsage.Ref = 1

How do I structure the query to do the substring query as part of the overall Insert Into query?

The

SELECT substring([Description], 1, charIndex(':',[Description] )-1) FROM tbRoomUsage 

works fine on its own in terms of selecting the truncated information I need.

Thanks

MG

Remove the additional and unnecessary brackets and you should be fine

INSERT INTO tbRoom ([Ref],[Type])
    SELECT [Ref], substring([Description], 1, charIndex(':',[Description] )-1) 
    FROM tbRoomUsage
    WHERE tbRoomUsage.Ref = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM