Click here to Skip to main content
15,903,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Helo friedns,
today with one new problem...
my application is working fine.it is permit the user to download its file in .txt or .xml format.

i am defining the file name format in my application hard coded.

but i want to papulate the file name from my DB.

how can assign the file name from my DB.

for example

ABD_YYYY-MM-DD_TIME.txt
ABD_YYYY-MM-DD_TIME.xml

i want to get name ABD from my DB.
Posted
Comments
Jim Jos 28-May-12 7:59am    
Do you know the file names before or after the user enters the filename only you will know the filename..
prince_rumeel 28-May-12 8:02am    
i know the file name before.
i just embed the time stamp with new file which user want to save...
Jim Jos 28-May-12 8:24am    
Then have a table and store the names as varchar(!) is there something else I need to know... u wouln't ask a question for that only isn't it?
prince_rumeel 28-May-12 9:09am    
yes i had store the name of my files in table.
now i want to add the time stemp with my file name.

after that i want to attach that name with my file.which user is going to download

You are storing the information incorrectly IMO, the concatenated filename is really three discrete peices of information, it would be better to form what you have in the select statement from these. I suggest:

  1. Create a new table FooAllowedFileNameExtension
  2. In the table add a column Extension, make it varchar type and restict its length as per your requirements probably 1+
  3. Make the column in 2 its primary key


With your current table split the filename you have into three columns:
OrininalFilename, Datestamp and extension (which FKs back to the table above).

This seems like a lot of effort, but:
a) You don't lose the original name.
b) You can query better: All files uploaded on a day etc
c) You can used the allowed extensions table to determine whether a file can be added.
d) If you want to add new file types it becomes much easier: just add it to the new table.
e) The acceptable filnames are lower down the stack, so if a new system connects to it, it is less likely that the developer will allow the uploading of an unsupported file type.

Obviously you'll need to migrate the existing data in the first place, and it doesn't really answer your original problem (which both need the ability to split the string) and you may not be able to alter the schema.
You can get the ABD part relatively and file extensions pretty easily:
SQL
declare @foo varchar(max)
set @foo = 'ABD_YYYY-MM-DD_TIME.txt'
Select substring( @foo, 0, CHARINDEX ('_',@foo, 0))
Select substring( @foo, CHARINDEX ('.',@foo, 0)+ 1,LEN(@foo) -CHARINDEX ('.',@foo, 0) )





This looks like a useful discussion[^] it discusses various custom split functions.
 
Share this answer
 
Comments
Prasad_Kulkarni 28-May-12 8:56am    
Good answer, +5!
It's simple as this example:
SQL
SELECT DB_NAME() + '_' + CONVERT(VARCHAR(10),GETDATE(),120) + '.txt' AS [FileName]


More about date and time convertion: CAST and CONVERT[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900