Missing Leading Zero In Datapart Func If .....
Jun 9, 2003
I use the following code to fill the missing leading zero from datepart sys function. Is there another way simple to get the leading zero if month < 10 and/or if day < 9?
-D
--++++++++++++++++++++++++++++++++
declare @currentYYYYMMDD char(8)
declare @currentYYYY char(4), @currentMM varchar(2), @currentDD varchar(2)
select @currentYYYY = cast(datepart(yyyy, getdate()) as char(4))
select @currentMM = cast(datepart(mm, getdate()) as varchar(2))
select @currentDD = cast(datepart(dd, getdate()) as varchar(2))
print '@currentYYYY: ' + @currentYYYY + ' @currentMM:' + @currentMM + ' @currentDD:' + @currentDD
select @currentMM = case when @currentMM < 10 then '0' + @currentMM
else @currentMM end
select @currentDD = case when @currentDD < 10 then '0' + @currentDD
else @currentDD end
print '@currentYYYY: ' + @currentYYYY + ' @currentMM:' + @currentMM + ' @currentDD:' + @currentDD
select @currentYYYYMMDD = @currentYYYY + @currentMM + @currentDD
print '@currentYYYYMMDD: ' + @currentYYYYMMDD
View 2 Replies
ADVERTISEMENT
Jul 3, 1999
hello,..
please take a look and tell me what's wrong.
*** declare @myfine money
select @myfine=sum(
select fp.total_fine from final_payment fp,inserted i
where fp.member_no=i.member_no and fp.return_complete=0)***
i have final_payment table and everything.I'm creating this syntax in an insert trigger on my final_payment table. will @myfine value be the sum of total_fine where the conditions are true?please help
truly,
JC
View 1 Replies
View Related
Jun 6, 2008
Hey Guys, I am creating a scalar function for the first time. I have googled, ck'd your FAQ's, looked 2 text books but cannot find the answer to my questions.
Is the IF statement used in the Scalar function like it is in the stored procedure. In other words does it have to return a Boolean? I want to perform a loop after I call the function, do I have to use a Cursor? Or can I code it like a VB function with a FOR/DO While? Also I can call this UDF from my Stored Proc, right?
Here is a quick synopsis of what I have been assigned to do:
--- Calculate Run Dates
--(start from Previous Saturday subtract 41 days
-- if the 41st day does not fall on a Saturday (day = 7)
-- keep subtracting days until Day=Saturday. Add 6 days to make
-- END date the following Friday)
Thanx,
Trudye
View 11 Replies
View Related
Jun 17, 2006
Hi to all,
Is It possible to use dynamic qyery inside a function in sql server.
For Example:
Create function fn_Test
Returns Table
As
Return sp_ExecuteSql 'SELECT * FROM EMP'
Like this.
With regards
Amjath
View 10 Replies
View Related
Jul 25, 2006
All,
I need to use ISNULL function in a Conditional split transformation. Data will be split based on the ISNULL function. ISNULL( col) can get all the null records, How to get the not null records? ISNULL(col) = €śfalse€? doesn€™t work.
Thanks in Advance
View 6 Replies
View Related
Oct 30, 2006
Hi all!
I'm trying to use a UDF that returns a table, but I'm not sure of the syntax to invoke it. I've found examples in BOL and on-line like the following:
SELECT * FROM dbo.fn_MyTableFunc( 123.09, 'MyID' )
But I need the input parameter to be obtained from another table. For a very simplistic example, I've got 4 tables (and yes, I know that I can get the results I want for this example without using a UDF, but humor me):
CREATE TABLE tUser (UserID int PRIMARY KEY, UserName varchar(50))
CREATE TABLE tAcctGroup (AcctGroupID int PRIMARY KEY, AcctGroupName varchar(50))
CREATE TABLE tAcct (AcctID int PRIMARY KEY, AcctGroupID int, AcctName varchar(50))
CREATE TABLE tMapUserToGroup (UserID int, AcctGroupID int)
GO
INSERT INTO tUser VALUES (111, 'Me')
INSERT INTO tAcctGroup VALUES (1, 'NY')
INSERT INTO tAcct VALUES (11, 1, 'New York City')
INSERT INTO tAcct VALUES (12, 1, 'Syracuse')
INSERT INTO tAcctGroup VALUES (2, 'GA')
INSERT INTO tAcct VALUES (21, 2, 'Atlanta')
INSERT INTO tAcct VALUES (22, 2, 'Savannah')
INSERT INTO tAcct VALUES (23, 2, 'Augusta')
INSERT INTO tAcctGroup VALUES (3, 'TX')
INSERT INTO tAcct VALUES (31, 3, 'Dallas')
INSERT INTO tAcct VALUES (32, 3, 'Houston')
INSERT INTO tAcct VALUES (33, 3, 'El Paso')
INSERT INTO tAcct VALUES (34, 3, 'San Antonio')
INSERT INTO tAcctGroup VALUES (4, 'CA')
INSERT INTO tAcct VALUES (41, 4, 'Los Angeles')
INSERT INTO tAcct VALUES (42, 4, 'San Francisco')
INSERT INTO tMapUserToGroup VALUES (111,2)
INSERT INTO tMapUserToGroup VALUES (111,4)
GO
CREATE FUNCTION dbo.ufnGetAcctList(@AcctGroupID int) RETURNS @tAcct table (AcctID int, AcctName varchar(50))
AS
BEGIN
INSERT INTO @tAcct
SELECT AcctID, AcctName FROM tAcct WHERE AcctGroupID = @AcctGroupID
RETURN
END
GO
I know that I can do:
SELECT * FROM TestDB.dbo.ufnGetAcctList(4)
But I want the equivalent of:
SELECT AcctID, AcctName FROM tAcct
WHERE AcctGroupID IN (SELECT AcctGroupID FROM tMapUserToGroup WHERE UserID = 111)
Which uses tMapUserToGroup to obtain the AcctGroupID to pass into the function. The results would be:
AcctID AcctName
-----------------------------
21 Atlanta
22 Savannah
23 Augusta
41 Los Angeles
42 San Francisco
Any thoughts?
Thanks in advance for your help.
Cat
View 7 Replies
View Related
Jul 18, 2007
I have code
Function GetDealCount(reportItems)
return iif(IsNothing(reportItems!txtDetailCountRows.Value), 0, reportItems!txtDetailCountRows.Value)
End Function
Function GetSumNotionalAmount(reportItems)
return iif(IsNothing(reportItems!txtDealSumNotionalAmount.Value), 0, reportItems!txtDealSumNotionalAmount.Value)
End Function
That I am calling from a textboxes in the page header
= Code.GetDealCount(ReportItems) & " Deal(s)"
also
= Parameters!BaseCurrency.Value + " " + Format(Code.GetSumNotionalAmount(ReportItems),"N2").ToString().Replace(",","'")
When I preview the report in VS.NET I get values showing.
When I deploy the report I just get #Error showing.
Also this report used to work fine in RS2000
Does anyone know the cause of this issue?
View 6 Replies
View Related
May 29, 2008
is there a way to create
"CREATE TABLE @table ( blah blah blah...."
from a table value function?
View 1 Replies
View Related
Dec 3, 2002
Hi over there, my first visit here and already the first question:
I need a SELECT-statement which formats all one-digit numbers with a leading zero.
IE:
1->01
2->02
7->07
10->10
11->11
View 5 Replies
View Related
Aug 3, 2004
I have fields like '0006','0007','0004'
etc. I need to convert this in 4 char field as right justify 6,7,4. How would I do that?
Another question:
fields in input file as 77,77.0,77.90,77.99,
I need to show them right justify in 10 chars field as 77.00,77.00,77.90 and 77.99
How would I do that?
Please help!!!!!!!!!
Thanks in advance!!
View 1 Replies
View Related
Oct 3, 2005
I am not a DBA whatsoever so please bear with me ...
There is an existing SQL table with 3 fields that are to gather numeric data:
Name - Data Type - Length
1. TPID1 - int - 4
2. TPID2 - int - 4
3. BPIN - int - 4
If any of the 3 above are submitted with a zero in front (input = 01234), the zero will not show. (output = 1234)
I thought if I changed Data Type to nvarchar, the data would be read just like text and appear as entered. (I reviewed other table designs and nvarchar is the data type for other similar data.) Saved the table changes and still the leading zero does not show.
Any advice would be appreciated - thanks
View 4 Replies
View Related
May 16, 2008
declare @Mymonth as varchar(2)
set @MyMonth = '0' + datepart(m,getdate())
print @MyMonth
why wont the above produce 05 just 5
View 2 Replies
View Related
Aug 21, 2006
Hi all,
Anyone know how to transfer data from 1 or 2 characters to 3 characters.
For example we have mpc_code in source as 1, 2, 3 then target need to be 001, 002 and 003.
Then if 12, 13 then change to 012, 013. Put zero as leading space.
Thanks.
Grace
View 6 Replies
View Related
Oct 4, 2006
I'm trying to write the contents of a csv file to a table, but I am having problems with fields with leading zeroes. Whenever I save as csv I lose the leading zeroes. Does anybody know how to prevent this?
View 1 Replies
View Related
May 6, 2008
I would like to add leading zeros in the date. Thsi is my existing procedure, it adds leading zeros, but it formats using "yyyy/mm/dd", instead of "yyyy-mm-dd"
Select
Id, Title, CONVERT(VARCHAR(10), ModifiedON, 111)
--CAST(YEAR(ModifiedOn) AS VARCHAR(4))+'-'+CAST(MONTH(ModifiedOn) AS VARCHAR(2))+'-'+CAST(DAY(ModifiedOn) AS VARCHAR(2))as ModifiedOn
From
ActiveAds
Where
Row between @startRowIndex And @endRowIndex
View 2 Replies
View Related
Jun 17, 2002
I have some results data which looks like below
.00
.00
1.0
13.0
6.0
I need to push it into a table that accepts whole numbers. (none of the results actually will have a decimal like .05 or 1.5, all .00 will be 0)
What I need in the results is whole numbers, but if the results are 6.00 I need 06 to go into the table. I know a straight int convert will drop the decimals, and I could use a csae to set the .00 to 00, but what would be the best way to change the 6.00 to 06?
THanks
View 1 Replies
View Related
Sep 20, 2005
MSSQL2000
Brain is overloaded and I'm just not getting this! Ugh! I need a field that will be exported/displayed to contain 10 characters, no spaces. The field I'm extracting is 8 characters and the numerical data (int) is any range up to that. So I have 35795 and need it to be 0000035795 but I could also have a 1057893 and will need it to be 0001057893. I tried various forms of this...
Select '00' + Right (chk_no, 8)
and it's just not correct.
Anyone have a suggestion on what I'm not seeing?
TIA!
View 14 Replies
View Related
May 13, 2008
Hi
I am unable to see how to generate a leading zero.
Table A
declare @TableA table ( ID numeric ,
Fruits varchar(10)
)
insert @TableA
select 1,'Oranges'
union all select 2,'Mangoes'
union all select 3,'Apricots'
ID Table A
1 Apples
2 Oranges
3 Grapes
4 Apricots
declare @TableB table ( seed numeric ,
)
insert @TableB
select 080513000448
union all select 080513000449
union all select 080513000450
Table B
seed
080513000448
080513000449
080513000450
I wrote the following query but i need generate a leading zero not sure which function can help maybe the right function but i am not sure how to use it in this case
SELECTconvert(varchar(10), getdate(), 12) +
(SELECT
CASE
WHEN SUBSTRING(ISNULL(max(seed),'00000'),1,6) = convert(varchar(10), getdate(), 12)
THEN SUBSTRING(ISNULL(max(seed),'00000'),7,12)
ELSE '000000'
END AS SEED
FROM B)
+ (row_number()
over (order by Id))
as SEED
FROM A
SEED
----
80513000451
The output which i need is
SEED
----
080513000451
rather then
SEED
----
80513000451
regards
Hrishy
View 20 Replies
View Related
Oct 31, 2005
can anyone tell me how to design a table that has an INT value that keeps the leading zeros???
no if i put in
0003453
i get
3453
thanks
View 12 Replies
View Related
Mar 31, 2008
Hiya all,
I have a linked table between SQL and BTrieve.
I have a column that returns 1 ,2 ,10 etc.
In Btrieve the datatype is numeric and 3 digits long.
But in SQL I want 001 , 002 ,010.
Anyone got some code to programatically add 00 if 1,2,3
or add o if 10,11 or add none if 100,101
I also tried casting as in CAST(table.ID AS NUMERIC(3,0)) as Del_ID,
but SQL still returns 1,2,10 and not 001,002,010
View 3 Replies
View Related
Aug 20, 2007
Hi All,
I want to add a leading zero to a field based on a param that I create on the fly in my stored proc. I have a @month which is created from my datetime param @date.
@Month needs to be char(2) but if the month is inputted as '04' I get '4 ' in the table (note the space after 4)
How can I add a leading zero to this field?
Set @Year = right('0',1)year(@Date) is spitting it's toys out.
Thanks,
Brett
View 1 Replies
View Related
Jul 26, 2007
Hi Folks,
I have a situation where I need to display an integer with leading zeros, with a defined length.
Example, 43 appears as 00043 when the length is 5 and 000043 when the length
is 6.
I tried using "=Format(Fields!DirID.Value.ToString)" with different variations to no avail.
Any ideas will be appreciated.
Regards
View 3 Replies
View Related
May 31, 2007
mssql 2000, asp.net(vbscript)
How am i able to trim leading zeros? Right now i have two values:00000005 500000010 1000000015 15..... etc...
how do i write a query where i can select an argument where 5 = 0000005?
the column with 00000005 is varchar and5 is numeric
View 4 Replies
View Related
Aug 23, 2001
What is the best way to check for leading spaces in your table, using ltrim?
Such as TableA(name, city)
the data in TableA Smith Dallas
John New York
Greg Richmond
David Chicago
Return only David.
View 1 Replies
View Related
Jun 7, 2004
I have a problem while importing data from Excel to SQL Server.The leading zeros in data get truncated.Even if I try and change the excel data column as 'Text' and copy paste the data back into the Text column, the problem persists.Does any one have any thoughts about this problem?
View 14 Replies
View Related
Aug 21, 2007
Is it possible to have leading zero for an auto increment id field? I need to do this when i am migrating a table. I am thinking if i cannot do this when migrating then i will have to use stored procedure to achieve this.
Thanks.
View 4 Replies
View Related
Jul 1, 2004
I need to cast an integer to a string to append to another string for a barcode.
How can I get 1 -> '0001' OR 100 -> '0100'
Any suggestions?
Mike B
View 2 Replies
View Related
Jan 31, 2008
Good afternoon,
I hope the title is self explaining. Here is an example of what i would like to do:
Before...
1
1.1
1.1.1
1.2.1
1.3.1
1.10.1
After...
01
01.01
01.01.01
01.02.01
01.03.01
01.10.01
Thanks,
Phil.
View 6 Replies
View Related
Jul 20, 2005
Hello All,None of the solutions I have found in the archives seem to solve myproblem. I have a date column in my tables (stored as a char(10))which I would like to append a leading zero to for those dates thatstart with 9 or lower.Any ideas?Thanks,Mike
View 2 Replies
View Related
Jan 25, 2008
I have an SSIS routine which uses a simple SQL select statement from a SQL Server 2005 database and then goes to a Flat File destination. The field (dischstatuscode) is a nvarchar(50) and it may contain data with leading zeros.
Code Snippet
Select DischStatusCode
from dbo.pm
...which returns:
01
23
37
05
04
41
When I open up the csv file produced by the SSIS routine, I see the following:
1
23
37
5
4
41
How can I have it retain leading zeros?
View 3 Replies
View Related
Feb 28, 2008
My records are like this.
Col1
-----
00001
03456
00577
05011
00099
01090
I want to remove the zeros on the left and the answer should be like this.
Col1
-----
1
3456
577
5011
99
1090
How to trim the leading zeros.
Thanks.
View 11 Replies
View Related
Dec 10, 2001
I loaded data into a CHAR fields from Legacy.
How to remove those leading spaces with in SQL server...?
View 1 Replies
View Related
Sep 27, 2001
I have a char(12) field that was loaded like '000000000101' I need to change the data to be ' 101'. Is there a way to do this and preserve the number and keep the leading spaces?
Thanks
View 3 Replies
View Related