顯示具有 SQL 標籤的文章。 顯示所有文章
顯示具有 SQL 標籤的文章。 顯示所有文章

2015年7月13日 星期一

抓副檔名

使用者上傳的檔名, 有時會利用系統的編碼重新命名.
此時要抓上傳檔案的副檔名.

但若使用者在檔名若有多加點.來做檔案命名.
就容易抓錯.

在SQL中利用下面的寫法. 可以抓到正確的檔名.

[結果: .doc]
DECLARE @x VARCHAR(32) = 'aaa.bbb.ccc.1.doc'
SELECT Right(@x, CHARINDEX('.', REVERSE(@x)))


[結果: aaa.]
DECLARE @x VARCHAR(32) = 'aaa.bbb.ccc.1.doc'
SELECT left(@x, CHARINDEX('.', REVERSE(@x)))

2013年5月9日 星期四

不用廻圈組字串

又是記一下, 自己以後上來查方便的文章.

需求很簡單.
就是先select 出來的Table , 把Table 某一欄位值當字串組起來.


create table #tmp_listppl (empno varchar(7), ppl nvarchar(500))

 insert into  #tmp_listppl (empno, ppl) select '1',  cname from dbo.v_epaperauth

--這時Table 會長的這個樣子
-- 1,王大明
-- 1,張小芉
-- 1, 李大
 
declare  @list as nvarchar(500),@no as char(7)
set @list =''
set @no=''

update #tmp_listppl
set @list = ppl = (CASE WHEN @no <> empno THEN rtrim (ltrim(ppl))  ELSE @list + ';' + rtrim (ltrim(ppl)) END),
@no = empno



select empno,max(ppl)as auditno into #tmp from #tmp_listppl group by empno

select * from #tmp

--這時會這樣子  1, 王大明;張小芉;李大

drop table #tmp
drop table #tmp_listppl

2011年9月14日 星期三

What is the difference between len() and DATALENGTH. I tried both in a SELECT, both are returning the same value. Can someone give me an example?

DATALENGTH returns the length of the string in bytes, including trailing spaces. LEN returns the length in characters, excluding trailing spaces. For example,


SELECT
LEN('string'),
LEN('string '),
DATALENGTH('string'),
DATALENGTH('string '),
LEN(N'string'),
LEN(N'string '),
DATALENGTH(N'string'),
DATALENGTH(N'string ')


will return 6, 6, 6, 9, 6, 6, 12, 18

sql 小發現

null + 欄位  =null 
null 和任何的值相加, 都會變成null 



  isnull(@year, '')  把null 值設成空白