Skip to main content

Posts

Showing posts from December, 2013

Character Limit for a Variable- Exec(@Variable) issues and solutions

Hi All, When you are using Varchar (MAX) in declaring a Variable, Always remember, that VARCHAR(MAX) can hold only 8000 characters. When we try to print Variables, PRINT Command display only 4000 character max. When we try to execute, Variable (MAX) which is more than 8000 Character, we may not get exact Error to solve. The problems seems to be with SET Statement which accepts only 4000 Characters. In Order to Execute a Variable which is having VARCHAR (MAX) and the length of the strings are more than 8000 Character, Then best way to work is Splitting the Queries with Multiple Variable. Example, If you have Dynamic Query which is 10000 characters, and is used as a string in a variable, Then, we need to Split the Queries. Find a Logical Break Point and Then concatenate remaining variables. DECLARE @Query VARCHAR ( MAX )       DECLARE @Query1 VARCHAR ( MAX )       DECLARE @Query2 VARCHAR ( MAX )       DECLARE @Query3 VARCHAR ( MAX )       SET @Query1 =

Query to fetch Last 'N' Year Data or Last 'N' Months data or Last 'N' Day data

Here is the Query to fetch Last 'N' Year Data or Last 'N' Months data or Last 'N' Day data. Trick is using  DATEADD(YEAR,-2,GETDATE()). This Query will fetch Last 2 year record. Change Year to Months and Number, to get last 'N' Month data and so on. SELECT * FROM TABLE_Name WHERE DATE_COLUMN > DATEADD ( YEAR , - 2 , GETDATE ( ) )

Query to fetch Last 'N' Year Data or Last 'N' Months data or Last 'N' Day data

Here is the Query to fetch Last 'N' Year Data or Last 'N' Months data or Last 'N' Day data. Trick is using  DATEADD(YEAR,-2,GETDATE()). This Query will fetch Last 2 year record. Change Year to Months and Number, to get last 'N' Month data and so on. SELECT * FROM TABLE_Name WHERE DATE_COLUMN > DATEADD ( YEAR , - 2 , GETDATE ( ) )