sql server - SQL query database on column data type with a condition -


for example, in sql server(10.0.5520. sql server 2008 sp3), have table 3 columns of type datetime.

our goal data has 1 of 3 columns > specificeddate don't want list out specified columns 1 one.

please view example below:

table (id int,           name varchar,           createdate datetime,           modifydate datetime,           voiddate datetime) 

normally be:

select * createdate > 'x' or modifydate > 'x' or voiddate > 'x' 

it should turn to

select * (if column datetime , column.data > 'x') 

can query in way? if yes, how can that?

you can use sys.columns columns given table datetime.

select c.name sys.columns c c.object_id = object_id('a') ,       c.system_type_id = type_id('datetime') 

then can use build , execute query using sp_executesql.

declare @sql nvarchar(max)  set @sql = ' select * dbo.a '+stuff((               select 'or '+quotename(c.name) + ' > @value '               sys.columns c               c.object_id = object_id('a') ,                     c.system_type_id = type_id('datetime')               xml path(''), type               ).value('text()[1]', 'nvarchar(max)'), 1, 3, '')   --print @sql  exec sp_executesql @sql, n'@value datetime', '2015-07-01' 

select * dbo.a [createdate] > @value or [modifydate] > @value or [voiddate] > @value  

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -