How to solve “Conversion failed when converting date and/or time from character string” in my case in SQL ServerHow to insert a line break in a SQL Server VARCHAR/NVARCHAR stringHow to return only the Date from a SQL Server DateTime datatypeHow to concatenate text from multiple rows into a single text string in SQL server?Sql Server string to date conversionHow can I get column names from a table in SQL Server?How do I UPDATE from a SELECT in SQL Server?SQL Server: CASE WHEN OR THEN ELSE END => the OR is not supportedConversion failed when converting date and/or time from character stringConversion failed when converting datetime from character stringSQL SERVER 2012: Conversion failed when converting date and/or time from character string

Did "Dirty Harry" feel lucky?

Why did Tony's Arc Reactor do this?

Can taking my 1-week-old on a 6-7 hours journey in the car lead to medical complications?

Why is Sojdlg123aljg a common password?

How strong is aircraft-grade spruce?

Why do the Brexit opposition parties not want a new election?

Is future tense in English really a myth?

How do English-speaking kids loudly request something?

Automatically end list item with proper punctuation (semicolon, period)

Why does PAUSE key have a long make code and no break code?

Yet another calculator problem

How to convert P2O5 concentration to H3PO4 concentration?

Why would an airport be depicted with symbology for runways longer than 8,069 feet even though it is reported on the sectional as 7,200 feet?

Did the Byzantines ever attempt to move their capital to Rome?

What explains the Genie's fate?

If every star in the universe except the Sun were destroyed, would we die?

Is it right to use the ideas of non-winning designers in a design contest?

Problem with listing a directory to grep

What exactly is Apple Cider

Get Emacs to jump to the start of a word after isearch

Why can't some airports handle heavy aircraft while others do it easily (same runway length)?

Does the word voltage exist in academic engineering?

Why is infinite intersection "towards infinity" an empty set?

The Green Glass Door, Revisited



How to solve “Conversion failed when converting date and/or time from character string” in my case in SQL Server


How to insert a line break in a SQL Server VARCHAR/NVARCHAR stringHow to return only the Date from a SQL Server DateTime datatypeHow to concatenate text from multiple rows into a single text string in SQL server?Sql Server string to date conversionHow can I get column names from a table in SQL Server?How do I UPDATE from a SELECT in SQL Server?SQL Server: CASE WHEN OR THEN ELSE END => the OR is not supportedConversion failed when converting date and/or time from character stringConversion failed when converting datetime from character stringSQL SERVER 2012: Conversion failed when converting date and/or time from character string






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am trying to make dynamics query using date, I don't know what is wrong



Actual Data of Time Field from SALES Table



Department Time ExtendedPrice
-------------------------------------
Toys 2018-11-01 1205.11
Toys 2018-12-02 1230.27
Baby Care 2018-12-01 6797.95
Sports 2019-01-01 365.8
Sports 2019-01-01 2993.44
Sports 2018-12-02 580.03
Baby Care 2019-01-02 1117.92
Baby Care 2019-01-02 390.75
soap 2018-11-01 3275.25
Baby Care 2018-12-01 367.56


the above date is sample format of actual data



DECLARE @cols AS Varchar(MAX)
select @cols = STUFF((SELECT ',' + convert(varchar(MAX),Time,105)+''
FROM #Sales
GROUP by Time
ORDER by Time

FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')


Output



select @cols
//output
01-11-2018,02-11-2018,01-12-2018,02-12-2018,01-01-2019,02-01-2019


@query



DECLARE @query AS Varchar(MAX)
SET @query = 'SELECT
' + @cols + '
FROM
(
SELECT
convert(varchar(MAX),Time,105) Time,
ExtendedPrice
FROM
#Sales
) x
pivot
(
sum(ExtendedPrice)
for Time in (' + @cols + ')
) p '
PRINT @query


it shows error following error




Conversion failed when converting date and/or time from character string.




UPDATE



I tried also this



DECLARE @cols AS Varchar(MAX)
SELECT @cols = STUFF((SELECT ',[' + convert(varchar(MAX),Time,105)+']'
FROM #Sales
GROUP by Time
ORDER by Time
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')

select @cols

//output
[01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]









share|improve this question





















  • 2





    Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.

    – Zohar Peled
    Mar 28 at 7:19











  • Don't you think the output of @Cols should looks like '01-11-2018','02-11-2018','01-12-2018','02-12-2018','01-01-2019','02-01-2019'? or even [01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]? in other way STUFF((SELECT ',' + QUOTENAME(convert(varchar(MAX),Time,105))

    – Sami
    Mar 28 at 7:38












  • @Sami I will check your code also. And see my updates

    – Liam neesan
    Mar 28 at 7:41






  • 1





    @Liamneesan I don't think so Live Demo

    – Sami
    Mar 28 at 7:46






  • 1





    @Sami Excellent. Thank you. Make it in answer. I will accept it

    – Liam neesan
    Mar 28 at 7:55

















1















I am trying to make dynamics query using date, I don't know what is wrong



Actual Data of Time Field from SALES Table



Department Time ExtendedPrice
-------------------------------------
Toys 2018-11-01 1205.11
Toys 2018-12-02 1230.27
Baby Care 2018-12-01 6797.95
Sports 2019-01-01 365.8
Sports 2019-01-01 2993.44
Sports 2018-12-02 580.03
Baby Care 2019-01-02 1117.92
Baby Care 2019-01-02 390.75
soap 2018-11-01 3275.25
Baby Care 2018-12-01 367.56


the above date is sample format of actual data



DECLARE @cols AS Varchar(MAX)
select @cols = STUFF((SELECT ',' + convert(varchar(MAX),Time,105)+''
FROM #Sales
GROUP by Time
ORDER by Time

FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')


Output



select @cols
//output
01-11-2018,02-11-2018,01-12-2018,02-12-2018,01-01-2019,02-01-2019


@query



DECLARE @query AS Varchar(MAX)
SET @query = 'SELECT
' + @cols + '
FROM
(
SELECT
convert(varchar(MAX),Time,105) Time,
ExtendedPrice
FROM
#Sales
) x
pivot
(
sum(ExtendedPrice)
for Time in (' + @cols + ')
) p '
PRINT @query


it shows error following error




Conversion failed when converting date and/or time from character string.




UPDATE



I tried also this



DECLARE @cols AS Varchar(MAX)
SELECT @cols = STUFF((SELECT ',[' + convert(varchar(MAX),Time,105)+']'
FROM #Sales
GROUP by Time
ORDER by Time
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')

select @cols

//output
[01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]









share|improve this question





















  • 2





    Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.

    – Zohar Peled
    Mar 28 at 7:19











  • Don't you think the output of @Cols should looks like '01-11-2018','02-11-2018','01-12-2018','02-12-2018','01-01-2019','02-01-2019'? or even [01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]? in other way STUFF((SELECT ',' + QUOTENAME(convert(varchar(MAX),Time,105))

    – Sami
    Mar 28 at 7:38












  • @Sami I will check your code also. And see my updates

    – Liam neesan
    Mar 28 at 7:41






  • 1





    @Liamneesan I don't think so Live Demo

    – Sami
    Mar 28 at 7:46






  • 1





    @Sami Excellent. Thank you. Make it in answer. I will accept it

    – Liam neesan
    Mar 28 at 7:55













1












1








1


2






I am trying to make dynamics query using date, I don't know what is wrong



Actual Data of Time Field from SALES Table



Department Time ExtendedPrice
-------------------------------------
Toys 2018-11-01 1205.11
Toys 2018-12-02 1230.27
Baby Care 2018-12-01 6797.95
Sports 2019-01-01 365.8
Sports 2019-01-01 2993.44
Sports 2018-12-02 580.03
Baby Care 2019-01-02 1117.92
Baby Care 2019-01-02 390.75
soap 2018-11-01 3275.25
Baby Care 2018-12-01 367.56


the above date is sample format of actual data



DECLARE @cols AS Varchar(MAX)
select @cols = STUFF((SELECT ',' + convert(varchar(MAX),Time,105)+''
FROM #Sales
GROUP by Time
ORDER by Time

FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')


Output



select @cols
//output
01-11-2018,02-11-2018,01-12-2018,02-12-2018,01-01-2019,02-01-2019


@query



DECLARE @query AS Varchar(MAX)
SET @query = 'SELECT
' + @cols + '
FROM
(
SELECT
convert(varchar(MAX),Time,105) Time,
ExtendedPrice
FROM
#Sales
) x
pivot
(
sum(ExtendedPrice)
for Time in (' + @cols + ')
) p '
PRINT @query


it shows error following error




Conversion failed when converting date and/or time from character string.




UPDATE



I tried also this



DECLARE @cols AS Varchar(MAX)
SELECT @cols = STUFF((SELECT ',[' + convert(varchar(MAX),Time,105)+']'
FROM #Sales
GROUP by Time
ORDER by Time
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')

select @cols

//output
[01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]









share|improve this question
















I am trying to make dynamics query using date, I don't know what is wrong



Actual Data of Time Field from SALES Table



Department Time ExtendedPrice
-------------------------------------
Toys 2018-11-01 1205.11
Toys 2018-12-02 1230.27
Baby Care 2018-12-01 6797.95
Sports 2019-01-01 365.8
Sports 2019-01-01 2993.44
Sports 2018-12-02 580.03
Baby Care 2019-01-02 1117.92
Baby Care 2019-01-02 390.75
soap 2018-11-01 3275.25
Baby Care 2018-12-01 367.56


the above date is sample format of actual data



DECLARE @cols AS Varchar(MAX)
select @cols = STUFF((SELECT ',' + convert(varchar(MAX),Time,105)+''
FROM #Sales
GROUP by Time
ORDER by Time

FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')


Output



select @cols
//output
01-11-2018,02-11-2018,01-12-2018,02-12-2018,01-01-2019,02-01-2019


@query



DECLARE @query AS Varchar(MAX)
SET @query = 'SELECT
' + @cols + '
FROM
(
SELECT
convert(varchar(MAX),Time,105) Time,
ExtendedPrice
FROM
#Sales
) x
pivot
(
sum(ExtendedPrice)
for Time in (' + @cols + ')
) p '
PRINT @query


it shows error following error




Conversion failed when converting date and/or time from character string.




UPDATE



I tried also this



DECLARE @cols AS Varchar(MAX)
SELECT @cols = STUFF((SELECT ',[' + convert(varchar(MAX),Time,105)+']'
FROM #Sales
GROUP by Time
ORDER by Time
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')

select @cols

//output
[01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]






sql sql-server datetime dynamic-sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 7:41







Liam neesan

















asked Mar 28 at 7:08









Liam neesanLiam neesan

7611 gold badge10 silver badges37 bronze badges




7611 gold badge10 silver badges37 bronze badges










  • 2





    Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.

    – Zohar Peled
    Mar 28 at 7:19











  • Don't you think the output of @Cols should looks like '01-11-2018','02-11-2018','01-12-2018','02-12-2018','01-01-2019','02-01-2019'? or even [01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]? in other way STUFF((SELECT ',' + QUOTENAME(convert(varchar(MAX),Time,105))

    – Sami
    Mar 28 at 7:38












  • @Sami I will check your code also. And see my updates

    – Liam neesan
    Mar 28 at 7:41






  • 1





    @Liamneesan I don't think so Live Demo

    – Sami
    Mar 28 at 7:46






  • 1





    @Sami Excellent. Thank you. Make it in answer. I will accept it

    – Liam neesan
    Mar 28 at 7:55












  • 2





    Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.

    – Zohar Peled
    Mar 28 at 7:19











  • Don't you think the output of @Cols should looks like '01-11-2018','02-11-2018','01-12-2018','02-12-2018','01-01-2019','02-01-2019'? or even [01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]? in other way STUFF((SELECT ',' + QUOTENAME(convert(varchar(MAX),Time,105))

    – Sami
    Mar 28 at 7:38












  • @Sami I will check your code also. And see my updates

    – Liam neesan
    Mar 28 at 7:41






  • 1





    @Liamneesan I don't think so Live Demo

    – Sami
    Mar 28 at 7:46






  • 1





    @Sami Excellent. Thank you. Make it in answer. I will accept it

    – Liam neesan
    Mar 28 at 7:55







2




2





Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.

– Zohar Peled
Mar 28 at 7:19





Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.

– Zohar Peled
Mar 28 at 7:19













Don't you think the output of @Cols should looks like '01-11-2018','02-11-2018','01-12-2018','02-12-2018','01-01-2019','02-01-2019'? or even [01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]? in other way STUFF((SELECT ',' + QUOTENAME(convert(varchar(MAX),Time,105))

– Sami
Mar 28 at 7:38






Don't you think the output of @Cols should looks like '01-11-2018','02-11-2018','01-12-2018','02-12-2018','01-01-2019','02-01-2019'? or even [01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]? in other way STUFF((SELECT ',' + QUOTENAME(convert(varchar(MAX),Time,105))

– Sami
Mar 28 at 7:38














@Sami I will check your code also. And see my updates

– Liam neesan
Mar 28 at 7:41





@Sami I will check your code also. And see my updates

– Liam neesan
Mar 28 at 7:41




1




1





@Liamneesan I don't think so Live Demo

– Sami
Mar 28 at 7:46





@Liamneesan I don't think so Live Demo

– Sami
Mar 28 at 7:46




1




1





@Sami Excellent. Thank you. Make it in answer. I will accept it

– Liam neesan
Mar 28 at 7:55





@Sami Excellent. Thank you. Make it in answer. I will accept it

– Liam neesan
Mar 28 at 7:55












1 Answer
1






active

oldest

votes


















2
















You should Quote the dates as



CREATE TABLE T
([Department] varchar(9), [Time] datetime, [ExtendedPrice] int)
;

INSERT INTO T
([Department], [Time], [ExtendedPrice])
VALUES
('Toys', '2018-11-01 00:00:00', 1205.11),
('Toys', '2018-12-02 00:00:00', 1230.27),
('Baby Care', '2018-12-01 00:00:00', 6797.95),
('Sports', '2019-01-01 00:00:00', 365.8),
('Sports', '2019-01-01 00:00:00', 2993.44),
('Sports', '2018-12-02 00:00:00', 580.03),
('Baby Care', '2019-01-02 00:00:00', 1117.92),
('Baby Care', '2019-01-02 00:00:00', 390.75),
('soap', '2018-11-01 00:00:00', 3275.25),
('Baby Care', '2018-12-01 00:00:00', 367.56)
;

DECLARE @cols AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT ',' + QUOTENAME(CONVERT(VARCHAR(10), Time, 105))
FROM T
GROUP by Time
ORDER by Time
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'');

DECLARE @query AS NVARCHAR(MAX)=
N'SELECT
' + @cols + '
FROM
(
SELECT
convert(varchar(10),Time,105) Time,
ExtendedPrice
FROM
T
) x
pivot
(
sum(ExtendedPrice)
for Time in (' + @cols + ')
) p ';

EXEC sp_executesql @query;


Returns:



+------------+------------+------------+------------+------------+
| 01-11-2018 | 01-12-2018 | 02-12-2018 | 01-01-2019 | 02-01-2019 |
+------------+------------+------------+------------+------------+
| 4480 | 7164 | 1810 | 3358 | 1507 |
+------------+------------+------------+------------+------------+


Demo






share|improve this answer

























  • I need your help. I want to make sum of all previous days (01-11-2018) + (01-12-2018) + (01-01-2019) and NextDays (02-11-2018) + (02-12-2018) + (02-01-2019)

    – Liam neesan
    Mar 28 at 8:40












  • @Liamneesan Just click here and fill the forms, then you will get answers not just one cause this is another question :)

    – Sami
    Mar 28 at 8:44












  • give me a min. I will make new question and text you, that question will be continuity of this question

    – Liam neesan
    Mar 28 at 8:45












  • can you check this link

    – Liam neesan
    Mar 28 at 9:07










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391924%2fhow-to-solve-conversion-failed-when-converting-date-and-or-time-from-character%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2
















You should Quote the dates as



CREATE TABLE T
([Department] varchar(9), [Time] datetime, [ExtendedPrice] int)
;

INSERT INTO T
([Department], [Time], [ExtendedPrice])
VALUES
('Toys', '2018-11-01 00:00:00', 1205.11),
('Toys', '2018-12-02 00:00:00', 1230.27),
('Baby Care', '2018-12-01 00:00:00', 6797.95),
('Sports', '2019-01-01 00:00:00', 365.8),
('Sports', '2019-01-01 00:00:00', 2993.44),
('Sports', '2018-12-02 00:00:00', 580.03),
('Baby Care', '2019-01-02 00:00:00', 1117.92),
('Baby Care', '2019-01-02 00:00:00', 390.75),
('soap', '2018-11-01 00:00:00', 3275.25),
('Baby Care', '2018-12-01 00:00:00', 367.56)
;

DECLARE @cols AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT ',' + QUOTENAME(CONVERT(VARCHAR(10), Time, 105))
FROM T
GROUP by Time
ORDER by Time
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'');

DECLARE @query AS NVARCHAR(MAX)=
N'SELECT
' + @cols + '
FROM
(
SELECT
convert(varchar(10),Time,105) Time,
ExtendedPrice
FROM
T
) x
pivot
(
sum(ExtendedPrice)
for Time in (' + @cols + ')
) p ';

EXEC sp_executesql @query;


Returns:



+------------+------------+------------+------------+------------+
| 01-11-2018 | 01-12-2018 | 02-12-2018 | 01-01-2019 | 02-01-2019 |
+------------+------------+------------+------------+------------+
| 4480 | 7164 | 1810 | 3358 | 1507 |
+------------+------------+------------+------------+------------+


Demo






share|improve this answer

























  • I need your help. I want to make sum of all previous days (01-11-2018) + (01-12-2018) + (01-01-2019) and NextDays (02-11-2018) + (02-12-2018) + (02-01-2019)

    – Liam neesan
    Mar 28 at 8:40












  • @Liamneesan Just click here and fill the forms, then you will get answers not just one cause this is another question :)

    – Sami
    Mar 28 at 8:44












  • give me a min. I will make new question and text you, that question will be continuity of this question

    – Liam neesan
    Mar 28 at 8:45












  • can you check this link

    – Liam neesan
    Mar 28 at 9:07















2
















You should Quote the dates as



CREATE TABLE T
([Department] varchar(9), [Time] datetime, [ExtendedPrice] int)
;

INSERT INTO T
([Department], [Time], [ExtendedPrice])
VALUES
('Toys', '2018-11-01 00:00:00', 1205.11),
('Toys', '2018-12-02 00:00:00', 1230.27),
('Baby Care', '2018-12-01 00:00:00', 6797.95),
('Sports', '2019-01-01 00:00:00', 365.8),
('Sports', '2019-01-01 00:00:00', 2993.44),
('Sports', '2018-12-02 00:00:00', 580.03),
('Baby Care', '2019-01-02 00:00:00', 1117.92),
('Baby Care', '2019-01-02 00:00:00', 390.75),
('soap', '2018-11-01 00:00:00', 3275.25),
('Baby Care', '2018-12-01 00:00:00', 367.56)
;

DECLARE @cols AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT ',' + QUOTENAME(CONVERT(VARCHAR(10), Time, 105))
FROM T
GROUP by Time
ORDER by Time
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'');

DECLARE @query AS NVARCHAR(MAX)=
N'SELECT
' + @cols + '
FROM
(
SELECT
convert(varchar(10),Time,105) Time,
ExtendedPrice
FROM
T
) x
pivot
(
sum(ExtendedPrice)
for Time in (' + @cols + ')
) p ';

EXEC sp_executesql @query;


Returns:



+------------+------------+------------+------------+------------+
| 01-11-2018 | 01-12-2018 | 02-12-2018 | 01-01-2019 | 02-01-2019 |
+------------+------------+------------+------------+------------+
| 4480 | 7164 | 1810 | 3358 | 1507 |
+------------+------------+------------+------------+------------+


Demo






share|improve this answer

























  • I need your help. I want to make sum of all previous days (01-11-2018) + (01-12-2018) + (01-01-2019) and NextDays (02-11-2018) + (02-12-2018) + (02-01-2019)

    – Liam neesan
    Mar 28 at 8:40












  • @Liamneesan Just click here and fill the forms, then you will get answers not just one cause this is another question :)

    – Sami
    Mar 28 at 8:44












  • give me a min. I will make new question and text you, that question will be continuity of this question

    – Liam neesan
    Mar 28 at 8:45












  • can you check this link

    – Liam neesan
    Mar 28 at 9:07













2














2










2









You should Quote the dates as



CREATE TABLE T
([Department] varchar(9), [Time] datetime, [ExtendedPrice] int)
;

INSERT INTO T
([Department], [Time], [ExtendedPrice])
VALUES
('Toys', '2018-11-01 00:00:00', 1205.11),
('Toys', '2018-12-02 00:00:00', 1230.27),
('Baby Care', '2018-12-01 00:00:00', 6797.95),
('Sports', '2019-01-01 00:00:00', 365.8),
('Sports', '2019-01-01 00:00:00', 2993.44),
('Sports', '2018-12-02 00:00:00', 580.03),
('Baby Care', '2019-01-02 00:00:00', 1117.92),
('Baby Care', '2019-01-02 00:00:00', 390.75),
('soap', '2018-11-01 00:00:00', 3275.25),
('Baby Care', '2018-12-01 00:00:00', 367.56)
;

DECLARE @cols AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT ',' + QUOTENAME(CONVERT(VARCHAR(10), Time, 105))
FROM T
GROUP by Time
ORDER by Time
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'');

DECLARE @query AS NVARCHAR(MAX)=
N'SELECT
' + @cols + '
FROM
(
SELECT
convert(varchar(10),Time,105) Time,
ExtendedPrice
FROM
T
) x
pivot
(
sum(ExtendedPrice)
for Time in (' + @cols + ')
) p ';

EXEC sp_executesql @query;


Returns:



+------------+------------+------------+------------+------------+
| 01-11-2018 | 01-12-2018 | 02-12-2018 | 01-01-2019 | 02-01-2019 |
+------------+------------+------------+------------+------------+
| 4480 | 7164 | 1810 | 3358 | 1507 |
+------------+------------+------------+------------+------------+


Demo






share|improve this answer













You should Quote the dates as



CREATE TABLE T
([Department] varchar(9), [Time] datetime, [ExtendedPrice] int)
;

INSERT INTO T
([Department], [Time], [ExtendedPrice])
VALUES
('Toys', '2018-11-01 00:00:00', 1205.11),
('Toys', '2018-12-02 00:00:00', 1230.27),
('Baby Care', '2018-12-01 00:00:00', 6797.95),
('Sports', '2019-01-01 00:00:00', 365.8),
('Sports', '2019-01-01 00:00:00', 2993.44),
('Sports', '2018-12-02 00:00:00', 580.03),
('Baby Care', '2019-01-02 00:00:00', 1117.92),
('Baby Care', '2019-01-02 00:00:00', 390.75),
('soap', '2018-11-01 00:00:00', 3275.25),
('Baby Care', '2018-12-01 00:00:00', 367.56)
;

DECLARE @cols AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT ',' + QUOTENAME(CONVERT(VARCHAR(10), Time, 105))
FROM T
GROUP by Time
ORDER by Time
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'');

DECLARE @query AS NVARCHAR(MAX)=
N'SELECT
' + @cols + '
FROM
(
SELECT
convert(varchar(10),Time,105) Time,
ExtendedPrice
FROM
T
) x
pivot
(
sum(ExtendedPrice)
for Time in (' + @cols + ')
) p ';

EXEC sp_executesql @query;


Returns:



+------------+------------+------------+------------+------------+
| 01-11-2018 | 01-12-2018 | 02-12-2018 | 01-01-2019 | 02-01-2019 |
+------------+------------+------------+------------+------------+
| 4480 | 7164 | 1810 | 3358 | 1507 |
+------------+------------+------------+------------+------------+


Demo







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 7:59









SamiSami

11.4k3 gold badges16 silver badges46 bronze badges




11.4k3 gold badges16 silver badges46 bronze badges















  • I need your help. I want to make sum of all previous days (01-11-2018) + (01-12-2018) + (01-01-2019) and NextDays (02-11-2018) + (02-12-2018) + (02-01-2019)

    – Liam neesan
    Mar 28 at 8:40












  • @Liamneesan Just click here and fill the forms, then you will get answers not just one cause this is another question :)

    – Sami
    Mar 28 at 8:44












  • give me a min. I will make new question and text you, that question will be continuity of this question

    – Liam neesan
    Mar 28 at 8:45












  • can you check this link

    – Liam neesan
    Mar 28 at 9:07

















  • I need your help. I want to make sum of all previous days (01-11-2018) + (01-12-2018) + (01-01-2019) and NextDays (02-11-2018) + (02-12-2018) + (02-01-2019)

    – Liam neesan
    Mar 28 at 8:40












  • @Liamneesan Just click here and fill the forms, then you will get answers not just one cause this is another question :)

    – Sami
    Mar 28 at 8:44












  • give me a min. I will make new question and text you, that question will be continuity of this question

    – Liam neesan
    Mar 28 at 8:45












  • can you check this link

    – Liam neesan
    Mar 28 at 9:07
















I need your help. I want to make sum of all previous days (01-11-2018) + (01-12-2018) + (01-01-2019) and NextDays (02-11-2018) + (02-12-2018) + (02-01-2019)

– Liam neesan
Mar 28 at 8:40






I need your help. I want to make sum of all previous days (01-11-2018) + (01-12-2018) + (01-01-2019) and NextDays (02-11-2018) + (02-12-2018) + (02-01-2019)

– Liam neesan
Mar 28 at 8:40














@Liamneesan Just click here and fill the forms, then you will get answers not just one cause this is another question :)

– Sami
Mar 28 at 8:44






@Liamneesan Just click here and fill the forms, then you will get answers not just one cause this is another question :)

– Sami
Mar 28 at 8:44














give me a min. I will make new question and text you, that question will be continuity of this question

– Liam neesan
Mar 28 at 8:45






give me a min. I will make new question and text you, that question will be continuity of this question

– Liam neesan
Mar 28 at 8:45














can you check this link

– Liam neesan
Mar 28 at 9:07





can you check this link

– Liam neesan
Mar 28 at 9:07








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391924%2fhow-to-solve-conversion-failed-when-converting-date-and-or-time-from-character%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript