Enclosure

  • Hi

    Is this correct way to enclose Dates & columns etc

    SET @Where = @Where + ' NOT(T1.U_APDE = ''N'' AND T1.U_ARDE = ''N'') and (T1.U_ItemCode is not null) and (T0.U_DATE BETWEEN ''2021-03-01'' and ''2021-03-31'') '

    Thanks

  • Use single quotes for literals, not double quotes.

    Date literals should be in the form 'YYYYMMDD'.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Hi

    WHen i write like this it says Incorrect syntax near 2021

    Set @Sql = 'Select * from tbl1 '

    SET @Where = @Where + ' T0.U_DATE BETWEEN '2021-03-01' and '2021-03-31' '

  • To avoid ambiguity, it's better to use columname >= startdatetime and columname < enddatetime -- 2021-04-01 would appear to be the enddatetime you want in your example (assuming you want to include everything in March 2021),

    BETWEEN is inclusive of the enddatetime, which may cause unpredictable/undesirable results depending on the precision of the data stored.

     

  • Within other quotes, you have to double up the single quotes around the date, like so:

    SET @Where = @Where + ' T0.U_DATE BETWEEN ''20210301'' and ''20210331'' '

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

  • To add to what Phil is saying - I would try it out and see what happens.  Basically, run your code and then either SELECT or PRINT @Where to see what the value is and make sure that things look like what you are expeting.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply