Question

I have this table,

create table Photonics
(
  dt datetime,
  [12:15 AM] decimal(10, 2),
  [12:30 AM] decimal(10, 2),
  [12:45 AM] decimal(10, 2),
  [1:00 AM] decimal(10, 2),
  [1:15 AM] decimal(10, 2)
);


insert into Photonics values
('2012-01-01', 239.04, 247.68, 253.44, 254.88, 253.44),
('2012-01-02', 247.68, 249.12, 246.24, 234.72, 230.40),
('2012-01-03', 259.20, 257.76, 254.88, 243.36, 244.80),
('2012-01-04', 270.72, 269.28, 256.32, 249.12, 253.44),
('2012-01-05', 257.76, 254.88, 263.52, 269.28, 272.16)

This is the Upovit SQL Code

select 'Photonics' name,
  timestamp = dt + cast(timestamp as datetime),
  value
from photonics p
unpivot
(
  value
  for timestamp in ([12:15 AM], [12:30 AM], [12:45 AM], [1:00 AM], [1:15 AM])
) unpiv;

The timestamp is diplayed as the following:

2012-01-01 00:15:00.000
2012-01-01 00:30:00.000
2012-01-01 00:45:00.000
2012-01-01 01:00:00.000
2012-01-01 01:15:00.000

I want the timestamp to be displayed as:

1/1/12 0:15
1/1/12 0:30
1/1/12 0:45
1/1/12 1:00
1/1/12 1:15

I tried GETDATE and all my tries failed. I just wanted to mention that, I did not write the code above, many thanks to bluefeet for the help.

Was it helpful?

Solution

You can convert the date using the following:

timestamp 
= convert(varchar(10), dt, 103) +' '+
  convert(varchar(5), cast(timestamp as datetime), 114)

Making the full query:

select 'Photonics' name,
  timestamp 
    = convert(varchar(10), dt, 103) +' '+
      convert(varchar(5), cast(timestamp as datetime), 114),
  value
from photonics p
unpivot
(
  value
  for timestamp in ([12:15 AM], [12:30 AM], [12:45 AM], [1:00 AM], [1:15 AM])
) unpiv;

See SQL Fiddle with Demo. See a list of SQL Server date formats

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top