Convert date and find date - sql

I'm having some trouble with syntax.
First, I'd like to convert a date from this format:
2014-12-18 21:49:54.047
To this format:
20141218
I can do this just fine using this select statement:
SELECT F253 = CONVERT (VARCHAR (20), F253, 112)
FROM SAL_HDR
My problem is with the syntax. How do I put that select statement inside of a select statement with a bunch of other lines? I can't seem to make it work right. I have commented out the line in question. How do I write that line into the larger select statement?
SELECT
SAL_HDR.F253 AS [Transaction Date],
/*F253 = CONVERT (VARCHAR (20), F253, 112)*/
SAL_HDR.F1036 AS [Transaction Time],
SAL_HDR.F1032 AS [Transaction #],
FROM SAL_HDR

Just add it in as an expression:
SELECT CONVERT(VARCHAR(20), SAL_HDR.F253, 112) AS [Transaction Date],
SAL_HDR.F1036 AS [Transaction Time],
SAL_HDR.F1032 AS [Transaction #]
FROM SAL_HDR;
Alternatively, you can use the syntax:
SELECT [Transaction Date] = CONVERT(VARCHAR(20), SAL_HDR.F253, 112),
[Transaction Time] = SAL_HDR.F1036,
[Transaction #] = SAL_HDR.F1032
FROM SAL_HDR;
I personally prefer the first version, because I find that the second is too close to variable assignment.

Related

How can I use Sum, Cast and Partition by functions together?

How can I use Sum, Cast and Partition by functions together?
I get error.
Conversion failed when converting the nvarchar value '693.41' to data
type int."
I tried this
SUM(CAST([total price] AS INT)) OVER (PARTITION BY (ProjectType)) as TotalPriceV2
Full query is at the below.
SELECT
ID,
[Project Manager],
Job#,
[Date],
[Job Type],
first_value([Job Name]) OVER (PARTITION BY value_partition ORDER BY ID) CustomerGroup, Value_Partition
Customer,
[Sales Rep1],
DeliveryType,
ProjectType,
[Item Price],
[Service Price],
[Total Price],
SUM(CAST([total price] AS INT)) OVER (PARTITION BY (ProjectType)) as TotalPriceV2
FROM (
SELECT
ID,
[Project Manager], Job#, [Date], [Job Type], [Job Name],Customer,[Sales Rep1],DeliveryType,ProjectType,[Item Price],[Service Price],[Total Price],
SUM(CASE WHEN [JOB NAME] IS NULL THEN 0 ELSE 1 END) OVER (ORDER BY ID) AS Value_Partition
FROM Testing2
WHERE
[Date] IS NOT NULL AND
([Date] NOT LIKE '0' OR JOB# NOT LIKE '0' OR [JOB TYPE] NOT LIKE '0')
AND [Project Manager] NOT LIKE 'ITEM / SERVICE'
) AS X
First, identify the rows that have problems. You are converting to an int, so:
select price
from Testing2
where try_convert(int, price) is null and price is not null;
At least some of the values will have decimal points (as in your example). To see if this is the only problem, I would suggest converting to a decimal next:
select price
from Testing2
where try_convert(decimal(20, 4), price) is null and price is not null;
If this returns nothing, you are set. If not, you will need to figure out how to address these exceptions.
Then, I would phrase the calculation using decimals and not floats:
SUM(TRY_CAST([total price] AS DECIMAL(20, 4))) OVER (PARTITION BY (ProjectType)) as TotalPriceV2
Monetary amounts should generally be represented using fixed-point values rather than approximate floating point values. The issue with floating point values is that information may be lost. Consider this example with reals:
select cast(1000000 as real) + cast(0.01 as real),
cast(1000000 as decimal(20, 4)) + cast(0.01 as decimal(20, 4))
Integer will not have decimal values, and it will throw an error.
If I use this, similar to the example you have.
Declare #val nvarchar(10) = '10.24'
select cast (#val as int)
I get this following error.
Msg 245, Level 16, State 1, Line 3
Conversion failed when converting the nvarchar value '10.24' to data type int.
Now to avoid this error, I can round the digit to 0 decimal places and convert as Int. Also, I would recommend to use Try_Cast to avoid any cases of actual nvarchar values(it would convert it to null) if you are using sql server 2012 or more.
Declare #val nvarchar(10) = '10.24'
select Try_cast (round(#val,0) as int) as valuen
Output, I think this will work in your sum function.
valuen
10
Why don't you cast it in inner query with decimal(16,9) instead of int
SELECT
ID,
[Project Manager],
Job#,
[Date],
[Job Type],
first_value([Job Name]) OVER (PARTITION BY value_partition ORDER BY ID) CustomerGroup, Value_Partition
Customer,
[Sales Rep1],
DeliveryType,
ProjectType,
[Item Price],
[Service Price],
[Total Price],
SUM([total price]) OVER (PARTITION BY (ProjectType)) as TotalPriceV2
FROM (
SELECT
ID,
[Project Manager], Job#, [Date], [Job Type], [Job Name],Customer,[Sales Rep1],DeliveryType,ProjectType,[Item Price],[Service Price],cast([Total Price] as decimal(16,9)) as [Total Price],
SUM(CASE WHEN [JOB NAME] IS NULL THEN 0 ELSE 1 END) OVER (ORDER BY ID) AS Value_Partition
FROM Testing2
WHERE
[Date] IS NOT NULL AND
([Date] NOT LIKE '0' OR JOB# NOT LIKE '0' OR [JOB TYPE] NOT LIKE '0')
AND [Project Manager] NOT LIKE 'ITEM / SERVICE'
) AS X
If you have a combination of string and number as your column value and you want to sum up the number. For SQL Server 2012+ you can use TRY_CAST or TRY_CONVERT to avoid any casting errors.
TRY_CAST (Transact-SQL)
In your scenario, you are not using proper data type for casting, you have values like 693.41 which can't be casted to INT, instead you should use DECIMAL datatype.
One more suggestion, instead of using NOT LIKE, better use <> in SQL Server like following.
Change
([Date] NOT LIKE '0' OR JOB# NOT LIKE '0' OR [JOB TYPE] NOT LIKE '0')
to
([Date] <> '0' OR JOB# <> '0' OR [JOB TYPE] <> '0')
One possible solution is to firstly convert the nvarchar value to decimal and then to integer:
CAST(CAST([total price] AS FLOAT) AS INT)
However, in this way the decimal part is lost if it matters to you.

How to solve this SQL WHERE (date and exception) Syntax Error?

Could you take a look at the query below please? I tried to convert my access table into a SQL query with very little knowledge (for now).
The last line seems to be wrong when I execute it.
USE [idb_datastore]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[idb_dataSTORESQLTesting]
AS
IF OBJECT_ID( 'idb_datastore.dbo.[Testing]','U' ) IS NOT NULL
DROP TABLE idb_datastore.dbo.[Testing];
SELECT dbo.REC_HDR.F91 AS [PO Number],
hd.F1032 AS [Trs Number],
hd.F76 AS [Order Date],
hd.F27 AS [Vendor ID],
hd.F334 AS [Vendor Name],
hd.F1246 AS [Delivery Date],
hd.F1127 AS [Operator Short Name],
hd.F1068 AS State,
hd.F1067 AS Status
FROM SMSSERVER01.STORESQL.dbo.REC_HDR hd
WHERE hd.F91 Is Not Null AND hd.F76 >= Convert(datetime, ’2017/01/01’ ) AND
hd.F1068 NOT like ’Voided’
Here
SQL datetime format is yyyy-mm-dd hh:mm:ss
change the final line to this
Convert(datetime, '2017-01-01' )
Change the where sentence to this
WHERE hd.F91 Is Not Null
AND hd.F76 >= CONVERT(DATETIME, '03/13/2013', 101)
AND hd.F1068 NOT like '%Voided%'

Find Gap in Dates SQL

I'm using Microsoft SQL Server and have 2 tables, AbsenceHistory and FITNoteHistory.
AbsenceHistory:
[Employee Number], [Absence Number], [Start Date], [End Date]
FITNoteHistory:
[Absence Number], [FIT Note Number], [Start Date], [End Date]
I need to identify where there is a gap in the FIT Note History, that doesn't cover the entire Absence Period between DateAdd(d,7,AbsenceHistory.[Start Date]) and AbsenceHistory.[End Date], where AbsenceHistory.[End Date] is not null and DATEDIFF(d,AbsenceHistory.[Start Date],AbsenceHistory.[End Date]) >= 7.
The output needs to give me the actual date gaps for each absence.
E.g. Absence Number, Date of Gap
Can anyone help?
Example Data:
AbsenceHistory:
[Employee Number], [Absence Number], [Start Date], [End Date]
18615, 70, '01-Jan-2018', '31-Jan-2018'
FITNoteHistory:
[Absence Number], [FIT Note Number], [Start Date], [End Date]
70, 1, '08-Jan-2018', '15-Jan-2018'
70, 15, '18-Jan-2018', '24-Jan-2018'
70, 31, '26-Jan-2018', '01-Feb-2018'
My expected output would be:
[Employee Number], [Absence Number], [Gap Date]
18615, 70, '16-Jan-2018'
18615, 70, '17-Jan-2018'
18615, 70, '25-Jan-2018'
This should get you started
declare #t table ([Absence Number] int, [FIT Note Number] int, [Start Date] date, [End Date] date)
insert into #t values
(70, 1, '08-Jan-2018', '15-Jan-2018')
, (70, 15, '18-Jan-2018', '24-Jan-2018')
, (70, 31, '26-Jan-2018', '01-Feb-2018');
select t.[Absence Number], t.[FIT Note Number], t.[Start Date], t.[End Date]
from #t t
order by t.[Start Date], t.[End Date], t.[FIT Note Number];
declare #minDate date = (select min([End Date]) from #t);
declare #maxDate date = (select max([Start Date]) from #t);
with cteDate as
(
select #minDate as dDate
union all
select cast(dateadd(dd, 1, dDate) as date)
from cteDate
where dDate < #maxDate
)
, cteNext as
( select t.[Absence Number], t.[FIT Note Number], t.[Start Date], t.[End Date]
, lead(t.[Start Date], 1) OVER (ORDER BY t.[Start Date]) AS nextStart
from #t t
)
select n.[Absence Number], n.[FIT Note Number], n.[Start Date], n.[End Date]
, d.dDate
from cteNext n
join cteDate d
on d.dDate > n.[End Date]
and d.dDate < n.nextStart
order by d.dDate;

for every row get all dates between columns and insert into column

I have table variable that I fill with all dates between a particular range (and the dw,wk,mm,yy,dd values)
I then join this to my inspection records by the start date.
SELECT CalDate
, DayN
, WeekN
, YearN
, DayOfMonthN
, [Start Date]
, [End Date]
, [Inspection Number]
FROM #Calendar AS dateInfo
LEFT JOIN [Inspection Records] AS IR
ON IR.[Start Date] = dateInfo.CalendarDate
This works fine, what I am now needing to do (and i have to do it this way to pass to a 3rd party application...) is get all the dates between the [Start Date] and [End Date] and add them into a column as a comma separated list.
I've come across this answer t-sql get all dates between 2 dates
that seems to get the dates perfectly for what I need.
I'm stuck on how to structure my script to use that script to get all the dates between my [Start Date] and [End Date] and add them into a column.
what is the best way to calculated all dates between my two columns
how to insert the outcome into a single column as comma separated list
NOTE: I can not add anything to the database.
Example: (Trying to get the dates column)
<table>
<tr>
<td>CalDate</td>
<td>DayN</td>
<td>WeekN</td>
<td>YearN</td>
<td>DayOfMonthN</td>
<td>Start Date</td>
<td>End Date</td>
<td>Inspection Number</td>
<td>Dates</td>
</tr>
<tr>
<td>07-08-2014</td>
<td>5</td>
<td>32</td>
<td>2014</td>
<td>7</td>
<td>07-08-2014</td>
<td>11-08-2014</td>
<td>A0001</td>
<td>07-08-2014,08-08-2014,09-08-2014,10-08-2014,11-08-2014</td>
</tr>
</table>
SELECT CalDate
, DayN
, WeekN
, YearN
, DayOfMonthN
, [Start Date]
, [End Date]
, [Inspection Number]
, [Dates] = STUFF((
SELECT ',' + CONVERT(VARCHAR(10), C.CalDate,105)
FROM #Calendar AS C
WHERE C.CalDate BETWEEN IR.[Start Date] AND IR.[End Date]
FOR XML PATH('')
), 1, 1, '')
FROM #Calendar AS dateInfo
LEFT JOIN [Inspection Records] AS IR
ON IR.[Start Date] = dateInfo.CalendarDate
This will put all your dates into a variable named #csv.
I'm sure you'll be able to modify to suit your needs.......
DECLARE #csv nVarchar(max)
SELECT #csv = COALESCE(#csv + ', ', '') + CONVERT(Nvarchar(20), yourDateColumn)
FROM yourDateTable

How to remove sql server date time default value

Hi I am pretty new to sql and I keep getting the sql default value 1900-01-01 00:00:00.000. I just want the field to be empty. I thought if I put '' the empty string it would use that instead of the default value. I commented the line where I think the problem is. I have tried other ways like ISNULL and Convert but that does not work. Thanks in advance and if you can explain how the default date works and how to override it that would be much appreciated.
DECLARE #custAccount nvarchar(20)
SET #custAccount='CUS07869'
SELECT 'SM' as [Origin]
,'SN' as [Action Code]
,ST.PurchOrderFormNum as [Order Number]
,SL.ITEMID AS [LINE ITEM]
,SL.QTYORDERED as [Quantity]
,S.SHIPMENTDATE as [Action Date]
,S.TRACKINGNUMBER as [TRACKING]
,ISNULL(S.SHIPMENTDATE, '') as [DATE_SHIPPED]
,S.SHIPMETHOD as [Carrier]
FROM SalesTable ST
INNER JOIN GBP_Shipping S
ON ST.SalesId=S.SalesId AND ST.DataAreaId=S.DataAreaId
INNER JOIN SalesLine SL
ON ST.SalesId=SL.SalesId AND ST.DataAreaId=SL.DataAreaId
WHERE ST.CUSTACCOUNT=#custAccount
AND S.SHIPMENTDATE = convert(varchar, getdate(), 111)
AND ST.salestype!=4
UNION
SELECT 'SM' as [Origin]
,'AN' as [Action Code]
,ST.PurchOrderFormNum as [Order Number]
,SL.ITEMID AS [LINE ITEM]
,SL.QTYORDERED as [Quantity]
,replace(convert(varchar, getdate(), 111), '/', '') as [Action Date]
,'' as [TRACKING]
,'' as [DATE_SHIPPED] --I think this is the problem
,'' as [Carrier]
FROM SalesTable ST
INNER JOIN SalesLine SL
ON ST.SalesId=SL.SalesId AND ST.DataAreaId=SL.DataAreaId
WHERE ST.CUSTACCOUNT=#custAccount
AND ST.CREATEDDATE = convert(varchar, getdate(), 111)
In the first part of your union sets the type of the data in the column.
try
,case when S.SHIPMENTDATE is null then ''
else Convert(VarChar(10),S.SHIPMENTDATE)
end as DATE_SHIPPED
instead
of
,ISNULL(S.SHIPMENTDATE, '') as [DATE_SHIPPED]
SQL does not have an "empty" default date -- you have to set something there. Under the hood they are stored as a binary value, and a binary value of 0 gets transalted to something; here, I'd guess you are using smalldatetime, and I'm reasonably sure 0 = Jan 1, 1900. (Datetime of 0 gets you something in 1753, when the Gregorian calendar officialy started. I'm not sure what coin they flipped for the date/time datatypes new to SQL 2008.)
If you want "no date", you'll probably have to use NULL values, and check for them wherever relevant with the isnull function. Irritating, perhaps, but IMHO it's one of the most legit uses of NULL values in SQL.