|
Try the query out - you will find that it selects the data from next calendar month
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
modified 13-Aug-14 5:22am.
|
|
|
|
|
Have a look at the DATEDIFF function.
E.g.
select datediff(mm,getdate(),getdate()+180)
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
modified 12-Aug-14 4:15am.
|
|
|
|
|
Hi, i've been looking at moving one of our processed from excel (+vba) into t-sql to make life easier but am stuck.
We have lots of tasks that are assigned to work groups which we want to distribute evenly across the work groups. This is a simple task for ntile.. However when these tasks are no longer required they are removed which leaves the groups uneven. When new tasks are added we want to still try to keep these groups balanced.
EG Existing groups :
GroupName - Task Count
Group1 - 1000
Group2 - 999
Group3 - 998
If we were to add 6 new tasks they would have more assigned to Group 2 & 3 as they have less than group 1.
Task 1 - Group3
Task 2 - Group3
Task 3 -Group2
Task 4 - Group1
Task 5 - Group2
Task 6 - Group3
Sample tables
Create table GroupTable
(GroupID int, Name varchar(200) )
Insert into GroupTable values (1,'Group1')
Insert into GroupTable values (2,'Group2')
Insert into GroupTable values (3,'Group3')
Create table Jobs(jobid int identity(1,1), name varchar(100),Groupid int)
Insert into Jobs(name,Groupid) values ('Task1',1)
Insert into Jobs(name,Groupid) values ('Task2',1)
Insert into Jobs(name,Groupid) values ('Task3',1)
Insert into Jobs(name,Groupid) values ('Task4',1)
Insert into Jobs(name,Groupid) values ('Task5',2)
Insert into Jobs(name,Groupid) values ('Task6',2)
Insert into Jobs(name,Groupid) values ('Task6',2)
Insert into Jobs(name,Groupid) values ('Task7',3)
Insert into Jobs(name) values ('TaskA')
Insert into Jobs(name) values ('TaskB')
Insert into Jobs(name) values ('TaskC')
Insert into Jobs(name) values ('TaskD')
Insert into Jobs(name) values ('TaskE')
Insert into Jobs(name) values ('TaskF')
This gives us 6 unassigned tasks and a uneven group assignment
GROUPNAME TASK_COUNT
<none> 6
Group1 4
Group2 3
Group3 2
This means the new tasks will be assigned like this
TaskA - Group3
TaskB - Group3
TaskC - Group2
TaskD - Group1
TaskE - Group2
TaskF - Group3
Can anyone help?
Thanks
Dan
|
|
|
|
|
Insert new tasks like this:
insert into jobs
select top(1)
@TaskName,
groupid
from jobs
group by groupid
order by count(*),groupid
|
|
|
|
|
Good Day All
i have a Following Query
1) Query 1
DECLARE @FINALVAUE VARCHAR(MAX) = (Convert(decimal,@CAUSATIVE_FACTORS_ID) - 1) + Convert(decimal,@LU_PERCENTAGE_FAILURE_IN_SAMPLE_ID) + Convert(decimal,@LU_REPORTING_HISTORY_ID)
PRINT @FINALVAUE
The Calculation is like this
2) Query 2
DECLARE @FINAL VARCHAR(MAX)
SET @FINAL= (4-1) + 0.08 + 0.2
SELECT @FINAL
The first Query i am retrieving values from the Variables which are varchar and converting to decimal and doing the calculations. and the second Query i am just taking the value as is. i used this to debug in trying to solve this problem. My problem here in the first Query in the @FINALVAUE variable the answer is "3" and in the second Query the answer is "3.28"
, i want the first Query to display the same value which is "3.28"
Thanks
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa[at]dotnetfunda.com
http://www.Dotnetfunda.com
|
|
|
|
|
CONVERT(DECIMAL, value) will convert without decimal palces ...
Use something like CONVERT(DECIMAL(4, 2), value)...
http://msdn.microsoft.com/en-us/library/ms187746.aspx[^]
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Thanks that saved the day.
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa[at]dotnetfunda.com
http://www.Dotnetfunda.com
|
|
|
|
|
I'm trying to consolidate 3 operations into 1
Not sure if it's possible, but it's seems like it is.
I want to select some data, and take the data from 1 table and update it to another table.
Her's what I have. It's kind of a soup at the moment, experienenting with different ideas.
In my Create Table, I can't figure out how to use those value in the update in place of the parameters.
Perhaps I should just use the line in Update, the select for CardLabel.
DECLARE @CardID INT;
SET @CardID = 645;
CREATE TABLE #CCInfo
(
Label VarChar(40)
, CardBrand VarChar(80)
, CardExpMonth VarChar(80)
, CardExpYear VarChar(80)
, CardHolder_FirstName VarChar(80)
, CardHolder_LastName VarChar(80)
, CardNum VarChar(80)
, CardPhoneNum VarChar(20)
)
INSERT INTO #CCInfo
(
Label
, CardBrand
, CardExpMonth
, CardExpYear
, CardHolder_FirstName
, CardHolder_LastName
, CardNum
, CardPhoneNum
)
SELECT
Label
, CardBrand
, CardExpMonth
, CardExpYear
, CardHolder_FirstName
, CardHolder_LastName
, CardNum
, CardPhoneNum
FROM CardInfoPerm
WHERE CardID=@CardID
UPDATE
OrderInfo
SET
CardID=@CardID
, Cardlabel= (SELECT Label FROM CardInfoPerm WHERE CardID=@CardID)
, CardBrand=Card_
, CardExpMonth=@CardExpMonth
, CardExpYear=@CardExpYear
, CardHolder_FirstName=@CardHolder_FirstName
, CardHolder_LastName=@CardHolder_LastName
, CardNum=@CardNum
, CardPhoneNum=@CardPhoneNum
, PaymentInfo=@CardID
, ApprovalCode=''
, AuthorizationID=''
, CheckID=''
, CheckLabel=''
, CheckType=''
, CheckName=''
, BankName=''
, CheckRoutingNum=''
, CheckAccountNum=''
WHERE OrderNum='CA-2054'
|
|
|
|
|
Here I get must declare the scalar @CCInfo in the UPDATE Section
I understand what it means, I just don't know how to go about getting the scalar to be recognized in Update.
The first part works, just having trouble with the update.
DECLARE @CardID INT, @OrderNumber VarChar(40);
SET @CardID = 645;
SET @OrderNumber = 'CA-2054';
DECLARE @CCInfo TABLE
(
Label VarChar(40)
, CardBrand VarChar(80)
, CardExpMonth VarChar(80)
, CardExpYear VarChar(80)
, CardHolder_FirstName VarChar(80)
, CardHolder_LastName VarChar(80)
, CardNum VarChar(80)
, CardPhoneNum VarChar(20)
);
INSERT @CCInfo
(
Label
, CardBrand
, CardExpMonth
, CardExpYear
, CardHolder_FirstName
, CardHolder_LastName
, CardNum
, CardPhoneNum
)
SELECT
Label
, CardBrand
, CardExpMonth
, CardExpYear
, CardHolder_FirstName
, CardHolder_LastName
, CardNum
, CardPhoneNum
FROM CardInfoPerm
WHERE CardID=@CardID
UPDATE
OrderInfo
SET
CardID=@CardID
, Cardlabel=@CCInfo.Label
, CardBrand=@CCInfo.CardBrand
, CardExpMonth=@CCInfo.CardExpMonth
, CardExpYear=@CCInfo.CardExpYear
, CardHolder_FirstName=@CCInfo.CardHolder_FirstName
, CardHolder_LastName=@CCInfo.CardHolder_LastName
, CardNum=@CCInfo.CardNum
, CardPhoneNum=@CCInfo.CardPhoneNum
, PaymentInfo=@CardID
, ApprovalCode=''
, AuthorizationID=''
, CheckID=''
, CheckLabel=''
, CheckType=''
, CheckName=''
, BankName=''
, CheckRoutingNum=''
, CheckAccountNum=''
WHERE OrderNum=@OrderNumber
|
|
|
|
|
Separate the statements with semicolons?
|
|
|
|
|
I went for the later in this function, but found out later that I could of just declared and set all the card data first
and then run the UPDATE using the variables.
So I ended up with this, I shortened it to keep it more brief
UPDATE " & _
OrderInfo " & _
SET
CardID=@CardID
, Cardlabel=(SELECT Label FROM CardInfoPerm WHERE CardID=@CardID)
, CardBrand=(SELECT CardBrand FROM CardInfoPerm WHERE CardID=@CardID)
, CardExpMonth=(SELECT CardExpMonth FROM CardInfoPerm WHERE CardID=@CardID)
, CardExpYear=(SELECT CardExpYear FROM CardInfoPerm WHERE CardID=@CardID)
WHERE OrderNum=@OrderNum"
But later the next day I ended up writing something that does everything in one shot, far beyond I could imagine that I can do. So I modified the above to this below
DECLARE " & _
@CardLabel VarChar(80)
, @CardBrand VarChar(80)
, @CardExpMonth VarChar(80)
, @CardExpYear VarChar(80)
, @New_OrderNumber VarChar(80);
SET @CardLabel = (SELECT Label FROM CardInfoPerm WHERE CardID=@CardID);
SET @CardBrand = (SELECT CardBrand FROM CardInfoPerm WHERE CardID=@CardID);
SET @CardExpMonth = (SELECT CardExpMonth FROM CardInfoPerm WHERE CardID=@CardID);
SET @CardExpYear = (SELECT CardExpYear FROM CardInfoPerm WHERE CardID=@CardID);
IF EXISTS(SELECT * FROM OrderInfo WHERE OrderNum=@OrderNum)
BEGIN
UPDATE
END
ELSE
BEGIN
INSERT
END
Then, I was able to do more,
delete previous cart items,
generate a new order number,
copy the cart contents over
return the new order number
all in one shot now, use to take me like 8 functions to do it all. I'm amazed at how fast it runs now.
I would post it, but it's too large
I'm stoked on how it came out, and learned a lot on my own. I think I got the semi-colons right this time, I need to read up on that.
|
|
|
|
|
The Select-Statement have to run into a variable. For all recorset in the variable you have to carry out the update statement
|
|
|
|
|
I have created a UNION query as the source for a report that is written to a Word Document using VBA. The UNION joins a Table (historical information from before the current database Application was written) and the output of a Query that extracts corresponding information from the live data. I have recently added a new text field to both 'sides'. The value of the text field for the Query (either "S" or "") is generated in an IIF() statement within it.
When I Open the UNION query via the Access interface, it works as intended - the new text field is set to the correct value for records derived from either source. When I open a ADO Recordset based on the UNION query in VBA, the records derived from the Table are correct, but the contents of the new field in the records derived from the Query is always a Null String (N.B. not Null). If I convert the Query which is the second source of the UNION into a Make Table Query, run it, and then UNION the two Tables, everything again works as it should. Does this make any sense to anyone?
|
|
|
|
|
Hello, I would like to try either SSMS 2012 or SSMS 2014. However I would like to keep my SQL Server 2008R2 instance.
First what is your suggestion on downloading? I hear there are changes for 2014 that make it very different.
If I download SQL Server Express will it install an instance or can I choose to just install SSMS. Is there a download just for SSMS.
Thank you,
|
|
|
|
|
There are various downloads available for SQL Express, including one that's just SSMS.
- 2014 Management Studio:
- 2012 Management Studio:
- 2008 R2 Management Studio:
You can get a reasonably complete list at: http://downloadsqlserverexpress.com/[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you, I seem to find the wrong download (full instance) when I look.
|
|
|
|
|
How to execute the below piece of code correctly. Please help me.
INSERT INTO #TempTable
(PatchAppId, PatchName,HostName)
if (@Id = 0)
BEGIN
SELECT PatchAppId, PatchName,HostName FROM Vw_ProductionAutoMailData1
where Criticality=@Criticality
end
else
begin
SELECT PatchAppId, PatchName,HostName FROM Vw_ProductionAutoMailData2
where ServerTypeName=@ServerTypeName
end
|
|
|
|
|
Erk!
You have an insert statement
Then an if statement on a variable that is not in scope (of the question)
Try moving the insert inside the if statement, so you will need the insert twice
if
begin
insert
select
end
else
begin
insert
select
end
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi I have legacy database that contain millions of records it has a column (DATA TYPE IS nvarchar) in database that has values in four date formats (5/29/2014) and (2014-05-15) and (16/4/2014) and (Apr 7 2014 4:42PM)
I want to convert it into (16/04/2014). When i use convert function it gives error message
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the nvarchar value 'Apr 7 2014 4:42PM' to data type int. We have so many scattered format so we cannot run a single query for specific data. so kindly provide solution for this problem.
these formats i have been using this statement to convet data.
select CONVERT(varchar(10), CONVERT(date,FormReceivedDate, 101), 103) from Applicant where FormReceivedDate=1
|
|
|
|
|
I would suggest using the DATE data type. SQL server saves the date as 20140416. You only need to convert(date, FormReceivedDate). Formatting of the data is done for output.
|
|
|
|
|
AzeeM_R wrote: Conversion failed when converting the nvarchar value 'Apr 7 2014 4:42PM' to data type int
You shouldn't be trying to convert the data to an int . SQL has specific data types for storing date/time data[^], which you should be using instead.
The yyyy-MM-dd and MMM d yyyy h:mmTT formats are easy to convert:
SELECT
Convert(date, '2014-05-01'),
Convert(datetime2(0), 'Apr 7 2014 4:42PM')
;
The other two formats will be impossible to convert unless your data contains another field indicating whether they're dd/MM/yyyy or MM/dd/yyyy . Without that additional field, there would be no way to know whether 01/02/2014 should be 1st February or 2nd January.
If you do have an additional field indicating the format of the date, then your options depend on the version of SQL that you're using. SQL 2012 and 2014 have the PARSE method[^]:
SELECT
PARSE('5/29/2014' As date),
PARSE('16/4/2014' As date USING 'en-GB')
;
For older versions, you would need to write your own function to parse the string.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
If you convert the garbage you have to a single string format and store it as varchar you are perpetuating the same garbage as you are having to deal with now. USE THE DATETIME DATA FORMAT.
And yes I did mean to shout.
You might need to do a number of passes through the data using the suggestion from the other replies to convert the data. Records that are ambiguous may require the mark I eyeball to get the context of the date from other data in the record.
Altogether a thoroughly nasty job 100% caused by not storing the data in the correct format. I know it is a legacy app but if you can add new fields in the correct format then your life will be possible and the users cannot repeat the input of garbage.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi thanks for your help
we have to do it fast as we did not have time so i just copy past all values of column and paste in excel file and change the desired format
and paste it in database
|
|
|
|
|
Hello everyone,
I came across a problem when I loaded my records from SSIS to SQL DB.
My source file is a txt file.
I use 5 fields to make my records unique but unfortunately there are some records that can't be made unique.
Even the records that aren't unique must be loaded into my table although in SSIS the duplicate records are written to an error log and are excluded out of the load into the DB.
This is a small example of a few records:
record 1 and 2 are unique, 3 and 4 not.
HEADER WH DATE_CREATED TIME_CREATED PO RECEIPT ARTICLE QTY_PBS CW CODE_DATE_RECEIVED SYSTEM_FLAG QTY_PBL FLOW_RECEIVING_FLAG FLOW_DEMAND_FLAG VENDOR_ID SHIPPING_UNIT WH_FROM AANT_PALLETS SSCC_NR EMPTY_FACTURATION_FORM
1010 3 30/07/2014 11:49:24 861819693 71663 1610570 4 36507 18/08/2014 S 0 N N B986 77000 0 7 054001090102087337
1010 3 30/07/2014 11:49:24 861819693 71663 6750 1 0 18/08/2014 S 0 N N B986 77000 0 7 054001090102087337 I
1010 3 30/07/2014 11:49:24 861819693 71663 6681 4 0 18/08/2014 S 0 N N B986 77000 0 7 054001090102087337 I
1010 3 30/07/2014 11:49:24 861819693 71663 6681 16 0 18/08/2014 S 0 N N B986 77000 0 7 054001090102087337 I
In SQL I use a View to transform the data and load only the data I need from the txt file:
ALTER VIEW [dbo].[F_Received_Colli_VW]
AS
SELECT
CAST(NULL AS Numeric(8, 0)) AS DT_Start_Dates
, CAST(NULL AS NVARCHAR(6)) AS TI_Start_Times
, CAST(NULL AS Numeric(8, 0)) AS DT_Code_Dates
, NULL AS FK_Article
, NULL AS FK_FlowType
, CAST(CAST(RIGHT(dbo.AD.DATE_CREATED, 4) + SUBSTRING(dbo.AD.DATE_CREATED, 4, 2)
+ LEFT(dbo.AD.DATE_CREATED, 2) AS int) AS Numeric(8, 0)) AS CREATE_DATE
, CAST(dbo.LPAD(LEFT(dbo.AD.TIME_CREATED, 2) + SUBSTRING(dbo.AD.TIME_CREATED, 4, 2)
+ RIGHT(dbo.AD.TIME_CREATED, 2) , 6, '0') AS NVARCHAR(6)) AS CREATE_TIME
, CAST(CAST(RIGHT(dbo.AD.CODE_DATE_RECEIVED, 4) + SUBSTRING(dbo.AD.CODE_DATE_RECEIVED, 4, 2)
+ LEFT(dbo.AD.CODE_DATE_RECEIVED, 2) AS int) AS Numeric(8, 0)) AS CODE_DATE
, CAST(dbo.AD.VENDOR_ID AS nvarchar(255)) AS Vendor_ID_Cd
, CAST(CASE WHEN dbo.AD.WH IS NULL THEN 0 ELSE dbo.AD.WH END AS nvarchar(255)) AS WH_Cd
, dbo.AD.PO AS BK_PO_Cd
, dbo.AD.RECEIPT AS BK_Receipt_Cd
, CAST(dbo.Devide_Shipping_Unit(dbo.AD.SHIPPING_UNIT, dbo.AD.ARTICLE) AS Nvarchar (255)) AS BK_Shipping_Unit_Cd
, dbo.AD.SSCC_NR AS BK_License_Plate_Cd
, dbo.AD.ARTICLE AS BK_Article_Cd
, CAST(dbo.Devide_Shipping_Unit(dbo.AD.SHIPPING_UNIT, dbo.AD.ARTICLE) AS Nvarchar (255)) AS SHIPPING_UNIT
, dbo.AD.ARTICLE
, CASE DWH.dbo.D_Article.FlowType WHEN 'SAP' THEN ISNULL(CAST(dbo.AD.QTY_PBS AS int), 0)
ELSE ISNULL(CAST(dbo.AD.QTY_PBL AS int), 0) END AS M_Nr_Of_Colli
, CAST(ISNULL(dbo.AD.CW, 0) AS numeric(15,2)) / 100 AS M_Received_Weight
, 1 AS M_Nr_of_Pallets
FROM
(SELECT dbo.GETRESTARTTIMESTAMP(N'F_Received_Colli') AS restartTS) AS tmp CROSS JOIN
dbo.AD LEFT OUTER JOIN
DWH.dbo.D_Article ON (dbo.AD.ARTICLE = DWH.dbo.D_Article.BK_Article_Cd) and CAST(dbo.Devide_Shipping_Unit(dbo.AD.SHIPPING_UNIT, dbo.AD.ARTICLE) AS int) = Cast(DWH.dbo.D_Article.BK_Unit_Ship_Case_Cd AS int)
WHERE (dbo.AD.HEADER = '1010')
The fieldNames starting with BK_ are the fields I'm using to make a record unique. So I have 5 BK fields to make a record unique in the View:
1) BK_PO_Cd
2) BK_Receipt_Cd
3) BK_License_Plate_Cd
4) BK_Article_Cd
5) BK_Shipping_Unit_Cd
I think I can solve this by sorting on the BK's and then checking if the record exists.
If the record exists then the amount of field "QTY_PBS" should be added to the first record.
In case of my example it would be 1 record with an amount of 20 instead of 2 records with amount 4 and 16.
I tried to create a SP to resolve this issue but I'm failing misserably (lack of knowledge).
Can somebody please help me with this???
USE [DWH]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE Check_Duplicates
(
@License_Plate VARCHAR(18),
@Article VARCHAR(18),
@PO VARCHAR(20),
@Receipt VARCHAR(5),
@M_Nr_Of_Colli Int
)
AS
BEGIN
Set @License_Plate = '054001090102087337'
Set @Article = '0006681'
Set @PO = '0861819693'
Set @Receipt = '71663'
Set @M_Nr_Of_Colli = 16
IF EXISTS (SELECT [M_Nr_of_Colli] FROM F_Received_Colli_VW
WHERE [BK_License_Plate_Cd]=@License_Plate AND BK_Article_Cd = @Article)
BEGIN
INSERT INTO F_Received_Colli_VW VALUES(@License_Plate,@Article,@PO, @Receipt, @M_Nr_Of_Colli)
PRINT 'New Record Insert Successfuly'
END
ELSE
BEGIN
PRINT 'Id All Ready Exist'
END
END
Kind regards,
Ambertje
|
|
|
|
|
Hi i want to split column into multiple columns using c#.
Ex.
100 rows in COLUMN_A , convert into
COLUMN_A1(Firt 20 records)
COLUMN_A2(next 20 records)
COLUMN_A3(next 20 records)
COLUMN_A4(next 20 records)
COLUMN_A5(next 20 records)
is it posiblie?
modified 5-Aug-14 4:43am.
|
|
|
|
|