Skip to content
Advertisement

Use SQL Function as select parameter

I have a sql table with a structure like this:

enter image description here

Each column is a hour of day (I can’t change the structure of table)

If I want to get the value of 13:00pm I have to write a query like this:

I want to know if is possible make something like this:

Advertisement

Answer

You need to use dynamic TSQL for the job. You can encapsulate the below script in a scalar function if you want to. I’m assuming the data type for all numbered columns is int.

As mentioned by Gordon Linoff this is a really bad table design. The table should really have 2 columns: one to indicate the hour of the day and the other the associated value.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement