I have a query that needs to be output in CSV.
No problem I say, but then this happens:
There are two columns (from and to date) which are date type, but I am trying to store them as nvarchar
or varchar
(tried both). This is in order to be able to insert the column headers in the extract.
I have tried a couple of CONVERT
, CAST
, STUFF
to force the date in the format of YYYY-MM-DD
, but to no avail.
Currently it is in this format: Jan 1 2014 12:00AM
I have tried almost everything I've found on Stack Overflow in similar topics, but nothing has worked for me.
I would also be happy if there could be an alternative on the column names, as I have not experimented with that one a lot.
Currently this is being used:
SELECT 'Column1', 'Column2', 'Column3'
UNION ALL
SELECT * FROM [Database].[dbo].Table
I think you want convert(varchar(10), col, 120)
which will truncate a date format of yyyy-mm-dd hh:mi:ss(24h)
to yyyy-mm-dd
see cast and convert for more details
SELECT
'id' AS id
, 'date_from' AS date_from
, 'date_to' AS date_to
UNION ALL
SELECT
convert(varchar(10), id)
, convert(varchar(10), date_from, 120)
, convert(varchar(10), date_to, 120)
FROM [Database].[dbo].Table
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.