Trying to show a date as blank in a total row - sql-server

SQL Server : when I select my totals row, for date I select ' ' but it gets converted to 1/1/1900 because it is a date column for all other rows. What can i do to make it say blank.
I'm trying to show blank on a total line, on the detail line it's a date. On the total line it's 1/1/1900 so can be confusing. I would like to show blank on the total line.
Here is an example of my total row. some of the blank values are dates in the detail.
UNION ALL
SELECT TOP 99999999
AddUpdate
, Members_SourceCode
, YearMonth + ' Total Month'
, ' '
, ' '
, ' '
, ' '
, ' '
, ' '
, ' '
, ' '
, ' '
, ' '
, ' '
, ' '
, COUNT(Members_sourceCode)
GROUP BY AddUpdate, Members_SourceCode, YearMonth
ORDER BY AddUpdate, Members_SourceCode, YearMonth

Nothing represents the default value of a data type. The default value depends on whether the variable is of a value type or of a reference type.
Nothing keyword (Visual Basic) - Remarks
With VB.NET and EF 6.X to save null is:
Dim nullableData As New Nullable(Of Date)

Related

Need help adding WHERE clause in pre-written SQL statement

Preface: my SQL is rudimentary. I received a SQL query from a vendor, it selects and exports every single employee comment and other data from a few different DBs as CSV meant for import, it was written by them but they're not helping with this request. The query is pulling so much data it makes a large time consuming file for import. So I want to add to / modify the query to have a "WHERE date > whateverdate" to narrow my results to recent data. For example, I want to pull only comments entered in the past 2 days.
The column I'm looking to add the clause for is the column "A.CMS502", defined as datetime. I believe this is the only relevant column in this query. An example date in this column is "2003-10-06 17:05:21.000". I am using SQL Server 2008 if it helps. Is it possible here? Thank you.
SELECT
'ID,Acct/LnNbr,NoteCreatedDate,CollectorId,ApplytoAll,Note'
UNION ALL
SELECT
ID + ',' + ID + ',' + NoteCreatedDate + ',' + CollectorId + ',' + 'No' + ',' + Note
FROM
(SELECT
CASE WHEN SUBSTRING(A.CMS301,LEN(A.CMS301),1) = 'S'
THEN SUBSTRING(A.CMS301,1,LEN(A.CMS301) - 1)
ELSE A.CMS301
END + '-' +
CASE WHEN SUBSTRING(A.CMS301,LEN(A.CMS301),1) = 'S'
THEN 'S' ELSE 'L'
END AS [ID],
REPLACE(CONVERT(VARCHAR, A.CMS501, 10), '-', '') AS [NoteCreatedDate],
CASE WHEN U.CMS1201 IS NOT NULL
THEN U.CMS1205 + ' ' + U.CMS1204
ELSE (SELECT CMS1205 + ' ' + CMS1204 FROM sysUSER WHERE CMS1201 = 'PSUSER')
END AS CollectorId,
CAST(A.CMS512 AS NVARCHAR(MAX)) AS [Note]
FROM
ACTIVITY AS A
LEFT JOIN
sysUSER AS U ON A.CMS503 = U.CMS1201
WHERE
A.CMS504 NOT IN (411,500,511,711,804,900,901,903,907,2000,999777)
AND A.CMS504 NOT BETWEEN 1102 AND 1199) AS S
Try this, this will output last 2 days.
SELECT 'ID,Acct/LnNbr,NoteCreatedDate,CollectorId,ApplytoAll,Note'
UNION ALL
SELECT ID + ',' + ID + ',' + NoteCreatedDate + ',' + CollectorId + ',' + 'No' + ',' + Note
FROM
(
SELECT CASE WHEN SUBSTRING(A.CMS301,LEN(A.CMS301),1) = 'S' THEN SUBSTRING(A.CMS301,1,LEN(A.CMS301) - 1) ELSE A.CMS301 END
+ '-' + CASE WHEN SUBSTRING(A.CMS301,LEN(A.CMS301),1) = 'S' THEN 'S' ELSE 'L'
END AS [ID]
,REPLACE(CONVERT(varchar,A.CMS501,10),'-','') AS [NoteCreatedDate]
,CASE WHEN U.CMS1201 IS NOT NULL THEN U.CMS1205 + ' ' + U.CMS1204 ELSE
(SELECT CMS1205 + ' ' + CMS1204 FROM sysUSER WHERE CMS1201 = 'PSUSER')
END AS CollectorId
,CAST(A.CMS512 AS nvarchar(max)) AS [Note]
FROM ACTIVITY AS A
LEFT JOIN sysUSER AS U
ON A.CMS503 = U.CMS1201
WHERE A.CMS504 NOT IN (411,500,511,711,804,900,901,903,907,2000,999777)
AND A.CMS504 NOT BETWEEN 1102 AND 1199
AND A.CMS502 >= DATEADD(D, -2, GETDATE())
) AS S

SQL Server build dynamic sql

I have a temp table called #temp, and I need to get all the CDate column from that table, to build a string.
The CDate list in that table is (20171209, 20171210....20171223)
I expected to see
'A.[20171209] as [20171209], A.[20171210] as [20171210],
A.[20171211] as [20171211], A.[20171212] as [20171212],
A.[20171213] as [20171213], A.[20171214] as [20171214],
A.[20171215] as [20171215], A.[20171216] as [20171216],
A.[20171217] as [20171217], A.[20171218] as [20171218],
A.[20171219] as [20171219], A.[20171220] as [20171220],
A.[20171221] as [20171221], A.[20171222] as [20171222],
A.[20171223] as [20171223], '
however the result I got is missing the first date , ie 'A.[20171209] as [20171209]'
Here is my code:
SELECT
#col2 = ISNULL(#col2 + 'A.' + QUOTENAME(CDate) + ' as ' + QUOTENAME(CDate) + ', ' , '')
FROM
(SELECT DISTINCT CDate FROM #temp) AS tmp;
Your current approach will not work in some cases, it is an undocumented feature, always use For Xml path to concatenating the rows into csv.
SET #col2 = stuff((SELECT ', A.' + Quotename(CDate) + ' as '
+ Quotename(CDate)
FROM (SELECT DISTINCT CDate
FROM #temp) a
FOR xml path('')),1,1,'')

Using COALESCE and Converting datetime fields

I was given some code from a current user that I can't figure out how to convert to Microsoft SQL Server 2014.
,TO_CHAR( COALESCE( TASK.ACT_START_DATE, TASK.TARGET_START_DATE,
TASK.EARLY_START_DATE ), 'MM/DD/YYYY HH24:MI')
||
COALESCE( NVL2( TASK.ACT_START_DATE, ' A', NULL),
NVL2`enter code here`( TASK.TARGET_START_DATE, ' P', NULL),
NVL2( TASK.EARLY_START_DATE, ' E', NULL)) AS START_DATE_FMT
The date fields have to be converted in order to add either an "A", "P" or an "E" behind the date.
Is there a way to use a similar code in SQL Server to get the information? I've considered using a CASE statement to validate for Null but can't get the syntax correct.
Try this...
FORMAT( COALESCE( TASK.ACT_START_DATE, TASK.TARGET_START_DATE,TASK.EARLY_START_DATE ), 'MM/dd/yyyy HH:mm') +
CASE WHEN TASK.ACT_START_DATE IS NOT NULL THEN ' A'
WHEN TASK.TARGET_START_DATE IS NOT NULL THEN ' P'
WHEN TASK.EARLY_START_DATE IS NOT NULL THEN ' E'
ELSE '' END AS START_DATE_FMT
Here is another option. Don't think you need a case expression here.
select
FORMAT( COALESCE( TASK.ACT_START_DATE, TASK.TARGET_START_DATE,TASK.EARLY_START_DATE ), 'MM/dd/yyyy HH:mm')
+ coalesce(' A' + CONVERT(VARCHAR(19),TASK.ACT_START_DATE,20)
, ' P' + CONVERT(VARCHAR(19), TASK_TARGET_START_DATE,20)
, ' E' + CONVERT(VARCHAR(19), TASK.EARLY_START_DATE,20))

SQL Server query: Concatenating two strings and converting to datetime changes the format thereby Datedifference calculated is wrong

Below is the query which I am trying to run:
SELECT
CONVERT(DATETIME,('12/1/2016' +' '+ '2:00:00')) AS A,
CONVERT(DATETIME,('12/1/2016' +' '+ '2:00:00')) AS B,
DATEDIFF(HOUR, CONVERT(DATETIME, ('12/1/2016' + ' ' + '2:00:00')), CONVERT(DATETIME, ('12/1/2016' + ' ' + '2:00:00'))) as DateDiffernce
Output is:
A B DateDiffernce
2016-12-01 02:00:00.000 2016-12-01 02:00:00.000 0
Let's take 12/1/2016 here the format is DD/MM/YYYY. After using convert function it is getting changed to YYYY-MM-DD which is fine the problem is places are changed i.e 12/1/2016 to 2016-12-01 12 which was the date is shifted or considered as month - which should not happen.
I even tried another query to restrict this conversion which didn't help:
SELECT
CONVERT(DATETIME, CONVERT(CHAR(10), '12/1/2016', 112)
+ ' ' + CONVERT(CHAR(8), '2:00:00', 108))
Kindly help..
Please check #Panagiotis Kanavos comment. He is right. Your query would work fine. Please change the time value like the below to get the DateDifference value.
SELECT CONVERT(DATETIME, ('13/1/2016' + ' ' + '2:00:00'),103) AS A
,CONVERT(DATETIME, ('13/1/2016' + ' ' + '13:00:00'),103) AS B
,DATEDIFF(HOUR, CONVERT(DATETIME, ('13/1/2016' + ' ' + '2:00:00'),103), CONVERT(DATETIME, ('13/1/2016' + ' ' + '13:00:00'),103)) AS DateDiffernce
Hello All Thankyou for your inputs, i was able to solve the issue by
using following code
SET DATEFORMAT YDM
Cheers!! Enjoy coding

SQL Server Row size

We are creating this crosstab report.. Generating query at time in SQL Server 2008.
In one of selection when user make Program Name as a column it is giving below error:
Creating or altering table 'FakeWorkTable' failed because the minimum row size would be 11852, including 189 bytes of internal overhead. This exceeds the maximum allowable table row size of 8094 bytes.
Query shall return like:
Date Program 1 ... Program 100... Program 500
It will tell some information about TV program datewise.
Is there any way to increase this row size?
Please let me know in-case any other information is needed.
Best Regards
My Code as below:
set #query
= 'SELECT ' + #PivotRowColumn + ',' + #cols + ' from
(
SELECT ' + #SelectCalculationColumn + ', ' + #PivotRowColumn + ', ' + #PivotColumn + '
FROM ##ResultCrosstab
--where programName like ''S%''
) x
pivot
(
' + #CalculateColumn + '
for ' + #PivotColumn + ' in (' + #cols + ')
) p '
#PivotRowColumn/#PivotColumn can be anything out of (SalesHouse/Station/Day/Week/Month/Date/Product/Program/SpotLength/Timeband/Campaign.
#SelectCalculationColumn is a KPI e.g Spots/Budget/Impacts/Variance/TVR etc.
#cols are column
This issue comes very rarely for bigger campaigns. I have added a drop down in-case user select programs as column. So in-case user gets error they can limit the program or use Filter (which is already in place)

Resources