How to add a single quotation mark to a date parameter in SQL

Source: Internet
Author: User

As the following statements, @start and @end are the parameters of the date type

Set @sql='SELECT * from'+cast(@viewName  as nvarchar)+'where Rcy_rq >='+CONVERT(nvarchar( -),@start,121)+'and Rcy_rq <='+CONVERT(nvarchar( -),@end,121)

The SQL statements entered at execution time are as follows:

Select *  fromview2010whereRcy_rq>=  .- A- in xx:xx:00.000and Rcy_rq <= .- A- to xx:xx:00.000

How to place single quotation marks (') on the two times of the date?

Someone on the internet said to use the other words multibyte to the desired position, and then replace with the Replace function to single quotation marks, I think this can be handled as follows:

    Declare @point nvarchar (5)     Set @point = Char (a)-single quotation mark

The SQL statement is changed as follows:

Set @sql='SELECT * from'+cast(@viewName  as nvarchar)+'where Rcy_rq >='+@point+CONVERT(nvarchar( -),@start,121)+@point+'and Rcy_rq <='+@point+CONVERT(nvarchar( -),@end,121)+@point 

It's a perfect solution to the problem.

How to add a single quotation mark to a date parameter in SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.