December 16, 2021 at 5:11 pm
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
December 16, 2021 at 5:27 pm
Use single quotes for literals, not double quotes.
Date literals should be in the form 'YYYYMMDD'.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 16, 2021 at 5:33 pm
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.
December 16, 2021 at 5:36 pm
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) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
December 16, 2021 at 5:37 pm
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